-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtests-basic.c
93 lines (77 loc) · 2.44 KB
/
tests-basic.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
#include "shmalloc.h"
#include <stdio.h>
#include <string.h>
#define MEM_SIZE 50000
int main(int argc, char *argv[])
{
char mem[MEM_SIZE];
size_t size = 10;
void *ptr[10];
memset(mem, 0, MEM_SIZE);
//Basic usage of malloc
printf("Allocating ptr0\n");
ptr[0] = shmalloc(1, &size, mem, MEM_SIZE);
printf("Freeing ptr0\n");
shmfree(ptr[0], MEM_SIZE, mem);
//Allocate 2 things
printf("Allocating ptr0\n");
ptr[0] = shmalloc(1, &size, mem, MEM_SIZE);
printf("Allocating ptr1\n");
ptr[1] = shmalloc(1, &size, mem, MEM_SIZE);
//Checking that size is right
if(size != 10)
{
fprintf(stderr, "Size returned from shmalloc is %lu, expected 10\n", (unsigned long) size);
return 1;
}
printf("Freeing ptr0\n");
shmfree(ptr[0], MEM_SIZE, mem);
printf("Freeing ptr1\n");
shmfree(ptr[1], MEM_SIZE, mem);
//Free something twice
printf("Attempting to free ptr0 again\n");
shmfree(ptr[0], MEM_SIZE, mem);
//Free ptr not returned by malloc
printf("Allocating ptr0\n");
ptr[0] = shmalloc(1, &size, mem, MEM_SIZE);
printf("Freeing invalid ptr\n");
shmfree(((char *) ptr[0] + 1), MEM_SIZE, mem);
printf("Freeing ptr0\n");
shmfree(ptr[0], MEM_SIZE, mem);
//Allocate super big thing
printf("Allocating more than we have\n");
size = MEM_SIZE - sizeof(Header) + 10;
ptr[0] = shmalloc(2, &size, mem, MEM_SIZE);
if(ptr[0] != NULL)
{
fprintf(stderr, "Failed test to allocate more than we have.\n");
return 1;
}
//Two references
printf("Testing multiple references\n");
size = sizeof(int);
printf("Allocating first\n");
ptr[0] = shmalloc(9, &size, mem, MEM_SIZE);
*((int *)ptr[0]) = 5;
printf("Getting second\n");
ptr[1] = shmalloc(9, &size, mem, MEM_SIZE);
if(*((int *)ptr[1]) != 5)
{
fprintf(stderr, "Multiple refernces did not work. Got %d, expecting 5\n", *((int *)ptr[1]));
return 1;
}
printf("Freeing first\n");
shmfree(ptr[0], MEM_SIZE, mem);
printf("Freeing second\n");
shmfree(ptr[1], MEM_SIZE, mem);
//Alloc free alloc free
printf("Allocating ptr0\n");
ptr[0] = shmalloc(50, &size, mem, MEM_SIZE);
printf("Freeing ptr0\n");
shmfree(ptr[0], MEM_SIZE, mem);
printf("Allocating ptr0\n");
ptr[0] = shmalloc(50, &size, mem, MEM_SIZE);
printf("Freeing ptr0\n");
shmfree(ptr[0], MEM_SIZE, mem);
return 0;
}