1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3 * Libmemcached client and server library.
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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
20 * * The names of its contributors may not be used to endorse or
21 * promote products derived from this software without specific prior
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.
38 // We let libmemcached/common.h define config since we are looking at
43 #include <libmemcached/memcached.h>
44 #include <libmemcached/string.hpp>
45 #include <libmemcached/is.h>
47 #include <libtest/test.hpp>
49 #include <tests/string.h>
51 test_return_t
string_static_null(void*)
53 memcached_st
*memc
= memcached_create(NULL
);
54 memcached_string_st string
;
56 memcached_string_st
*string_ptr
= memcached_string_create(memc
, &string
, 0);
57 test_true(string
.options
.is_initialized
);
58 test_true(string_ptr
);
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
);
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
));
75 test_return_t
string_alloc_null(void*)
77 memcached_st
*memc
= memcached_create(NULL
);
79 memcached_string_st
*string
= memcached_string_create(memc
, NULL
, 0);
81 test_true(memcached_is_allocated(string
));
82 test_true(memcached_is_initialized(string
));
83 memcached_string_free(string
);
90 test_return_t
string_alloc_with_size(void*)
92 memcached_st
*memc
= memcached_create(NULL
);
93 memcached_string_st
*string
= memcached_string_create(memc
, NULL
, 1024);
95 test_true(memcached_is_allocated(string
));
96 test_true(memcached_is_initialized(string
));
97 memcached_string_free(string
);
104 test_return_t
string_alloc_with_size_toobig(void*)
106 memcached_st
*memc
= memcached_create(NULL
);
107 memcached_string_st
*string
= memcached_string_create(memc
, NULL
, SIZE_MAX
);
109 memcached_free(memc
);
114 test_return_t
string_alloc_append(void*)
116 memcached_st
*memc
= memcached_create(NULL
);
119 memcached_string_st
*string
;
122 memset(buffer
, 6, BUFSIZ
);
124 string
= memcached_string_create(memc
, NULL
, 100);
126 test_true(memcached_is_allocated(string
));
127 test_true(memcached_is_initialized(string
));
129 for (unsigned int x
= 0; x
< 1024; x
++)
131 memcached_return_t rc
;
132 rc
= memcached_string_append(string
, buffer
, BUFSIZ
);
133 test_true(rc
== MEMCACHED_SUCCESS
);
135 test_true(memcached_is_allocated(string
));
136 memcached_string_free(string
);
138 memcached_free(memc
);
143 test_return_t
string_alloc_append_toobig(void*)
145 memcached_st
*memc
= memcached_create(NULL
);
147 memcached_return_t rc
;
149 memcached_string_st
*string
;
152 memset(buffer
, 6, sizeof(buffer
));
154 string
= memcached_string_create(memc
, NULL
, 100);
156 test_true(memcached_is_allocated(string
));
157 test_true(memcached_is_initialized(string
));
159 for (unsigned int x
= 0; x
< 1024; x
++)
161 rc
= memcached_string_append(string
, buffer
, BUFSIZ
);
162 test_true(rc
== MEMCACHED_SUCCESS
);
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
);
169 memcached_free(memc
);
174 test_return_t
string_alloc_append_multiple(void*)
176 memcached_st
*memc
= memcached_create(NULL
);
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(" ("));
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(")"));
186 memcached_string_free(error_string
);
188 memcached_free(memc
);