summaryrefslogtreecommitdiff
path: root/app/bin/unittest/shortentest.c
blob: 9288017064961199b42d9dc37a002f941cee6bd3 (plain)
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/** \file stringutiltest.c
* Unit tests for the dxfformat module
*/

#include <malloc.h>
#include <stdarg.h>
#include <stddef.h>
#include <string.h>
#include <stdio.h>
#include <setjmp.h>
#include <cmocka.h>

#include <shortentext.h>

#define RESULTSTRING "This is a test!"


static void NoRemoveBlanks(void **state)
{
	char *result = malloc(strlen(RESULTSTRING) + 20);
	(void)state;

	RemoveFormatChars(RESULTSTRING, result);
	assert_string_equal(result, RESULTSTRING);

	RemoveFormatChars(" " RESULTSTRING, result);
	assert_string_equal(result, " " RESULTSTRING);
}

static void RemoveMultipleBlanks(void **state)
{
	char *result = malloc(strlen(RESULTSTRING) + 20);
	(void)state;

	RemoveFormatChars("This  is a test!", result);
	assert_string_equal(result, RESULTSTRING);

	RemoveFormatChars("This   is a test!", result);
	assert_string_equal(result, RESULTSTRING);
}


static void RemoveTabs(void **state)
{
	char *result = malloc(strlen(RESULTSTRING) + 20);
	(void)state;

	RemoveFormatChars("This \tis\ta test!", result);
	assert_string_equal(result, RESULTSTRING);

	RemoveFormatChars("This\t\tis a\ttest!", result);
	assert_string_equal(result, RESULTSTRING);

	RemoveFormatChars("\tThis is a test!", result);
	assert_string_equal(result, " " RESULTSTRING);
}

static void RemoveCRs(void **state)
{
	char *result = malloc(strlen(RESULTSTRING) + 20);
	(void)state;

	RemoveFormatChars("This\r is a test!", result);
	assert_string_equal(result, RESULTSTRING);

	RemoveFormatChars("This\ris a test!", result);
	assert_string_equal(result, RESULTSTRING);

	RemoveFormatChars("This is a test!\r", result);
	assert_string_equal(result, RESULTSTRING);
}

static void RemoveLFs(void **state)
{
	char *result = malloc(strlen(RESULTSTRING) + 20);
	(void)state;

	RemoveFormatChars("This\n is a test!", result);
	assert_string_equal(result, RESULTSTRING);

	RemoveFormatChars("This\nis a test!", result);
	assert_string_equal(result, RESULTSTRING);

	RemoveFormatChars("\nThis is a test!", result);
	assert_string_equal(result, " " RESULTSTRING);

	RemoveFormatChars("This is a test!\r\n", result);
	assert_string_equal(result, RESULTSTRING);
}

#define LONGSTRING "The strrchr() function in C/C++ locates the last occurrence of a character in a string. It returns a pointer to the last occurrence in the string. The terminating null character is considered part of the C string. ... str : specifies the pointer to the null terminated string to be searched for."

static void NoEllipsizeText(void **state)
{
	char *result = malloc(strlen(RESULTSTRING) + 20);

	(void)state;

	EllipsizeString(RESULTSTRING, result, strlen(RESULTSTRING) + 10);
	assert_string_equal(result, RESULTSTRING);

	EllipsizeString(RESULTSTRING, NULL, strlen(RESULTSTRING) + 10);
	assert_string_equal(result, RESULTSTRING);
}

static void EllipsizeText(void **state)
{
	char *result = malloc(sizeof(LONGSTRING));
	(void)state;

	EllipsizeString(LONGSTRING, result, strlen(LONGSTRING));
	assert_string_equal(result, LONGSTRING);

	EllipsizeString(LONGSTRING, result, 40);
	assert_string_equal(result, "The strrchr() function in C/C++...");

	EllipsizeString(LONGSTRING, result, 23);
	assert_string_equal(result, "The strrchr()...");

	strcpy(result, LONGSTRING);
	EllipsizeString(result, NULL, 23);
	assert_string_equal(result, "The strrchr()...");

}

static void LongText(void **state)
{
	char *result = malloc(sizeof(LONGSTRING));
	(void)state;

	strcpy(result, "abcdefghijklmnopqrstuvwxyz");
	EllipsizeString(result, NULL, 23);
	assert_string_equal(result, "abcdefghijklmnopqrst...");

	EllipsizeString("abcdefghijklmnopqrstuvwxyz", result, 10);
	assert_string_equal(result, "abcdefg...");
}

int main(void)
{
	const struct CMUnitTest tests[] = {
		cmocka_unit_test(NoRemoveBlanks),
		cmocka_unit_test(RemoveMultipleBlanks),
		cmocka_unit_test(RemoveTabs),
		cmocka_unit_test(RemoveCRs),
		cmocka_unit_test(RemoveLFs),
		cmocka_unit_test(NoEllipsizeText),
		cmocka_unit_test(EllipsizeText),
		cmocka_unit_test(LongText),
	};
	return cmocka_run_group_tests(tests, NULL, NULL);
}