1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3 * Gearmand 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 #define BUILDING_LIBMEMCACHED
43 #include <libmemcached/common.h>
45 #include <libtest/test.hpp>
47 #include <libmemcached/error.h>
48 #include <tests/string.h>
50 test_return_t
string_static_null(memcached_st
*memc
)
52 memcached_string_st string
;
53 memcached_string_st
*string_ptr
;
55 string_ptr
= memcached_string_create(memc
, &string
, 0);
56 test_true(string
.options
.is_initialized
== true);
57 test_true(string_ptr
);
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
);
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);
72 test_return_t
string_alloc_null(memcached_st
*memc
)
74 memcached_string_st
*string
;
76 string
= memcached_string_create(memc
, NULL
, 0);
78 test_true(memcached_is_allocated(string
) == true);
79 test_true(memcached_is_initialized(string
) == true);
80 memcached_string_free(string
);
85 test_return_t
string_alloc_with_size(memcached_st
*memc
)
87 memcached_string_st
*string
;
89 string
= memcached_string_create(memc
, NULL
, 1024);
91 test_true(memcached_is_allocated(string
) == true);
92 test_true(memcached_is_initialized(string
) == true);
93 memcached_string_free(string
);
98 test_return_t
string_alloc_with_size_toobig(memcached_st
*memc
)
100 memcached_string_st
*string
;
102 string
= memcached_string_create(memc
, NULL
, SIZE_MAX
);
103 test_true(string
== NULL
);
108 test_return_t
string_alloc_append(memcached_st
*memc
)
111 char buffer
[SMALL_STRING_LEN
];
112 memcached_string_st
*string
;
115 memset(buffer
, 6, SMALL_STRING_LEN
);
117 string
= memcached_string_create(memc
, NULL
, 100);
119 test_true(memcached_is_allocated(string
) == true);
120 test_true(memcached_is_initialized(string
) == true);
122 for (x
= 0; x
< 1024; x
++)
124 memcached_return_t rc
;
125 rc
= memcached_string_append(string
, buffer
, SMALL_STRING_LEN
);
126 test_true(rc
== MEMCACHED_SUCCESS
);
128 test_true(memcached_is_allocated(string
) == true);
129 memcached_string_free(string
);
134 test_return_t
string_alloc_append_toobig(memcached_st
*memc
)
136 memcached_return_t rc
;
137 char buffer
[SMALL_STRING_LEN
];
138 memcached_string_st
*string
;
141 memset(buffer
, 6, sizeof(buffer
));
143 string
= memcached_string_create(memc
, NULL
, 100);
145 test_true(memcached_is_allocated(string
) == true);
146 test_true(memcached_is_initialized(string
) == true);
148 for (unsigned int x
= 0; x
< 1024; x
++)
150 rc
= memcached_string_append(string
, buffer
, SMALL_STRING_LEN
);
151 test_true(rc
== MEMCACHED_SUCCESS
);
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
);
161 test_return_t
string_alloc_append_multiple(memcached_st
*memc
)
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(" ("));
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(")"));
171 memcached_string_free(error_string
);