drd/tests/sem_open: Change the semaphore name (#331839)
[valgrind.git] / memcheck / tests / pdb-realloc2.c
blob81d9f4e3da608218dbd4d7e59980da1ac27c731e
2 /* A test which involves copying (using realloc) a block containing
3 some partially defined bytes. Really this is to check that
4 copy_address_range_perms in mc_main.c works. I don't think it's a
5 good test - it may well not exercise all the code in
6 copy_address_range_perms. */
8 #include <assert.h>
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include "memcheck/memcheck.h"
13 typedef unsigned char UChar;
14 typedef unsigned int UInt;
17 static UInt seed = 0;
18 static inline UInt myrand ( UInt size )
20 /* From "Numerical Recipes in C" 2nd Edition */
21 seed = 1664525UL * seed + 1013904223UL;
22 return seed % size;
25 static void barf ( int size, int offset )
27 printf("pdb-realloc2: fail: size %d, offset %d\n", size,offset);
28 exit(1);
31 void do_test ( int size )
33 int i,j,r;
34 UChar* v;
35 UChar* p = malloc(size);
36 assert(p);
37 // fill
38 seed = 0;
39 for (i = 0; i < size; i++) {
41 j = myrand( 256 * 25 );
42 //printf("%d\n", j);
43 if (j >= 256 * 13) {
44 // def 1s
45 p[i] = 0xFF;
46 } else
47 if (j >= 256 && j < 256*13) {
48 // def 0s
49 p[i] = 0;
50 } else {
51 // pdb
52 p[i] &= (UChar)j;
57 // copy
58 for (i = 1; i <= 100; i++) {
59 p = realloc(p, size+i);
60 assert(p);
63 // check
64 v = malloc(size+100);
65 assert(v);
66 r = VALGRIND_GET_VBITS(p,v, size+100);
67 assert(r == 1);
69 //for (i = 0; i < size+100; i++)
70 // printf("%02x ", (UInt)v[i]);
71 //printf("\n");
73 seed = 0;
74 for (i = 0; i < size; i++) {
76 j = myrand( 256 * 25 );
78 if (j >= 256) {
79 // expecting a defined value
80 if (v[i] != 0)
81 barf(size, i);
82 } else {
83 // expecting a PDB == j
84 if (v[i] != (UChar)j)
85 barf(size,i);
90 // in the extension area, everything should be undefined
91 for (i = 0; i < 100; i++) {
92 if (v[size+i] != 0xFF)
93 barf(size, i);
96 free(v);
97 free(p);
100 int main ( void )
102 int z;
103 for (z = 0; z < 100; z++) {
104 printf("pdb_realloc: z = %d\n", z);
105 do_test(z);
106 do_test(z + 173);
107 do_test(z + 1731);
109 printf("pdb-realloc2: done\n");
110 return 0;