Fixed autoconf macros for cross compile
[awesomized/libmemcached] / unittests / strings.cc
1 /* libMemcached
2 * Copyright (C) 2010 Brian Aker
3 * All rights reserved.
4 *
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
7 */
8
9 #include "config.h"
10
11 #define __STDC_LIMIT_MACROS
12 #include <stdint.h>
13
14 #include <gtest/gtest.h>
15
16 #include <libmemcached/common.h>
17
18 TEST(memcached_string_st, memcached_create_static)
19 {
20 memcached_string_st string;
21 memcached_string_st *string_ptr;
22
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);
27
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);
32
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));
37
38 memcached_free(memc);
39 }
40
41 TEST(memcached_string_st, memcached_create_null)
42 {
43 memcached_string_st *string;
44 memcached_st *memc= memcached_create(NULL);
45
46 string= memcached_string_create(memc, NULL, 0);
47 ASSERT_TRUE(string);
48 ASSERT_TRUE(memcached_is_allocated(string));
49 ASSERT_TRUE(memcached_is_initialized(string));
50 memcached_string_free(string);
51
52 memcached_free(memc);
53 }
54
55 TEST(memcached_string_st, string_alloc_with_size)
56 {
57 memcached_string_st *string;
58 memcached_st *memc= memcached_create(NULL);
59
60 string= memcached_string_create(memc, NULL, 1024);
61 ASSERT_TRUE(string);
62 ASSERT_TRUE(memcached_is_allocated(string));
63 ASSERT_TRUE(memcached_is_initialized(string));
64 memcached_string_free(string);
65
66 memcached_free(memc);
67 }
68
69 TEST(memcached_string_st, string_alloc_with_size_toobig)
70 {
71 memcached_st *memc= memcached_create(NULL);
72 memcached_string_st *string;
73
74 string= memcached_string_create(memc, NULL, SIZE_MAX);
75 ASSERT_FALSE(string);
76
77 memcached_free(memc);
78 }
79
80 TEST(memcached_string_st, string_alloc_append)
81 {
82 char buffer[SMALL_STRING_LEN];
83 memcached_string_st *string;
84
85 memcached_st *memc= memcached_create(NULL);
86
87 /* Ring the bell! */
88 memset(buffer, 6, SMALL_STRING_LEN);
89
90 string= memcached_string_create(memc, NULL, 100);
91 ASSERT_TRUE(string);
92 ASSERT_TRUE(memcached_is_allocated(string));
93 ASSERT_TRUE(memcached_is_initialized(string));
94
95 for (uint32_t x= 0; x < 1024; x++)
96 {
97 memcached_return_t rc;
98 rc= memcached_string_append(string, buffer, SMALL_STRING_LEN);
99 EXPECT_EQ(rc, MEMCACHED_SUCCESS);
100 }
101 ASSERT_TRUE(memcached_is_allocated(string));
102 memcached_string_free(string);
103
104 memcached_free(memc);
105 }
106
107 TEST(memcached_string_st, string_alloc_append_toobig)
108 {
109 memcached_return_t rc;
110 char buffer[SMALL_STRING_LEN];
111 memcached_string_st *string;
112
113 memcached_st *memc= memcached_create(NULL);
114
115 /* Ring the bell! */
116 memset(buffer, 6, SMALL_STRING_LEN);
117
118 string= memcached_string_create(memc, NULL, 100);
119 ASSERT_TRUE(string);
120 ASSERT_TRUE(memcached_is_allocated(string));
121 ASSERT_TRUE(memcached_is_initialized(string));
122
123 for (uint32_t x= 0; x < 1024; x++)
124 {
125 rc= memcached_string_append(string, buffer, SMALL_STRING_LEN);
126 EXPECT_EQ(rc, MEMCACHED_SUCCESS);
127 }
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);
132
133 memcached_free(memc);
134 }