SELKIELogger  1.0.0
SATests.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2023 Swansea University
3  *
4  * This file is part of the SELKIELogger suite of tools.
5  *
6  * SELKIELogger is free software: you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the Free
8  * Software Foundation, either version 3 of the License, or (at your option)
9  * any later version.
10  *
11  * SELKIELogger is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14  * more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this SELKIELogger product.
18  * If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 #include <stdio.h>
22 #include <stdlib.h>
23 
24 #include "SELKIELoggerBase.h"
25 
46 int main(void) {
47  string *exA = str_new(20, "Testing String Lib.");
48  if (exA == NULL) {
49  // LCOV_EXCL_START
50  fprintf(stderr, "Failed to create example string A\n");
51  return -1;
52  // LCOV_EXCL_STOP
53  }
54 
55  string *exB = str_new(2, "AA");
56  if (exB == NULL) {
57  // LCOV_EXCL_START
58  fprintf(stderr, "Failed to create example string B\n");
59  return -1;
60  // LCOV_EXCL_STOP
61  }
62 
63  string *exC = str_new(0, "");
64  if (exC == NULL) {
65  // LCOV_EXCL_START
66  fprintf(stderr, "Failed to create example string C\n");
67  return -1;
68  // LCOV_EXCL_STOP
69  }
70 
71  string *exD = str_duplicate(exC);
72  if (exD == NULL) {
73  // LCOV_EXCL_START
74  fprintf(stderr, "Failed to create example string D\n");
75  return -1;
76  // LCOV_EXCL_STOP
77  }
78  if (!str_copy(exD, exA)) {
79  // LCOV_EXCL_START
80  fprintf(stderr, "Failed to copy example string A into example string E\n");
81  return -1;
82  // LCOV_EXCL_STOP
83  }
84 
85  string *exE = str_duplicate(exB);
86  if (exE == NULL) {
87  // LCOV_EXCL_START
88  fprintf(stderr, "Failed to create example string E\n");
89  return -1;
90  // LCOV_EXCL_STOP
91  }
92 
93  if (!str_update(exE, 50, "This is an updated test message for the Example E")) {
94  // LCOV_EXCL_START
95  fprintf(stderr, "Failed to update string\n");
96  return -1;
97  // LCOV_EXCL_STOP
98  }
99 
100  strarray *SA = sa_new(6);
101  if (SA == NULL) {
102  // LCOV_EXCL_START
103  fprintf(stderr, "Failed to allocate string array\n");
104  return -1;
105  // LCOV_EXCL_STOP
106  }
107 
108  if (!sa_set_entry(SA, 0, exA)) {
109  // LCOV_EXCL_START
110  fprintf(stderr, "Failed to set SA entry 0\n");
111  return -1;
112  // LCOV_EXCL_STOP
113  }
114 
115  if (!sa_set_entry(SA, 1, exB)) {
116  // LCOV_EXCL_START
117  fprintf(stderr, "Failed to set SA entry 1\n");
118  return -1;
119  // LCOV_EXCL_STOP
120  }
121 
122  if (!sa_set_entry(SA, 2, exC)) {
123  // LCOV_EXCL_START
124  fprintf(stderr, "Failed to set SA entry 2\n");
125  return -1;
126  // LCOV_EXCL_STOP
127  }
128 
129  if (!sa_set_entry(SA, 3, exD)) {
130  // LCOV_EXCL_START
131  fprintf(stderr, "Failed to set SA entry 3\n");
132  return -1;
133  // LCOV_EXCL_STOP
134  }
135 
136  if (!sa_set_entry(SA, 4, exE)) {
137  // LCOV_EXCL_START
138  fprintf(stderr, "Failed to set SA entry 4\n");
139  return -1;
140  // LCOV_EXCL_STOP
141  }
142 
143  if (!sa_create_entry(SA, 5, 35, "Creating string in place on array\n")) {
144  // LCOV_EXCL_START
145  fprintf(stderr, "Failed to set SA entry 5\n");
146  return -1;
147  // LCOV_EXCL_STOP
148  }
149 
150  sa_clear_entry(SA, 3);
151  if (SA->strings[3].data != NULL) {
152  // LCOV_EXCL_START
153  fprintf(stderr, "Failed to clear SA entry 3\n");
154  return -1;
155  // LCOV_EXCL_STOP
156  }
157 
158  str_destroy(exA);
159  str_destroy(exB);
160  str_destroy(exC);
161  str_destroy(exD);
162  str_destroy(exE);
163  free(exA);
164  free(exB);
165  free(exC);
166  free(exD);
167  free(exE);
168 
169  strarray *SA2 = sa_new(0);
170  if (!sa_move(SA2, SA)) {
171  // LCOV_EXCL_START
172  fprintf(stderr, "Unable to move string array contents\n");
173  return -1;
174  // LCOV_EXCL_STOP
175  }
176  sa_destroy(SA);
177  // Should be safe to call twice, so check we can!
178  sa_destroy(SA);
179  // Then test destruction of a malformed array
180  SA->entries = 1;
181  sa_destroy(SA);
182 
183  sa_destroy(SA2);
184  free(SA);
185  free(SA2);
186  fprintf(stdout, "All tests succeeded\n");
187  return 0;
188 }
int main(void)
Definition: SATests.c:46
void sa_destroy(strarray *sa)
Destroy array and contents.
Definition: strarray.c:182
bool str_copy(string *dst, const string *src)
Update an existing string by copying the contents from another.
Definition: strarray.c:253
bool sa_move(strarray *dst, strarray *src)
Move strings from one array to another.
Definition: strarray.c:112
bool str_update(string *str, const size_t len, const char *src)
Update an existing string from a character array of given length.
Definition: strarray.c:294
bool sa_set_entry(strarray *array, const int index, string *str)
Copy a string to a given position in the array.
Definition: strarray.c:131
string * str_duplicate(const string *src)
Allocate a new string containing the contents of another.
Definition: strarray.c:281
string * str_new(const size_t len, const char *ca)
Create new string from character array.
Definition: strarray.c:217
void str_destroy(string *str)
Destroy a string and free its contents.
Definition: strarray.c:323
strarray * sa_new(int entries)
Allocate storage for a new array.
Definition: strarray.c:37
void sa_clear_entry(strarray *array, const int index)
Clear an array entry.
Definition: strarray.c:163
bool sa_create_entry(strarray *array, const int index, const size_t len, const char *src)
Create an string in a given position from a character array and length.
Definition: strarray.c:149
Array of strings.
Definition: strarray.h:43
int entries
Maximum number of strings in array, set when calling sa_new()
Definition: strarray.h:44
string * strings
Simple array of string structures.
Definition: strarray.h:45
char * data
Character array, should be null terminated.
Definition: strarray.h:39