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 #define BUILDING_LIBMEMCACHED
40 #include <libmemcached/common.h>
42 #include <libtest/test.hpp>
44 #include <libmemcached/error.h>
45 #include <tests/string.h>
47 test_return_t
string_static_null(memcached_st
*memc
)
49 memcached_string_st string
;
50 memcached_string_st
*string_ptr
;
52 string_ptr
= memcached_string_create(memc
, &string
, 0);
53 test_true(string
.options
.is_initialized
== true);
54 test_true(string_ptr
);
56 /* The following two better be the same! */
57 test_true(memcached_is_allocated(string_ptr
) == false);
58 test_true(memcached_is_allocated(&string
) == false);
59 test_true(&string
== string_ptr
);
61 test_true(string
.options
.is_initialized
== true);
62 test_true(memcached_is_initialized(&string
) == true);
63 memcached_string_free(&string
);
64 test_true(memcached_is_initialized(&string
) == false);
69 test_return_t
string_alloc_null(memcached_st
*memc
)
71 memcached_string_st
*string
;
73 string
= memcached_string_create(memc
, NULL
, 0);
75 test_true(memcached_is_allocated(string
) == true);
76 test_true(memcached_is_initialized(string
) == true);
77 memcached_string_free(string
);
82 test_return_t
string_alloc_with_size(memcached_st
*memc
)
84 memcached_string_st
*string
;
86 string
= memcached_string_create(memc
, NULL
, 1024);
88 test_true(memcached_is_allocated(string
) == true);
89 test_true(memcached_is_initialized(string
) == true);
90 memcached_string_free(string
);
95 test_return_t
string_alloc_with_size_toobig(memcached_st
*memc
)
97 memcached_string_st
*string
;
99 string
= memcached_string_create(memc
, NULL
, SIZE_MAX
);
100 test_true(string
== NULL
);
105 test_return_t
string_alloc_append(memcached_st
*memc
)
108 char buffer
[SMALL_STRING_LEN
];
109 memcached_string_st
*string
;
112 memset(buffer
, 6, SMALL_STRING_LEN
);
114 string
= memcached_string_create(memc
, NULL
, 100);
116 test_true(memcached_is_allocated(string
) == true);
117 test_true(memcached_is_initialized(string
) == true);
119 for (x
= 0; x
< 1024; x
++)
121 memcached_return_t rc
;
122 rc
= memcached_string_append(string
, buffer
, SMALL_STRING_LEN
);
123 test_true(rc
== MEMCACHED_SUCCESS
);
125 test_true(memcached_is_allocated(string
) == true);
126 memcached_string_free(string
);
131 test_return_t
string_alloc_append_toobig(memcached_st
*memc
)
133 memcached_return_t rc
;
134 char buffer
[SMALL_STRING_LEN
];
135 memcached_string_st
*string
;
138 memset(buffer
, 6, sizeof(buffer
));
140 string
= memcached_string_create(memc
, NULL
, 100);
142 test_true(memcached_is_allocated(string
) == true);
143 test_true(memcached_is_initialized(string
) == true);
145 for (unsigned int x
= 0; x
< 1024; x
++)
147 rc
= memcached_string_append(string
, buffer
, SMALL_STRING_LEN
);
148 test_true(rc
== MEMCACHED_SUCCESS
);
150 rc
= memcached_string_append(string
, buffer
, SIZE_MAX
);
151 test_true(rc
== MEMCACHED_MEMORY_ALLOCATION_FAILURE
);
152 test_true(memcached_is_allocated(string
) == true);
153 memcached_string_free(string
);
158 test_return_t
string_alloc_append_multiple(memcached_st
*memc
)
160 memcached_string_st
*error_string
= memcached_string_create(memc
, NULL
, 1024);
161 memcached_string_append(error_string
, memcached_literal_param("Error occured while parsing: "));
162 memcached_string_append(error_string
, memcached_string_make_from_cstr("jog the strlen() method"));
163 memcached_string_append(error_string
, memcached_literal_param(" ("));
165 memcached_string_append(error_string
, memcached_string_make_from_cstr(memcached_strerror(NULL
, MEMCACHED_SUCCESS
)));
166 memcached_string_append(error_string
, memcached_literal_param(")"));
168 memcached_string_free(error_string
);