Branch data Line data Source code
1 : : #include "test_libtestitall_all.h"
2 : :
3 : : static const char expected_noisy_trim_trailing_eol_stderr_pattern[] =
4 : : "\\A"
5 : : "ERROR: [^\\n]*Memory management; string truncate requires a string descriptor[^\\n]*\n"
6 : : "ERROR: [^\\n]*Memory management; String descriptor is inconsistent during truncate[^\\n]*\n"
7 : : "\\Z";
8 : :
9 : : static memory *noisy_data_text = NULL;
10 : : static memory *noisy_inconsistent_text = NULL;
11 : : static Return noisy_data_text_status = SUCCESS;
12 : : static Return noisy_inconsistent_text_status = SUCCESS;
13 : :
14 : : /**
15 : : * @brief Exercise trim_trailing_eol() failures diagnosed by libmem
16 : : *
17 : : * The helper is invoked through match_function_output() so expected libmem
18 : : * diagnostics stay captured instead of leaking into the common test output
19 : : *
20 : : * @return SUCCESS when both negative calls were exercised
21 : : */
22 : 1 : static Return capture_noisy_trim_trailing_eol_cases(void)
23 : : {
24 : : /* Status returned by this function through deliver()
25 : : Default value assumes successful completion */
26 : 1 : Return status = SUCCESS;
27 : :
28 : 1 : noisy_data_text_status = trim_trailing_eol(noisy_data_text);
29 : 1 : noisy_inconsistent_text_status = trim_trailing_eol(noisy_inconsistent_text);
30 : :
31 : 1 : deliver(status);
32 : : }
33 : :
34 : : /**
35 : : * @brief Apply trim_trailing_eol() to one byte string and check its metadata
36 : : *
37 : : * @param input Initial visible text
38 : : * @param expected Expected visible text after trimming
39 : : *
40 : : * @return Return status code
41 : : */
42 : 6 : static Return check_trim_trailing_eol_case(
43 : : const char *input,
44 : : const char *expected)
45 : : {
46 : : /* Status returned by this function through deliver()
47 : : Default value assumes successful completion */
48 : 6 : Return status = SUCCESS;
49 : 6 : m_create(char,text,MEMORY_STRING);
50 : 6 : size_t original_length = 0U;
51 : :
52 : 6 : run(m_copy_string(text,input));
53 : :
54 : 6 : IF(SUCCESS == status)
55 : : {
56 : 6 : original_length = text->length;
57 : : }
58 : :
59 : 6 : run(trim_trailing_eol(text));
60 : :
61 : 6 : IF(SUCCESS == status)
62 : : {
63 : 6 : ASSERT(0 == strcmp(m_text(text),expected));
64 : 6 : ASSERT(text->string_length == strlen(expected));
65 : 6 : ASSERT(text->length == original_length);
66 : : }
67 : :
68 : 6 : call(m_del(text));
69 : :
70 : 6 : deliver(status);
71 : : }
72 : :
73 : : /**
74 : : * @brief Check trailing-EOL removal through libmem string metadata and truncation
75 : : *
76 : : * Covers byte-string endings, an already reserved string buffer, and inputs
77 : : * that do not satisfy or have corrupted the byte-string descriptor contract
78 : : *
79 : : * @return Return status code
80 : : */
81 : 1 : Return test_libtestitall_0007(void)
82 : : {
83 : 1 : INITTEST;
84 : :
85 : : static const struct {
86 : : const char *input;
87 : : const char *expected;
88 : : } test_cases[] = {
89 : : {"text\n","text"},
90 : : {"text\r","text"},
91 : : {"text\r\n","text"},
92 : : {"text\n\n","text\n"},
93 : : {"text","text"},
94 : : {"",""}
95 : : };
96 : :
97 [ + + ]: 7 : for(size_t i = 0U; i < sizeof(test_cases) / sizeof(test_cases[0]); ++i)
98 : : {
99 : 6 : ASSERT(SUCCESS == check_trim_trailing_eol_case(
100 : : test_cases[i].input,
101 : : test_cases[i].expected));
102 : : }
103 : :
104 : 1 : m_create(char,reserved_text,MEMORY_STRING);
105 : :
106 : 1 : ASSERT(SUCCESS == m_copy_string(reserved_text,"reserved\r\n"));
107 : 1 : ASSERT(SUCCESS == m_resize(reserved_text,64U));
108 : 1 : ASSERT(SUCCESS == trim_trailing_eol(reserved_text));
109 : 1 : ASSERT(0 == strcmp(m_text(reserved_text),"reserved"));
110 : 1 : ASSERT(reserved_text->string_length == strlen("reserved"));
111 : 1 : ASSERT(reserved_text->length == 64U);
112 : :
113 : 1 : call(m_del(reserved_text));
114 : :
115 : 1 : m_create(char,data_text);
116 : 1 : m_create(unsigned short,wide_text,MEMORY_STRING);
117 : :
118 : 1 : char invalid_visible_value = '\0';
119 : 1 : memory inconsistent_text = m_init(char,MEMORY_STRING);
120 : 1 : inconsistent_text.data = &invalid_visible_value;
121 : 1 : inconsistent_text.length = 1U;
122 : 1 : inconsistent_text.actually_allocated_bytes = sizeof(invalid_visible_value);
123 : 1 : inconsistent_text.string_length = 1U;
124 : :
125 : 1 : memory stale_empty_text = m_init(char,MEMORY_STRING);
126 : 1 : stale_empty_text.string_length = 1U;
127 : :
128 : 1 : noisy_data_text = data_text;
129 : 1 : noisy_inconsistent_text = &inconsistent_text;
130 : 1 : noisy_data_text_status = SUCCESS;
131 : 1 : noisy_inconsistent_text_status = SUCCESS;
132 : :
133 : 1 : ASSERT(FAILURE == trim_trailing_eol(NULL));
134 : 1 : ASSERT(FAILURE == trim_trailing_eol(wide_text));
135 : 1 : ASSERT(FAILURE == trim_trailing_eol(&stale_empty_text));
136 : 1 : ASSERT(SUCCESS == match_function_output(
137 : : NULL,
138 : : expected_noisy_trim_trailing_eol_stderr_pattern,
139 : : capture_noisy_trim_trailing_eol_cases));
140 : 1 : ASSERT(FAILURE & noisy_data_text_status);
141 : 1 : ASSERT(FAILURE & noisy_inconsistent_text_status);
142 : :
143 : 1 : call(m_del(wide_text));
144 : 1 : call(m_del(data_text));
145 : :
146 : 1 : RETURN_STATUS;
147 : : }
|