-
Notifications
You must be signed in to change notification settings - Fork 0
/
mem_test.c
181 lines (154 loc) · 4.76 KB
/
mem_test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include <stdio.h>
#include <stddef.h>
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
#include <assert.h>
#include "my_malloc.h"
// Checks if free blocks have been joined
void check_block_joined()
{
struct block *current = block_list;
while (current && current->next)
{
assert(!(current->free && current->next->free));
current = current->next;
}
}
int main()
{
mem_init();
// Allocate and free
int *arr1 = (int *)my_malloc(5 * sizeof(int));
arr1[2] = 21;
printf("arr1[2] = %d\n", arr1[2]);
my_free(arr1);
check_block_joined();
// Allocate and free multiple blocks
int *arr2 = (int *)my_malloc(3 * sizeof(int));
int *arr3 = (int *)my_malloc(2 * sizeof(int));
arr2[2] = 6;
arr3[0] = 10;
printf("arr2[2] = %d\n", arr2[2]);
printf("arr3[0] = %d\n", arr3[0]);
int x = arr2[2] + arr3[0];
printf("arr2[2] + arr3[0] = %d\n", x);
my_free(arr2);
my_free(arr3);
check_block_joined();
// Allocate, free, and allocate again
int *num1 = (int *)my_malloc(sizeof(int));
num1[0] = 90;
printf("num1 = %d\n", num1[0]);
my_free(num1);
int *num2 = (int *)my_malloc(sizeof(int));
num2[0] = 80;
printf("num2 = %d\n", num2[0]);
check_block_joined();
// Allocate and free a struct
struct Test_struct
{
int a;
char b;
};
struct Test_struct *struct1 = (struct Test_struct *)my_malloc(sizeof(struct Test_struct));
struct1->a = 5;
struct1->b = 4;
printf("a = %d\n", struct1->a);
printf("b = %d\n", struct1->b);
my_free(struct1);
check_block_joined();
// Allocate and free float
float *flt1 = (float *)my_malloc(sizeof(float));
flt1[0] = 12.3455;
printf("flt1 = %f\n", flt1[0]);
my_free(flt1);
check_block_joined();
// Allocate and free multiple blocks for doubles
double *dbl1 = (double *)my_malloc(2 * sizeof(double));
double *dbl2 = (double *)my_malloc(3 * sizeof(double));
dbl1[1] = 3.1111111111;
dbl2[2] = 4.2222222222;
printf("dbl1 = %f\n", dbl1[1]);
printf("dbl2 = %f\n", dbl2[2]);
double y = dbl1[1] + dbl2[2];
printf("dbl1 + dbl2 = %f\n", y);
my_free(dbl1);
my_free(dbl2);
check_block_joined();
// Allocate and free struct of different data types
struct AnotherStruct
{
double x;
int y;
char *z;
};
struct AnotherStruct *anotherStruct = (struct AnotherStruct *)my_malloc(sizeof(struct AnotherStruct));
anotherStruct->x = 3.121216;
anotherStruct->y = 20;
anotherStruct->z = "Testing hehe";
printf("x = %f\n", anotherStruct->x);
printf("y = %d\n", anotherStruct->y);
printf("z = %s\n", anotherStruct->z);
my_free(anotherStruct);
check_block_joined();
// Allocate and free string
char *str1 = (char *)my_malloc(10 * sizeof(char));
strcpy(str1, "Hello");
printf("str1 = %s\n", str1);
my_free(str1);
check_block_joined();
// Allocate and free multiple large blocks to ensure free works (1000 byte total)
char *str2 = (char *)my_malloc(200 * sizeof(char));
char *str3 = (char *)my_malloc(700 * sizeof(char));
my_free(str2);
my_free(str3);
check_block_joined();
char *str4 = (char *)my_malloc(800 * sizeof(char));
my_free(str4);
check_block_joined();
// Allocate and free an array and fill with loops
double *doubleArray = (double *)my_malloc(5 * sizeof(double));
for (int i = 0; i < 5; ++i)
{
doubleArray[i] = i * 1.5;
}
printf("Allocated double array: ");
for (int i = 0; i < 5; ++i)
{
printf("%.2f ", doubleArray[i]);
}
printf("\n");
my_free(doubleArray);
check_block_joined();
// Allocate and free each cell individually in an array
char **stringArray = (char **)my_malloc(3 * sizeof(char *));
for (int i = 0; i < 3; ++i)
{
stringArray[i] = (char *)my_malloc(10 * sizeof(char));
sprintf(stringArray[i], "String %d", i + 1);
}
printf("Allocated string array: %s, %s, %s\n", stringArray[0], stringArray[1], stringArray[2]);
for (int i = 0; i < 3; ++i)
{
my_free(stringArray[i]);
}
my_free(stringArray);
check_block_joined();
// Use my_malloc with size 0
int *empty_array = (int *)my_malloc(0);
printf("Empty array pointer: %p\n", (void *)empty_array);
my_free(empty_array);
// Try to use my_free with NULL pointer
int *null_pointer = NULL;
my_free(null_pointer);
// Use my_malloc with a very large size
char *large_string = (char *)my_malloc(850 * sizeof(char));
strcpy(large_string, "Test");
printf("large_string: %s\n", large_string);
my_free(large_string);
check_block_joined();
free_block();
printf("\nAll tests passed successfully!\n");
return 0;
}