test.c
Raw
1#include <cutils/cutils.h>
2#include <stdio.h>
3
4struct test
5{
6 int a;
7 int b;
8};
9
10static int cmp_str(const void* str1, const void* str2)
11{
12 const struct test* str1_s = str1;
13 const struct test* str2_s = str2;
14 return !((str1_s->a == str2_s->a) && (str1_s->b == str2_s->b));
15}
16
17static void test1(void)
18{
19 size_t* find;
20 Vector* test;
21 struct test* my_struct;
22
23 my_struct = malloc(sizeof(*my_struct));
24 test = new_vector()
25
26 my_struct->a = 5;
27 my_struct->b = 5;
28 vector_push(test, my_struct);
29
30 my_struct = malloc(sizeof(*my_struct));
31 my_struct->a = 6;
32 my_struct->b = 6;
33 vector_push(test, my_struct);
34
35 my_struct = malloc(sizeof(*my_struct));
36 my_struct->a = 7;
37 my_struct->b = 7;
38 vector_push(test, my_struct);
39 printf("%ld\n", test->length);
40
41 vector_remove(test, 0,free);
42 printf("%ld\n", test->length);
43
44 my_struct = malloc(sizeof(*my_struct));
45 my_struct->a = 8;
46 my_struct->b = 8;
47 vector_insert(test, 2, my_struct);
48 printf("%ld\n", test->length);
49
50 my_struct = malloc(sizeof(*my_struct));
51 my_struct->a = 8;
52 my_struct->b = 8;
53
54 find = vector_find(test, my_struct, cmp_str);
55 printf("pos: %lu\n", *find);
56 free(find);
57
58 free(my_struct);
59 my_struct = vector_pop(test);
60 printf("struct a: %d, b: %d\n", my_struct->a, my_struct->b);
61 free(my_struct);
62
63 delete_vector(test,free);
64}
65
66static void test2(void)
67{
68 String* test;
69 String* test2;
70 char* cstring;
71
72 test = new_string();
73
74 string_push(test, 'a');
75 string_push(test, 'b');
76 string_push(test, 'c');
77 string_push(test, 'd');
78 printf("%ld\n", test->length);
79
80 cstring = to_cstring(test);
81 printf("%s\n", cstring);
82 free(cstring);
83
84 string_remove(test, 3);
85 cstring = to_cstring(test);
86 printf("%s\n", cstring);
87 free(cstring);
88 printf("%ld\n", test->length);
89
90 string_insert(test, 0,'a');
91 cstring = to_cstring(test);
92 printf("%s\n", cstring);
93 free(cstring);
94
95 test2 = from_cstring("xyz");
96
97 string_concat(test,test2);
98 cstring = to_cstring(test);
99 printf("%s\n", cstring);
100 free(cstring);
101
102 delete_string(test2);
103 delete_string(test);
104}
105
106int main(int argc, char** argv)
107{
108 test1();
109 test2();
110 sleep_ms(1000);
111 return 0;
112}
113