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/is.h>
46 #include <libtest/test.hpp>
48 #include <tests/string.h>
50 test_return_t
string_static_null(void*)
52 memcached_st
*memc
= memcached_create(NULL
);
53 memcached_string_st string
;
55 memcached_string_st
*string_ptr
= memcached_string_create(memc
, &string
, 0);
56 test_true(string
.options
.is_initialized
);
57 test_true(string_ptr
);
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
);
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
));
74 test_return_t
string_alloc_null(void*)
76 memcached_st
*memc
= memcached_create(NULL
);
78 memcached_string_st
*string
= memcached_string_create(memc
, NULL
, 0);
80 test_true(memcached_is_allocated(string
));
81 test_true(memcached_is_initialized(string
));
82 memcached_string_free(string
);
89 test_return_t
string_alloc_with_size(void*)
91 memcached_st
*memc
= memcached_create(NULL
);
92 memcached_string_st
*string
= memcached_string_create(memc
, NULL
, 1024);
94 test_true(memcached_is_allocated(string
));
95 test_true(memcached_is_initialized(string
));
96 memcached_string_free(string
);
103 test_return_t
string_alloc_with_size_toobig(void*)
105 memcached_st
*memc
= memcached_create(NULL
);
106 memcached_string_st
*string
= memcached_string_create(memc
, NULL
, SIZE_MAX
);
108 memcached_free(memc
);
113 test_return_t
string_alloc_append(void*)
115 memcached_st
*memc
= memcached_create(NULL
);
118 memcached_string_st
*string
;
121 memset(buffer
, 6, BUFSIZ
);
123 string
= memcached_string_create(memc
, NULL
, 100);
125 test_true(memcached_is_allocated(string
));
126 test_true(memcached_is_initialized(string
));
128 for (unsigned int x
= 0; x
< 1024; x
++)
130 memcached_return_t rc
;
131 rc
= memcached_string_append(string
, buffer
, BUFSIZ
);
132 test_true(rc
== MEMCACHED_SUCCESS
);
134 test_true(memcached_is_allocated(string
));
135 memcached_string_free(string
);
137 memcached_free(memc
);
142 test_return_t
string_alloc_append_toobig(void*)
144 memcached_st
*memc
= memcached_create(NULL
);
146 memcached_return_t rc
;
148 memcached_string_st
*string
;
151 memset(buffer
, 6, sizeof(buffer
));
153 string
= memcached_string_create(memc
, NULL
, 100);
155 test_true(memcached_is_allocated(string
));
156 test_true(memcached_is_initialized(string
));
158 for (unsigned int x
= 0; x
< 1024; x
++)
160 rc
= memcached_string_append(string
, buffer
, BUFSIZ
);
161 test_true(rc
== MEMCACHED_SUCCESS
);
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
);
168 memcached_free(memc
);
173 test_return_t
string_alloc_append_multiple(void*)
175 memcached_st
*memc
= memcached_create(NULL
);
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(" ("));
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(")"));
185 memcached_string_free(error_string
);
187 memcached_free(memc
);