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