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 = new_string();
69 String* test2;
70 char* cstring;
71 string_append(test, 'a');
72 string_append(test, 'b');
73 string_append(test, 'c');
74 string_append(test, 'd');
75 printf("%ld\n", test->length);
76
77 cstring = to_cstring(test);
78 printf("%s\n", cstring);
79 free(cstring);
80
81 string_remove(test, 3);
82 cstring = to_cstring(test);
83 printf("%s\n", cstring);
84 free(cstring);
85 printf("%ld\n", test->length);
86
87 string_insert(test, 0,'a');
88 cstring = to_cstring(test);
89 printf("%s\n", cstring);
90 free(cstring);
91
92 test2 = from_cstring("xyz");
93
94 string_concat(test,test2);
95 cstring = to_cstring(test);
96 printf("%s\n", cstring);
97 free(cstring);
98
99 delete_string(test2);
100 delete_string(test);
101}
102
103int main(int argc, char** argv)
104{
105 test1();
106 test2();
107 sleep_ms(1000);
108 return 0;
109}
110