Crossfire Server, Trunk
check_stringbuffer.cpp
Go to the documentation of this file.
1 /*
2  * CrossFire, A Multiplayer game for X-windows
3  *
4  * Copyright (C) 2022 the Crossfire Development Team
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  *
20  * The authors can be reached via e-mail at crossfire-devel@real-time.com
21  */
22 
23 /*
24  * This is the unit tests file for common/stringbuffer.c
25  */
26 
27 #include <stdlib.h>
28 #include <string.h>
29 #include <check.h>
30 #include "global.h"
31 #include "sproto.h"
32 
33 static void setup(void) {
35 }
36 
37 static void teardown(void) {
38  /* nothing to do */
39 }
40 
41 static void check_sb_equals(StringBuffer *sb, const char *value) {
42  char *end = stringbuffer_finish(sb);
43  ck_assert_str_eq(end, value);
44  free(end);
45 }
46 
47 START_TEST(test_empty) {
49  check_sb_equals(sb, "");
50 }
51 END_TEST
52 
53 START_TEST(test_shared) {
55  stringbuffer_append_string(sb, "test");
57  ck_assert_str_eq(end, "test");
58  ck_assert_ptr_ne(find_string("test"), NULL);
59  free_string(end);
60 }
61 END_TEST
62 
63 START_TEST(test_append_int64_zero) {
66  check_sb_equals(sb, "0");
67 }
68 END_TEST
69 
70 START_TEST(test_append_int64_positive) {
72  stringbuffer_append_int64(sb, 75387);
73  check_sb_equals(sb, "75387");
74 }
75 END_TEST
76 
77 START_TEST(test_append_int64_negative) {
79  stringbuffer_append_int64(sb, -778185);
80  check_sb_equals(sb, "-778185");
81 }
82 END_TEST
83 
84 START_TEST(test_append_int64_max) {
86  stringbuffer_append_int64(sb, INT64_MAX);
87  check_sb_equals(sb, "9223372036854775807");
88 }
89 END_TEST
90 
91 START_TEST(test_append_int64_min) {
93  stringbuffer_append_int64(sb, INT64_MIN);
94  check_sb_equals(sb, "-9223372036854775807");
95 }
96 END_TEST
97 
98 static Suite *stringbuffer_suite(void) {
99  Suite *s = suite_create("stringbuffer");
100  TCase *tc_core = tcase_create("Core");
101 
102  /*setup and teardown will be called before each test in testcase 'tc_core' */
103  tcase_add_checked_fixture(tc_core, setup, teardown);
104 
105  suite_add_tcase(s, tc_core);
106  tcase_add_test(tc_core, test_empty);
107  tcase_add_test(tc_core, test_shared);
108  tcase_add_test(tc_core, test_append_int64_zero);
109  tcase_add_test(tc_core, test_append_int64_positive);
110  tcase_add_test(tc_core, test_append_int64_negative);
111  tcase_add_test(tc_core, test_append_int64_max);
112  tcase_add_test(tc_core, test_append_int64_min);
113 
114  return s;
115 }
116 
117 int main(void) {
118  int nf;
119  Suite *s = stringbuffer_suite();
120  SRunner *sr = srunner_create(s);
121 
122  srunner_set_xml(sr, LOGDIR "/unit/common/stringbuffer.xml");
123  srunner_set_log(sr, LOGDIR "/unit/common/stringbuffer.out");
124  srunner_run_all(sr, CK_ENV); /*verbosity from env variable*/
125  nf = srunner_ntests_failed(sr);
126  srunner_free(sr);
127  return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
128 }
global.h
stringbuffer_new
StringBuffer * stringbuffer_new(void)
Definition: stringbuffer.cpp:57
main
int main(void)
Definition: check_stringbuffer.cpp:117
stringbuffer_append_int64
void stringbuffer_append_int64(StringBuffer *sb, int64_t x)
Definition: stringbuffer.cpp:111
stringbuffer_finish
char * stringbuffer_finish(StringBuffer *sb)
Definition: stringbuffer.cpp:76
check_sb_equals
static void check_sb_equals(StringBuffer *sb, const char *value)
Definition: check_stringbuffer.cpp:41
stringbuffer_finish_shared
sstring stringbuffer_finish_shared(StringBuffer *sb)
Definition: stringbuffer.cpp:85
teardown
static void teardown(void)
Definition: check_stringbuffer.cpp:37
find_string
sstring find_string(const char *str)
Definition: shstr.cpp:236
START_TEST
START_TEST(test_empty)
Definition: check_stringbuffer.cpp:47
setup
static void setup(void)
Definition: check_stringbuffer.cpp:33
sproto.h
stringbuffer_append_string
void stringbuffer_append_string(StringBuffer *sb, const char *str)
Definition: stringbuffer.cpp:95
free_string
void free_string(sstring str)
Definition: shstr.cpp:280
StringBuffer
Definition: stringbuffer.cpp:25
init_hash_table
void init_hash_table(void)
Definition: shstr.cpp:55
sstring
const typedef char * sstring
Definition: sstring.h:2
autojail.value
value
Definition: autojail.py:6
stringbuffer_suite
static END_TEST Suite * stringbuffer_suite(void)
Definition: check_stringbuffer.cpp:98