2 * Copyright (C) 2010 Brian Aker
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
11 #define __STDC_LIMIT_MACROS
14 #include <gtest/gtest.h>
16 #include <libmemcached/common.h>
18 TEST(memcached_string_st
, memcached_create_static
)
20 memcached_string_st string
;
21 memcached_string_st
*string_ptr
;
23 memcached_st
*memc
= memcached_create(NULL
);
24 string_ptr
= memcached_string_create(memc
, &string
, 0);
25 ASSERT_TRUE(string
.options
.is_initialized
);
26 ASSERT_TRUE(string_ptr
);
28 /* The following two better be the same! */
29 ASSERT_FALSE(memcached_is_allocated(string_ptr
));
30 ASSERT_FALSE(memcached_is_allocated(&string
));
31 EXPECT_EQ(&string
, string_ptr
);
33 ASSERT_TRUE(string
.options
.is_initialized
);
34 ASSERT_TRUE(memcached_is_initialized(&string
));
35 memcached_string_free(&string
);
36 ASSERT_FALSE(memcached_is_initialized(&string
));
41 TEST(memcached_string_st
, memcached_create_null
)
43 memcached_string_st
*string
;
44 memcached_st
*memc
= memcached_create(NULL
);
46 string
= memcached_string_create(memc
, NULL
, 0);
48 ASSERT_TRUE(memcached_is_allocated(string
));
49 ASSERT_TRUE(memcached_is_initialized(string
));
50 memcached_string_free(string
);
55 TEST(memcached_string_st
, string_alloc_with_size
)
57 memcached_string_st
*string
;
58 memcached_st
*memc
= memcached_create(NULL
);
60 string
= memcached_string_create(memc
, NULL
, 1024);
62 ASSERT_TRUE(memcached_is_allocated(string
));
63 ASSERT_TRUE(memcached_is_initialized(string
));
64 memcached_string_free(string
);
69 TEST(memcached_string_st
, string_alloc_with_size_toobig
)
71 memcached_st
*memc
= memcached_create(NULL
);
72 memcached_string_st
*string
;
74 string
= memcached_string_create(memc
, NULL
, SIZE_MAX
);
80 TEST(memcached_string_st
, string_alloc_append
)
82 char buffer
[SMALL_STRING_LEN
];
83 memcached_string_st
*string
;
85 memcached_st
*memc
= memcached_create(NULL
);
88 memset(buffer
, 6, SMALL_STRING_LEN
);
90 string
= memcached_string_create(memc
, NULL
, 100);
92 ASSERT_TRUE(memcached_is_allocated(string
));
93 ASSERT_TRUE(memcached_is_initialized(string
));
95 for (uint32_t x
= 0; x
< 1024; x
++)
97 memcached_return_t rc
;
98 rc
= memcached_string_append(string
, buffer
, SMALL_STRING_LEN
);
99 EXPECT_EQ(rc
, MEMCACHED_SUCCESS
);
101 ASSERT_TRUE(memcached_is_allocated(string
));
102 memcached_string_free(string
);
104 memcached_free(memc
);
107 TEST(memcached_string_st
, string_alloc_append_toobig
)
109 memcached_return_t rc
;
110 char buffer
[SMALL_STRING_LEN
];
111 memcached_string_st
*string
;
113 memcached_st
*memc
= memcached_create(NULL
);
116 memset(buffer
, 6, SMALL_STRING_LEN
);
118 string
= memcached_string_create(memc
, NULL
, 100);
120 ASSERT_TRUE(memcached_is_allocated(string
));
121 ASSERT_TRUE(memcached_is_initialized(string
));
123 for (uint32_t x
= 0; x
< 1024; x
++)
125 rc
= memcached_string_append(string
, buffer
, SMALL_STRING_LEN
);
126 EXPECT_EQ(rc
, MEMCACHED_SUCCESS
);
128 rc
= memcached_string_append(string
, buffer
, SIZE_MAX
);
129 EXPECT_EQ(rc
, MEMCACHED_MEMORY_ALLOCATION_FAILURE
);
130 ASSERT_TRUE(memcached_is_allocated(string
));
131 memcached_string_free(string
);
133 memcached_free(memc
);