Merge lp:~tangent-org/libmemcached/1.0-build/ Build: jenkins-Libmemcached-187
[m6w6/libmemcached] / tests / keys.hpp
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Libmemcached library
4 *
5 * Copyright (C) 2012 Data Differential, http://datadifferential.com/
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following disclaimer
16 * in the documentation and/or other materials provided with the
17 * distribution.
18 *
19 * * The names of its contributors may not be used to endorse or
20 * promote products derived from this software without specific prior
21 * written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *
35 */
36
37
38 #if defined(HAVE_UUID_UUID_H) && HAVE_UUID_UUID_H
39 #include <uuid/uuid.h>
40 #endif
41
42 struct keys_st {
43 public:
44 keys_st(size_t arg)
45 {
46 init(arg, UUID_STRING_MAXLENGTH);
47 }
48
49 keys_st(size_t arg, size_t padding)
50 {
51 init(arg, padding);
52 }
53
54 void init(size_t arg, size_t padding)
55 {
56 _lengths.resize(arg);
57 _keys.resize(arg);
58
59 for (size_t x= 0; x < _keys.size(); x++)
60 {
61 libtest::vchar_t key_buffer;
62 key_buffer.resize(padding +1);
63 memset(&key_buffer[0], 'x', padding);
64
65 if (HAVE_UUID_UUID_H)
66 {
67 #if defined(HAVE_UUID_UUID_H) && HAVE_UUID_UUID_H
68 uuid_t out;
69 uuid_generate(out);
70
71 uuid_unparse(out, &key_buffer[0]);
72 _keys[x]= strdup(&key_buffer[0]);
73 (_keys[x])[UUID_STRING_MAXLENGTH]= 'x';
74 #endif
75 }
76 else // We just use a number and pad the string if UUID is not available
77 {
78 char int_buffer[MEMCACHED_MAXIMUM_INTEGER_DISPLAY_LENGTH +1];
79 int key_length= snprintf(int_buffer, sizeof(int_buffer), "%u", uint32_t(x));
80 memcpy(&key_buffer[0], int_buffer, key_length);
81 _keys[x]= strdup(&key_buffer[0]);
82 }
83 _lengths[x]= padding;
84 }
85 }
86
87 ~keys_st()
88 {
89 for (libtest::vchar_ptr_t::iterator iter= _keys.begin();
90 iter != _keys.end();
91 ++iter)
92 {
93 ::free(*iter);
94 }
95 }
96
97 libtest::vchar_ptr_t::iterator begin()
98 {
99 return _keys.begin();
100 }
101
102 libtest::vchar_ptr_t::iterator end()
103 {
104 return _keys.end();
105 }
106
107 size_t size() const
108 {
109 return _keys.size();
110 }
111
112 std::vector<size_t>& lengths()
113 {
114 return _lengths;
115 }
116
117 libtest::vchar_ptr_t& keys()
118 {
119 return _keys;
120 }
121
122 size_t* lengths_ptr()
123 {
124 return &_lengths[0];
125 }
126
127 char** keys_ptr()
128 {
129 return &_keys[0];
130 }
131
132 char* key_at(size_t arg)
133 {
134 return _keys[arg];
135 }
136
137 size_t length_at(size_t arg)
138 {
139 return _lengths[arg];
140 }
141
142 private:
143 libtest::vchar_ptr_t _keys;
144 std::vector<size_t> _lengths;
145 };