-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvlibc_tests.c
173 lines (144 loc) · 6.26 KB
/
vlibc_tests.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
// clang -o main.o vlibc_tests.c && ./main.o
#include <stdio.h>
#include "vlibc.h"
int main(int argc, char const* argv[]) {
// printf("vlibc_sqrt(2.0): %f\nVLIBC_M_SQRT2: %f\n", vlibc_sqrt(2.0f),
// VLIBC_M_SQRT2); printf("vlibc_sqrt(4.0): %f\n", vlibc_sqrt(4.0f));
// printf("vlibc_sin((VLIBC_M_PI/2)): %f\n", vlibc_sin((VLIBC_M_PI / 2)));
// printf("vlibc_cos((90.0)): %f\n", vlibc_cos((90.0)));
// printf("vlibc_tan((60.0)): %f\n", vlibc_tan((60.0)));
// printf("vlibc_asin((0.5)): %f\n", vlibc_asin((0.5)));
// printf("vlibc_acos((0.5)): %f\n", vlibc_acos((0.5)));
// printf("vlibc_atan((0.5)): %f\n", vlibc_atan((0.5)));
// printf("vlibc_exp((2.0)): %f\n", vlibc_exp((2.0)));
// printf("vlibc_log((VLIBC_M_E)): %f\n", vlibc_log((VLIBC_M_E)));
// printf("vlibc_log10((100)): %f\n", vlibc_log10((100)));
// printf("vlibc_pow(7.0, 2.0): %f\n", vlibc_pow(7.0, 2.0));
// printf("vlibc_ceil(2.3): %f\n", vlibc_ceil(2.3));
// printf("vlibc_floor(2.3): %f\n", vlibc_floor(2.3));
// char src[] = "test vlibc_memcpy";
// char dest[20];
// vlibc_memcpy(dest, src, sizeof(src));
// printf("%s\n", dest);
// char data[] = "abc def";
// vlibc_memmove(data + 4, data, 3);
// printf("%s\n", data);
// char dest1[20];
// char dest2[20];
// vlibc_strcpy(dest1, "test vlibc_strcpy");
// printf("%s\n", dest1);
// vlibc_strncpy(dest2, "test vlibc_strncpy", 10);
// printf("%s\n", dest2);
// char dest3[50] = "test vlibc_strcat: ";
// char dest4[50] = "test vlibc_strncat: ";
// vlibc_strcat(dest1, "appending full string");
// printf("%s\n", dest1);
// vlibc_strncat(dest2, "appending partial string", 9);
// printf("%s\n", dest2);
// char buffer1[] = "test vlibc_memcmp";
// char buffer2[] = "test vlibc_memcmp";
// char buffer3[] = "test vlibc_mem";
// // identical buffers (ASCII value cmp)
// int out1 = vlibc_memcmp(buffer1, buffer2, sizeof(buffer1));
// printf("comparison of buffer1 and buffer2: %d\n", out1);
// // different buffers (ASCII value cmp)
// int out2 = vlibc_memcmp(buffer1, buffer3, sizeof(buffer3));
// printf("comparison of buffer1 and buffer3: %d\n", out2);
// const char *a1 = "test vlibc_strcmp";
// const char *a2 = "test vlibc_strcmp";
// const char *a3 = "test vlibc_str";
// printf("strcmp (a1 vs a2): %d\n", vlibc_strcmp(a1, a2)); //
// expected 0 printf("strcmp (a1 vs a3): %d\n", vlibc_strcmp(a1, a3)); //
// expected non-zero printf("strncmp (a1 vs a3, 10): %d\n", vlibc_strncmp(a1,
// a3, 10)); // expected 0 printf("strncmp (a1 vs a3, 15): %d\n",
// vlibc_strncmp(a1, a3, 15)); // expected non-zero
// char buf[20];
// vlibc_memset(buf, '*', 10);
// buf[10] = '\0';
// printf("memset result: %s\n", buf); // expected **********
// const char *str = "test vlibc_strlen";
// vlibc_size_t len = vlibc_strlen(str);
// printf("strlen result: %llu\n", len); // expected length of "test
// vlibc_strlen" & null char
// const char *str2 = "test vlibc_strchr";
// char *found = vlibc_strchr(str2, 'c');
// printf("strchr result: %s\n", found); // expected "c_strchr"
// char *not_found = vlibc_strchr(str2, 'z');
// printf("strchr not found result: %s\n", not_found ? not_found : "NULL"); //
// expected NULL
// const char *str1 = "test vlibc_strrchr example";
// char *last_e = vlibc_strrchr(str1, 'e');
// if (last_e) {
// printf("last occurrence of 'e': %c\n", *last_e); // expected 'e'
// } else {
// printf("character not found\n");
// }
// const char *str3 = "test vlibc_strspn and vlibc_strcspn examples";
// const char *accept = "te";
// const char *reject = " ";
// vlibc_size_t span_length = vlibc_strspn(str3, accept);
// printf("length of initial segment containing only 't' or 'e': %llu\n",
// span_length); // expected 2 vlibc_size_t cspan_length = vlibc_strcspn(str3,
// reject); printf("length of initial segment not containing a space: %llu\n",
// cspan_length); // expected 4
// const char *str4 = "test vlibc_strpbrk and vlibc_strstr examples";
// const char *accept2 = "aeiou";
// const char *needle = "vlibc_strstr";
// char *first_vowel = vlibc_strpbrk(str4, accept2);
// printf("first vowel in string: %c\n", first_vowel ? *first_vowel : 'N'); //
// expected 'e' char *substr = vlibc_strstr(str4, needle); printf("substring:
// %s\n", substr ? substr : "NULL"); // expected "vlibc_strstr examples"
// char str5[] = "test,vlibc,strtok,example";
// const char *delim = ",";
// char *token = vlibc_strtok(str5, delim);
// while (token) {
// printf("token: %s\n", token);
// token = vlibc_strtok(NULL, delim);
// }
// printf("char upper: %c\n", vlibc_toupper('k'));
// printf("is alpha: %d\n", vlibc_isalpha('k'));
// printf("is num: %d\n", vlibc_isdigit('9'));
// printf("is alphanum: %d\n", vlibc_isalnum('2'));
// int _fd = vlibc_open(".gitignore", VLIBC_O_RDONLY, 0);
// printf(".gitignore fd: %d\n", _fd);
// int fd2 = vlibc_open("doesntexist.txt", VLIBC_O_RDONLY, 0);
// printf("doesntexist.txt fd: %d\n", fd2);
// int fd = vlibc_open("README.md", VLIBC_O_RDWR | VLIBC_O_APPEND, 0);
// if (fd >= 0) {
// char buffer[128];
// vlibc_ssize_t bytes_read = vlibc_read(fd, buffer, sizeof(buffer) - 1);
// if (bytes_read > 0) {
// buffer[bytes_read] = '\0';
// printf("%s", buffer);
// }
// const char *write_data = "write data";
// vlibc_ssize_t bytes_written = vlibc_write(fd, write_data,
// vlibc_strlen(write_data)); if (bytes_written > 0) {
// printf("wrote: %s\n", write_data);
// }
// vlibc_close(fd);
// }
// clang -o main.o vlibc_tests.c && ./main.o
// strace -e trace=mmap ./main.o
int* ptr = (int*)vlibc_malloc(4 * sizeof(int));
int* _ptr = (int*)vlibc_malloc(4 * sizeof(int));
int* __ptr = (int*)vlibc_malloc(4 * sizeof(int));
int* ___ptr = (int*)vlibc_malloc(4 * sizeof(int));
int* ____ptr = (int*)vlibc_malloc(4 * sizeof(int));
vlibc_free(vlibc_malloc(1000000));
vlibc_free(ptr);
vlibc_free(_ptr);
vlibc_free(__ptr);
vlibc_free(___ptr);
vlibc_free(____ptr);
printf("ptr address: %p\n", ptr);
printf("ptr address: %p\n", _ptr);
printf("ptr address: %p\n", __ptr);
printf("ptr address: %p\n", ___ptr);
printf("ptr address: %p\n", ____ptr);
*(char*)ptr = 0xFF;
if (*(char*)ptr != 0xFF) {
return 1;
}
return 0;
}