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
41 #include "mem_config.h"
43 #include "libmemcached-1.0/memcached.h"
45 #include "libmemcached/string.hpp"
46 #include "libmemcached/is.h"
48 #include "libtest/test.hpp"
50 #include "tests/string.h"
52 test_return_t
string_static_null(void*)
54 memcached_st
*memc
= memcached_create(NULL
);
55 memcached_string_st string
;
57 memcached_string_st
*string_ptr
= memcached_string_create(memc
, &string
, 0);
58 test_true(string
.options
.is_initialized
);
59 test_true(string_ptr
);
61 /* The following two better be the same! */
62 test_false(memcached_is_allocated(string_ptr
));
63 test_false(memcached_is_allocated(&string
));
64 test_true(&string
== string_ptr
);
66 test_true(string
.options
.is_initialized
);
67 test_true(memcached_is_initialized(&string
));
68 memcached_string_free(&string
);
69 test_false(memcached_is_initialized(&string
));
76 test_return_t
string_alloc_null(void*)
78 memcached_st
*memc
= memcached_create(NULL
);
80 memcached_string_st
*string
= memcached_string_create(memc
, NULL
, 0);
82 test_true(memcached_is_allocated(string
));
83 test_true(memcached_is_initialized(string
));
84 memcached_string_free(string
);
91 test_return_t
string_alloc_with_size(void*)
93 memcached_st
*memc
= memcached_create(NULL
);
94 memcached_string_st
*string
= memcached_string_create(memc
, NULL
, 1024);
96 test_true(memcached_is_allocated(string
));
97 test_true(memcached_is_initialized(string
));
98 memcached_string_free(string
);
100 memcached_free(memc
);
105 test_return_t
string_alloc_with_size_toobig(void*)
107 memcached_st
*memc
= memcached_create(NULL
);
108 memcached_string_st
*string
= memcached_string_create(memc
, NULL
, SIZE_MAX
);
110 memcached_free(memc
);
115 test_return_t
string_alloc_append(void*)
117 memcached_st
*memc
= memcached_create(NULL
);
120 memcached_string_st
*string
;
123 memset(buffer
, 6, BUFSIZ
);
125 string
= memcached_string_create(memc
, NULL
, 100);
127 test_true(memcached_is_allocated(string
));
128 test_true(memcached_is_initialized(string
));
130 for (unsigned int x
= 0; x
< 1024; x
++)
132 memcached_return_t rc
;
133 rc
= memcached_string_append(string
, buffer
, BUFSIZ
);
134 test_true(rc
== MEMCACHED_SUCCESS
);
136 test_true(memcached_is_allocated(string
));
137 memcached_string_free(string
);
139 memcached_free(memc
);
144 test_return_t
string_alloc_append_toobig(void*)
146 memcached_st
*memc
= memcached_create(NULL
);
148 memcached_return_t rc
;
150 memcached_string_st
*string
;
153 memset(buffer
, 6, sizeof(buffer
));
155 string
= memcached_string_create(memc
, NULL
, 100);
157 test_true(memcached_is_allocated(string
));
158 test_true(memcached_is_initialized(string
));
160 for (unsigned int x
= 0; x
< 1024; x
++)
162 rc
= memcached_string_append(string
, buffer
, BUFSIZ
);
163 test_true(rc
== MEMCACHED_SUCCESS
);
165 rc
= memcached_string_append(string
, buffer
, SIZE_MAX
);
166 test_true(rc
== MEMCACHED_MEMORY_ALLOCATION_FAILURE
);
167 test_true(memcached_is_allocated(string
));
168 memcached_string_free(string
);
170 memcached_free(memc
);
175 test_return_t
string_alloc_append_multiple(void*)
177 memcached_st
*memc
= memcached_create(NULL
);
179 memcached_string_st
*error_string
= memcached_string_create(memc
, NULL
, 1024);
180 memcached_string_append(error_string
, test_literal_param("Error occured while parsing: "));
181 memcached_string_append(error_string
, test_string_make_from_cstr("jog the strlen() method"));
182 memcached_string_append(error_string
, test_literal_param(" ("));
184 memcached_string_append(error_string
, test_string_make_from_cstr(memcached_strerror(NULL
, MEMCACHED_SUCCESS
)));
185 memcached_string_append(error_string
, test_literal_param(")"));
187 memcached_string_free(error_string
);
189 memcached_free(memc
);