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