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