test_basic.c (2207B)
1 #include <cstring.h> 2 3 // Compilation: gcc test_basic.c -lcstring 4 5 int 6 main(int argc, char *argv[]) 7 { 8 cstring s = cstring_create("Hello world"); 9 printf("cstring_create: %s (Len: %ld, Capacity: %ld)\n", s.str, s.len, s.capacity); 10 11 cstring_sort_chars(&s, CSTRING_SORT_ASCENDING, NULL); 12 printf("cstring_sort_chars: %s (Len: %ld, Capacity: %ld)\n", s.str, s.len, s.capacity); 13 14 cstring_assign(&s, "New string"); 15 printf("cstring_assign: %s (Len: %ld, Capacity: %ld)\n", s.str, s.len, s.capacity); 16 17 cstring_append(&s, "Appended text"); 18 printf("cstring_append: %s (Len: %ld, Capacity: %ld)\n", s.str, s.len, s.capacity); 19 20 cstring_prepend(&s, "OK"); 21 printf("cstring_prepend: %s (Len: %ld, Capacity: %ld)\n", s.str, s.len, s.capacity); 22 23 cstring_push_back(&s, 'c'); 24 printf("cstring_push_back: %s (Len: %ld, Capacity: %ld)\n", s.str, s.len, s.capacity); 25 26 cstring_insert(&s, "Inserted text", 4); 27 printf("cstring_insert: %s (Len: %ld, Capacity: %ld)\n", s.str, s.len, s.capacity); 28 29 cstring_pop_back(&s); 30 printf("cstring_pop_back: %s (Len: %ld, Capacity: %ld)\n", s.str, s.len, s.capacity); 31 32 cstring_clear(&s); 33 printf("cstring_clear: %s (Len: %ld, Capacity: %ld)\n", s.str, s.len, s.capacity); 34 35 cstring_assign(&s, "CSTRING"); 36 printf("cstring_assign: %s (Len: %ld, Capacity: %ld)\n", s.str, s.len, s.capacity); 37 printf("cstring_front: %c\n", cstring_front(&s)); 38 printf("cstring_back: %c\n", cstring_back(&s)); 39 40 cstring_replace_char(&s, 3, 'x'); 41 printf("cstring_replace_char: %s (Len: %ld, Capacity: %ld)\n", s.str, s.len, s.capacity); 42 43 cstring_replace_str(&s, "hell", 0, strlen("hell")); 44 printf("cstring_replace_str: %s (Len: %ld, Capacity: %ld)\n", s.str, s.len, s.capacity); 45 46 cstring_erase(&s, 1, 4); 47 printf("cstring_erase: %s (Len: %ld, Capacity: %ld)\n", s.str, s.len, s.capacity); 48 49 cstring_assign(&s, "hello aaaa hello abbb helo hello"); 50 cstring_erase_all_matching(&s, "hello"); 51 printf("cstring_erase_all_matching: %s (Len: %ld, Capacity: %ld)\n", s.str, s.len, s.capacity); 52 53 cstring_trim(&s, " "); 54 printf("cstring_trim: %s (Len: %ld, Capacity: %ld)\n", s.str, s.len, s.capacity); 55 56 cstring_delete(&s); 57 if (cstring_empty(&s)) 58 puts("cstring_delete: Deleted string."); 59 60 return (0); 61 }