-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchar.c
87 lines (70 loc) · 2.35 KB
/
char.c
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
// NOTE: The location of this include might differ in your code depending on location
// For example, it could be: #include "caught.h"
#include "../src/caught.h"
#include <string.h>
TEST("char - strstr")
{
EXPECT_CHAR('a', ==, *strchr("what a day", 'a'));
EXPECT_CHAR('d', ==, *strchr("what a day", 'd'));
}
TEST("char - basic")
{
EXPECT_CHAR('a', ==, 'a');
EXPECT_CHAR('a', !=, 'z');
}
TEST("char - inequalities")
{
EXPECT_CHAR('B', <, 'c');
EXPECT_CHAR('a', <=, 'z');
EXPECT_CHAR('z', <=, 'z');
EXPECT_CHAR('a', >, 'W');
EXPECT_CHAR('c', >=, 'b');
EXPECT_CHAR('c', >=, 'c');
}
TEST("char - ptrs")
{
char array[] = {'a', 'c', 'b', 'd'};
char array2[] = {'a', 'c', 'b', 'd'};
EXPECT_CHAR_PTR(array, ==, array2);
EXPECT_CHAR_PTR(array, !=, array + 1);
EXPECT_CHAR_PTR(NULL, ==, NULL);
EXPECT_CHAR_PTR(NULL, !=, array);
EXPECT_CHAR_PTR(array + 2, <, array + 1);
EXPECT_CHAR_PTR(array + 2, <=, array + 1);
EXPECT_CHAR_PTR(array, <=, array2);
EXPECT_CHAR_PTR(array + 1, >, array);
EXPECT_CHAR_PTR(array + 1, >=, array);
EXPECT_CHAR_PTR(array, >=, array);
}
TEST("char - arrays")
{
char array[] = {'a', 'b', 'c'};
char array2[] = {'a', 'b', 'c'};
char array3[] = {'b', 'c', 'd'};
EXPECT_CHAR_ARRAY(array, ==, array2, 3);
EXPECT_CHAR_ARRAY(array, >=, array2, 3);
EXPECT_CHAR_ARRAY(array, <=, array2, 3);
array[2] = 'w';
EXPECT_CHAR_ARRAY(array, !=, array2, 3);
EXPECT_CHAR_ARRAY(array, ==, array2, 2);
EXPECT_CHAR_ARRAY(array2, <, array3, 3);
EXPECT_CHAR_ARRAY(array3, >, array2, 3);
// Can also use c-strings, though recommended to just use EXPECT_STR methods instead
// only benefit of this is getting explicit output of each character
char string[] = "xyz";
char string2[] = "xyz";
EXPECT_CHAR_ARRAY(string, ==, string2, -1);
string[2] = 'w';
EXPECT_CHAR_ARRAY(string, !=, string2, -1);
}
TEST("char - in")
{
char array[] = {'a', 'b', 'c'};
EXPECT_CHAR_ARRAY_ELEMENT('a', in, array, 3);
EXPECT_CHAR_ARRAY_ELEMENT('b', in, array, 3);
EXPECT_CHAR_ARRAY_ELEMENT('w', not in, array, 3);
EXPECT_CHAR_ARRAY_ELEMENT('c', not in, array, 2);
char *cstr = "axy"; // This works because cstrs are '\0' terminated
EXPECT_CHAR_ARRAY_ELEMENT('a', in, cstr, -1);
EXPECT_CHAR_ARRAY_ELEMENT('z', not in, cstr, -1);
}