fix warning
[m6w6/libmemcached] / tests / libmemcached-1.0 / string.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Libmemcached client and server library.
4 *
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
11 *
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * * Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following disclaimer
17 * in the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * * The names of its contributors may not be used to endorse or
21 * promote products derived from this software without specific prior
22 * written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 *
36 */
37
38 // We let libmemcached/common.h define config since we are looking at
39 // library internals.
40
41 #include "mem_config.h"
42
43 #include "libmemcached-1.0/memcached.h"
44
45 #include "libmemcached/string.hpp"
46 #include "libmemcached/is.h"
47
48 #include "libtest/test.hpp"
49
50 #include "tests/string.h"
51
52 test_return_t string_static_null(void*)
53 {
54 memcached_st *memc= memcached_create(NULL);
55 memcached_string_st string;
56
57 memcached_string_st *string_ptr= memcached_string_create(memc, &string, 0);
58 test_true(string.options.is_initialized);
59 test_true(string_ptr);
60
61 /* The following two better be the same! */
62 test_false(memcached_is_allocated(string_ptr));
63 test_false(memcached_is_allocated(&string));
64 test_true(&string == string_ptr);
65
66 test_true(string.options.is_initialized);
67 test_true(memcached_is_initialized(&string));
68 memcached_string_free(&string);
69 test_false(memcached_is_initialized(&string));
70
71 memcached_free(memc);
72
73 return TEST_SUCCESS;
74 }
75
76 test_return_t string_alloc_null(void*)
77 {
78 memcached_st *memc= memcached_create(NULL);
79
80 memcached_string_st *string= memcached_string_create(memc, NULL, 0);
81 test_true(string);
82 test_true(memcached_is_allocated(string));
83 test_true(memcached_is_initialized(string));
84 memcached_string_free(string);
85
86 memcached_free(memc);
87
88 return TEST_SUCCESS;
89 }
90
91 test_return_t string_alloc_with_size(void*)
92 {
93 memcached_st *memc= memcached_create(NULL);
94 memcached_string_st *string= memcached_string_create(memc, NULL, 1024);
95 test_true(string);
96 test_true(memcached_is_allocated(string));
97 test_true(memcached_is_initialized(string));
98 memcached_string_free(string);
99
100 memcached_free(memc);
101
102 return TEST_SUCCESS;
103 }
104
105 test_return_t string_alloc_with_size_toobig(void*)
106 {
107 memcached_st *memc= memcached_create(NULL);
108 memcached_string_st *string= memcached_string_create(memc, NULL, SIZE_MAX);
109 test_zero(string);
110 memcached_free(memc);
111
112 return TEST_SUCCESS;
113 }
114
115 test_return_t string_alloc_append(void*)
116 {
117 memcached_st *memc= memcached_create(NULL);
118
119 char buffer[BUFSIZ];
120 memcached_string_st *string;
121
122 /* Ring the bell! */
123 memset(buffer, 6, BUFSIZ);
124
125 string= memcached_string_create(memc, NULL, 100);
126 test_true(string);
127 test_true(memcached_is_allocated(string));
128 test_true(memcached_is_initialized(string));
129
130 for (unsigned int x= 0; x < 1024; x++)
131 {
132 memcached_return_t rc;
133 rc= memcached_string_append(string, buffer, BUFSIZ);
134 test_true(rc == MEMCACHED_SUCCESS);
135 }
136 test_true(memcached_is_allocated(string));
137 memcached_string_free(string);
138
139 memcached_free(memc);
140
141 return TEST_SUCCESS;
142 }
143
144 test_return_t string_alloc_append_toobig(void*)
145 {
146 memcached_st *memc= memcached_create(NULL);
147
148 memcached_return_t rc;
149 char buffer[BUFSIZ];
150 memcached_string_st *string;
151
152 /* Ring the bell! */
153 memset(buffer, 6, sizeof(buffer));
154
155 string= memcached_string_create(memc, NULL, 100);
156 test_true(string);
157 test_true(memcached_is_allocated(string));
158 test_true(memcached_is_initialized(string));
159
160 for (unsigned int x= 0; x < 1024; x++)
161 {
162 rc= memcached_string_append(string, buffer, BUFSIZ);
163 test_true(rc == MEMCACHED_SUCCESS);
164 }
165 rc= memcached_string_append(string, buffer, SIZE_MAX);
166 test_true(rc == MEMCACHED_MEMORY_ALLOCATION_FAILURE);
167 test_true(memcached_is_allocated(string));
168 memcached_string_free(string);
169
170 memcached_free(memc);
171
172 return TEST_SUCCESS;
173 }
174
175 test_return_t string_alloc_append_multiple(void*)
176 {
177 memcached_st *memc= memcached_create(NULL);
178
179 memcached_string_st *error_string= memcached_string_create(memc, NULL, 1024);
180 memcached_string_append(error_string, test_literal_param("Error occured while parsing: "));
181 memcached_string_append(error_string, test_string_make_from_cstr("jog the strlen() method"));
182 memcached_string_append(error_string, test_literal_param(" ("));
183
184 memcached_string_append(error_string, test_string_make_from_cstr(memcached_strerror(NULL, MEMCACHED_SUCCESS)));
185 memcached_string_append(error_string, test_literal_param(")"));
186
187 memcached_string_free(error_string);
188
189 memcached_free(memc);
190
191 return TEST_SUCCESS;
192 }