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