MCPcopy Create free account
hub / github.com/emscripten-core/emscripten / test_realloc

Function test_realloc

test/core/test_emmalloc.c:87–145  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

85}
86
87void test_realloc() {
88 stage("realloc0");
89 emmalloc_blank_slate_from_orbit();
90 for (int i = 0; i < 2; i++) {
91 char* ptr = (char*)malloc(100);
92 stage("realloc0.1");
93 char* raptr = (char*)realloc(ptr, 1);
94 assert(raptr == ptr);
95 stage("realloc0.2");
96 char* raptr2 = (char*)realloc(raptr, 100);
97 assert(raptr2 == ptr);
98 char* last = (char*)malloc(1);
99 assert(last >= ptr + 100);
100 // slightly more still fits
101 stage("realloc0.3");
102 char* raptr3 = (char*)realloc(raptr2, 11);
103 assert(raptr3 == ptr);
104 // finally, realloc a size we must reallocate for
105 stage("realloc0.4");
106 char* raptr4 = (char*)realloc(raptr3, 1000);
107 assert(raptr4);
108 assert(raptr4 != ptr);
109 // leaving those in place, do another iteration
110 }
111 stage("realloc1");
112 emmalloc_blank_slate_from_orbit();
113
114 // realloc of NULL is like malloc
115 assert(realloc(NULL, 10) != 0);
116
117 stage("realloc2");
118 emmalloc_blank_slate_from_orbit();
119 {
120 // realloc to 0 is like free
121 void* ptr = malloc(10);
122 assert(realloc(ptr, 0) == NULL);
123 }
124 stage("realloc3");
125 emmalloc_blank_slate_from_orbit();
126 {
127 // realloc copies
128 char* ptr = (char*)malloc(10);
129 *ptr = 123;
130 for (int i = 5; i <= 16; i++) {
131 char* temp = (char*)realloc(ptr, i);
132 assert(*temp == 123);
133 assert(temp == ptr);
134 }
135 stage("realloc3.5");
136 malloc(1);
137 malloc(100);
138 {
139 char* temp = (char*)realloc(ptr, 17);
140 assert(*temp == 123);
141 assert(temp != ptr);
142 ptr = temp;
143 }
144 }

Callers 1

mainFunction · 0.85

Calls 5

stageFunction · 0.85
mallocFunction · 0.70
reallocFunction · 0.70
assertFunction · 0.50

Tested by

no test coverage detected