bb90e94b974adce3feacdb45ec7f6d295291c396
[awesomized/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 <config.h>
42
43 #include <libmemcached/memcached.h>
44 #include <libmemcached/string.hpp>
45 #include <libmemcached/is.h>
46
47 #include <libtest/test.hpp>
48
49 #include <tests/string.h>
50
51 test_return_t string_static_null(void*)
52 {
53 memcached_st *memc= memcached_create(NULL);
54 memcached_string_st string;
55
56 memcached_string_st *string_ptr= memcached_string_create(memc, &string, 0);
57 test_true(string.options.is_initialized);
58 test_true(string_ptr);
59
60 /* The following two better be the same! */
61 test_false(memcached_is_allocated(string_ptr));
62 test_false(memcached_is_allocated(&string));
63 test_true(&string == string_ptr);
64
65 test_true(string.options.is_initialized);
66 test_true(memcached_is_initialized(&string));
67 memcached_string_free(&string);
68 test_false(memcached_is_initialized(&string));
69
70 memcached_free(memc);
71
72 return TEST_SUCCESS;
73 }
74
75 test_return_t string_alloc_null(void*)
76 {
77 memcached_st *memc= memcached_create(NULL);
78
79 memcached_string_st *string= memcached_string_create(memc, NULL, 0);
80 test_true(string);
81 test_true(memcached_is_allocated(string));
82 test_true(memcached_is_initialized(string));
83 memcached_string_free(string);
84
85 memcached_free(memc);
86
87 return TEST_SUCCESS;
88 }
89
90 test_return_t string_alloc_with_size(void*)
91 {
92 memcached_st *memc= memcached_create(NULL);
93 memcached_string_st *string= memcached_string_create(memc, NULL, 1024);
94 test_true(string);
95 test_true(memcached_is_allocated(string));
96 test_true(memcached_is_initialized(string));
97 memcached_string_free(string);
98
99 memcached_free(memc);
100
101 return TEST_SUCCESS;
102 }
103
104 test_return_t string_alloc_with_size_toobig(void*)
105 {
106 memcached_st *memc= memcached_create(NULL);
107 memcached_string_st *string= memcached_string_create(memc, NULL, SIZE_MAX);
108 test_zero(string);
109 memcached_free(memc);
110
111 return TEST_SUCCESS;
112 }
113
114 test_return_t string_alloc_append(void*)
115 {
116 memcached_st *memc= memcached_create(NULL);
117
118 char buffer[BUFSIZ];
119 memcached_string_st *string;
120
121 /* Ring the bell! */
122 memset(buffer, 6, BUFSIZ);
123
124 string= memcached_string_create(memc, NULL, 100);
125 test_true(string);
126 test_true(memcached_is_allocated(string));
127 test_true(memcached_is_initialized(string));
128
129 for (unsigned int x= 0; x < 1024; x++)
130 {
131 memcached_return_t rc;
132 rc= memcached_string_append(string, buffer, BUFSIZ);
133 test_true(rc == MEMCACHED_SUCCESS);
134 }
135 test_true(memcached_is_allocated(string));
136 memcached_string_free(string);
137
138 memcached_free(memc);
139
140 return TEST_SUCCESS;
141 }
142
143 test_return_t string_alloc_append_toobig(void*)
144 {
145 memcached_st *memc= memcached_create(NULL);
146
147 memcached_return_t rc;
148 char buffer[BUFSIZ];
149 memcached_string_st *string;
150
151 /* Ring the bell! */
152 memset(buffer, 6, sizeof(buffer));
153
154 string= memcached_string_create(memc, NULL, 100);
155 test_true(string);
156 test_true(memcached_is_allocated(string));
157 test_true(memcached_is_initialized(string));
158
159 for (unsigned int x= 0; x < 1024; x++)
160 {
161 rc= memcached_string_append(string, buffer, BUFSIZ);
162 test_true(rc == MEMCACHED_SUCCESS);
163 }
164 rc= memcached_string_append(string, buffer, SIZE_MAX);
165 test_true(rc == MEMCACHED_MEMORY_ALLOCATION_FAILURE);
166 test_true(memcached_is_allocated(string));
167 memcached_string_free(string);
168
169 memcached_free(memc);
170
171 return TEST_SUCCESS;
172 }
173
174 test_return_t string_alloc_append_multiple(void*)
175 {
176 memcached_st *memc= memcached_create(NULL);
177
178 memcached_string_st *error_string= memcached_string_create(memc, NULL, 1024);
179 memcached_string_append(error_string, test_literal_param("Error occured while parsing: "));
180 memcached_string_append(error_string, test_string_make_from_cstr("jog the strlen() method"));
181 memcached_string_append(error_string, test_literal_param(" ("));
182
183 memcached_string_append(error_string, test_string_make_from_cstr(memcached_strerror(NULL, MEMCACHED_SUCCESS)));
184 memcached_string_append(error_string, test_literal_param(")"));
185
186 memcached_string_free(error_string);
187
188 memcached_free(memc);
189
190 return TEST_SUCCESS;
191 }