File: split_test.c

package info (click to toggle)
orcania 2.3.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 388 kB
  • sloc: ansic: 2,352; makefile: 199; sh: 29
file content (108 lines) | stat: -rw-r--r-- 3,291 bytes parent folder | download | duplicates (2)
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
/* Public domain, no copyright. Use at your own risk. */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <time.h>

#include <check.h>
#include "orcania.h"

START_TEST(test_split_string)
{
  char * str = "ab,cd,ef,gghhii,jkl",  * str_2 = "ab, cd,ef,gghhii, jkl", * separator = ",", * long_separator = ", ", ** splitted;
  int nb_elt;
  nb_elt = split_string(str, separator, &splitted);
  ck_assert_ptr_ne(splitted, NULL);
  ck_assert_int_eq(nb_elt, 5);
  ck_assert_int_eq(split_string(str, "nope", NULL), 1);
  ck_assert_str_eq(splitted[0], "ab");
  free_string_array(splitted);
  
  nb_elt = split_string(str_2, long_separator, &splitted);
  ck_assert_ptr_ne(splitted, NULL);
  ck_assert_int_eq(nb_elt, 3);
  free_string_array(splitted);
  
  nb_elt = split_string(str_2, "yolo", &splitted);
  ck_assert_ptr_ne(splitted, NULL);
  ck_assert_int_eq(nb_elt, 1);
  free_string_array(splitted);
  
  nb_elt = split_string("john", "and-yolo", &splitted);
  ck_assert_ptr_ne(splitted, NULL);
  ck_assert_int_eq(nb_elt, 1);
  free_string_array(splitted);
}
END_TEST

START_TEST(test_string_array_has_value)
{
  char * str = "ab,cd,ef,gghhii,jkl", * separator = ",", ** splitted;
  ck_assert_int_eq(split_string(str, separator, &splitted), 5);
  ck_assert_int_eq(string_array_has_value((const char **)splitted, "ab"), 1);
  ck_assert_int_eq(string_array_has_value((const char **)splitted, "nope"), 0);
  ck_assert_int_eq(string_array_has_value((const char **)splitted, NULL), 0);
  ck_assert_int_eq(string_array_has_value(NULL, "ab"), 0);
  free_string_array(splitted);
}
END_TEST

START_TEST(test_string_array_join)
{
  char * str = "ab,cd,ef,gghhii,jkl", * separator = ",", * join, ** splitted;
  ck_assert_int_eq(split_string(str, separator, &splitted), 5);
  join = string_array_join((const char **)splitted, " ");
  ck_assert_str_eq(join, "ab cd ef gghhii jkl");
  ck_assert_ptr_eq(string_array_join(NULL, " "), NULL);
  ck_assert_ptr_eq(string_array_join((const char **)splitted, NULL), NULL);
  ck_assert_ptr_eq(string_array_join(NULL, NULL), NULL);
  free_string_array(splitted);
  o_free(join);
}
END_TEST

START_TEST(test_string_array_size)
{
  char * str = "ab,cd,ef,gghhii,jkl", * separator = ",", ** splitted, ** empty_array = {NULL};
  ck_assert_int_eq(split_string(str, separator, &splitted), 5);
  ck_assert_int_eq(string_array_size(splitted), 5);
  ck_assert_int_eq(string_array_size(NULL), 0);
  ck_assert_int_eq(string_array_size(empty_array), 0);
  free_string_array(splitted);
}
END_TEST

static Suite *orcania_suite(void)
{
	Suite *s;
	TCase *tc_core;

	s = suite_create("Orcania tests split string functions");
	tc_core = tcase_create("test_orcania_split");
	tcase_add_test(tc_core, test_split_string);
	tcase_add_test(tc_core, test_string_array_has_value);
	tcase_add_test(tc_core, test_string_array_join);
	tcase_add_test(tc_core, test_string_array_size);
	tcase_set_timeout(tc_core, 30);
	suite_add_tcase(s, tc_core);

	return s;
}

int main(int argc, char *argv[])
{
  int number_failed;
  Suite *s;
  SRunner *sr;
  
  s = orcania_suite();
  sr = srunner_create(s);

  srunner_run_all(sr, CK_VERBOSE);
  number_failed = srunner_ntests_failed(sr);
  srunner_free(sr);
  
	return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}