forked from yinqiwen/ardb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhash_test.cpp
executable file
·233 lines (207 loc) · 8.02 KB
/
hash_test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
* hash_testcase.cpp
*
* Created on: 2013-4-9
* Author: yinqiwen
*/
#include "ardb.hpp"
#include <string>
using namespace ardb;
void test_hash_common(Context& ctx, Ardb& db)
{
db.GetConfig().hash_max_ziplist_entries = 16;
RedisCommandFrame del;
del.SetFullCommand("del myhash");
db.Call(ctx, del, 0);
for (uint32 i = 0; i < 10; i++)
{
RedisCommandFrame hset;
hset.SetFullCommand("hset myhash field%u value%u", i, i);
db.Call(ctx, hset, 0);
CHECK_FATAL(ctx.reply.integer != 1, "hset myhash failed");
RedisCommandFrame hget;
hget.SetFullCommand("hget myhash field%u", i);
db.Call(ctx, hget, 0);
CHECK_FATAL(ctx.reply.str != hset.GetArguments()[2], "hget myhash failed");
}
RedisCommandFrame hexist;
hexist.SetFullCommand("hexists myhash field4");
db.Call(ctx, hexist, 0);
CHECK_FATAL(ctx.reply.integer != 1, "hexist myhash failed");
hexist.SetFullCommand("hexists myhash field400");
db.Call(ctx, hexist, 0);
CHECK_FATAL(ctx.reply.integer != 0, "hexist myhash failed");
RedisCommandFrame hdel;
hdel.SetFullCommand("hdel myhash yfield5");
db.Call(ctx, hdel, 0);
CHECK_FATAL(ctx.reply.integer != 0, "hdel myhash failed");
hdel.SetFullCommand("hdel myhash field5");
db.Call(ctx, hdel, 0);
CHECK_FATAL(ctx.reply.integer != 1, "hdel myhash failed");
RedisCommandFrame hgetall;
hgetall.SetFullCommand("hgetall myhash");
db.Call(ctx, hgetall, 0);
CHECK_FATAL(ctx.reply.MemberSize() != 18, "hgetall myhash failed");
RedisCommandFrame hkeys;
hkeys.SetFullCommand("hkeys myhash");
db.Call(ctx, hkeys, 0);
CHECK_FATAL(ctx.reply.MemberSize() != 9, "hkeys myhash failed");
RedisCommandFrame hvalues;
hvalues.SetFullCommand("hvals myhash");
db.Call(ctx, hvalues, 0);
CHECK_FATAL(ctx.reply.MemberSize() != 9, "hvalues myhash failed");
RedisCommandFrame hlen;
hlen.SetFullCommand("hlen myhash");
db.Call(ctx, hlen, 0);
CHECK_FATAL(ctx.reply.integer != 9, "hlen myhash failed");
//non zipmap
for (uint32 i = 0; i < 10; i++)
{
RedisCommandFrame hset;
hset.SetFullCommand("hset myhash xfield%u xvalue%u", i, i);
db.Call(ctx, hset, 0);
CHECK_FATAL(ctx.reply.integer != 1, "hset myhash failed");
RedisCommandFrame hget;
hget.SetFullCommand("hget myhash xfield%u", i);
db.Call(ctx, hget, 0);
CHECK_FATAL(ctx.reply.str != hset.GetArguments()[2], "hget myhash failed");
}
hexist.SetFullCommand("hexists myhash xfield4");
db.Call(ctx, hexist, 0);
CHECK_FATAL(ctx.reply.integer != 1, "hexist myhash failed");
hexist.SetFullCommand("hexists myhash xfield400");
db.Call(ctx, hexist, 0);
CHECK_FATAL(ctx.reply.integer != 0, "hexist myhash failed");
hdel.SetFullCommand("hdel myhash yfield5 yfield6");
db.Call(ctx, hdel, 0);
CHECK_FATAL(ctx.reply.integer != 0, "hdel myhash failed");
hdel.SetFullCommand("hdel myhash xfield5 xfield6");
db.Call(ctx, hdel, 0);
CHECK_FATAL(ctx.reply.integer != 2, "hdel myhash failed");
db.Call(ctx, hgetall, 0);
CHECK_FATAL(ctx.reply.MemberSize() != 34, "hgetall myhash failed");
db.Call(ctx, hkeys, 0);
CHECK_FATAL(ctx.reply.MemberSize() != 17, "hkeys myhash failed");
db.Call(ctx, hvalues, 0);
CHECK_FATAL(ctx.reply.MemberSize() != 17, "hvalues myhash failed");
db.Call(ctx, hlen, 0);
CHECK_FATAL(ctx.reply.integer != 17, "hlen myhash failed");
}
void test_hash_incr(Context& ctx, Ardb& db)
{
db.GetConfig().hash_max_ziplist_entries = 16;
RedisCommandFrame del;
del.SetFullCommand("del myhash");
db.Call(ctx, del, 0);
//incr on not exist key
RedisCommandFrame incr;
incr.SetFullCommand("HINCRBY myhash filed 11");
db.Call(ctx, incr, 0);
CHECK_FATAL(ctx.reply.integer != 11, "HINCRBY myhash failed");
RedisCommandFrame hget;
hget.SetFullCommand("hget myhash filed");
db.Call(ctx, hget, 0);
CHECK_FATAL(ctx.reply.str != "11", "hget myhash failed");
db.Call(ctx, incr, 0);
CHECK_FATAL(ctx.reply.integer != 22, "HINCRBY myhash failed");;
db.Call(ctx, hget, 0);
CHECK_FATAL(ctx.reply.str != "22", "hget myhash failed");
RedisCommandFrame incrbyfloat;
incrbyfloat.SetFullCommand("hincrbyfloat myhash ffiled 10.1");
db.Call(ctx, incrbyfloat, 0);
CHECK_FATAL(ctx.reply.str != "10.1", "hincrbyfloat myhash failed");
hget.SetFullCommand("hget myhash ffiled");
db.Call(ctx, hget, 0);
CHECK_FATAL(ctx.reply.str != "10.1", "hget myhash failed");
db.Call(ctx, incrbyfloat, 0);
CHECK_FATAL(ctx.reply.str != "20.2", "incrbyfloat myhash failed");;
db.Call(ctx, hget, 0);
CHECK_FATAL(ctx.reply.str != "20.2", "hget myhash failed");
for (uint32 i = 0; i < 20; i++)
{
RedisCommandFrame hset;
hset.SetFullCommand("hset myhash xfield%u xvalue%u", i, i);
db.Call(ctx, hset, 0);
}
incr.SetFullCommand("HINCRBY myhash filedx 11");
db.Call(ctx, incr, 0);
CHECK_FATAL(ctx.reply.integer != 11, "HINCRBY myhash failed");
hget.SetFullCommand("hget myhash filedx");
db.Call(ctx, hget, 0);
CHECK_FATAL(ctx.reply.str != "11", "hget myhash failed");
db.Call(ctx, incr, 0);
CHECK_FATAL(ctx.reply.integer != 22, "HINCRBY myhash failed");;
db.Call(ctx, hget, 0);
CHECK_FATAL(ctx.reply.str != "22", "hget myhash failed");
incrbyfloat.SetFullCommand("hincrbyfloat myhash xffiled 10.1");
db.Call(ctx, incrbyfloat, 0);
CHECK_FATAL(ctx.reply.str != "10.1", "hincrbyfloat myhash failed");
hget.SetFullCommand("hget myhash xffiled");
db.Call(ctx, hget, 0);
CHECK_FATAL(ctx.reply.str != "10.1", "hget myhash failed");
db.Call(ctx, incrbyfloat, 0);
CHECK_FATAL(ctx.reply.str != "20.2", "incrbyfloat myhash failed");;
db.Call(ctx, hget, 0);
CHECK_FATAL(ctx.reply.str != "20.2", "hget myhash failed");
}
void test_hash_mgetset(Context& ctx, Ardb& db)
{
db.GetConfig().hash_max_ziplist_entries = 16;
RedisCommandFrame del;
del.SetFullCommand("del myhash");
db.Call(ctx, del, 0);
//incr on not exist key
RedisCommandFrame hmset;
hmset.SetFullCommand("hmset myhash filed0 0 field1 1");
db.Call(ctx, hmset, 0);
CHECK_FATAL(ctx.reply.str != "OK", "hmset myhash failed");
RedisCommandFrame hmget;
hmget.SetFullCommand("hmget myhash filed0 field10");
db.Call(ctx, hmget, 0);
CHECK_FATAL(ctx.reply.MemberSize() != 2, "hmget myhash failed");
CHECK_FATAL(ctx.reply.MemberAt(1).type != REDIS_REPLY_NIL, "hmget myhash failed");
for (uint32 i = 0; i < 20; i++)
{
RedisCommandFrame hset;
hset.SetFullCommand("hset myhash xfield%u xvalue%u", i, i);
db.Call(ctx, hset, 0);
}
db.Call(ctx, hmset, 0);
CHECK_FATAL(ctx.reply.str != "OK", "hmset myhash failed");
db.Call(ctx, hmget, 0);
CHECK_FATAL(ctx.reply.MemberSize() != 2, "hmget myhash failed");
CHECK_FATAL(ctx.reply.MemberAt(1).type != REDIS_REPLY_NIL, "hmget myhash failed");
}
void test_hash_setnx(Context& ctx, Ardb& db)
{
db.GetConfig().hash_max_ziplist_entries = 16;
RedisCommandFrame del;
del.SetFullCommand("del myhash");
db.Call(ctx, del, 0);
//incr on not exist key
RedisCommandFrame hsetnx;
hsetnx.SetFullCommand("hsetnx myhash filed0 0");
db.Call(ctx, hsetnx, 0);
CHECK_FATAL(ctx.reply.integer != 1, "hsetnx myhash failed");
db.Call(ctx, hsetnx, 0);
CHECK_FATAL(ctx.reply.integer != 0, "hsetnx myhash failed");
for (uint32 i = 0; i < 20; i++)
{
RedisCommandFrame hset;
hset.SetFullCommand("hset myhash xfield%u xvalue%u", i, i);
db.Call(ctx, hset, 0);
}
hsetnx.SetFullCommand("hsetnx myhash zfiled0 0");
db.Call(ctx, hsetnx, 0);
CHECK_FATAL(ctx.reply.integer != 1, "hsetnx myhash failed");
db.Call(ctx, hsetnx, 0);
CHECK_FATAL(ctx.reply.integer != 0, "hsetnx myhash failed");
}
void test_hash(Ardb& db)
{
Context tmpctx;
test_hash_common(tmpctx, db);
test_hash_incr(tmpctx, db);
test_hash_mgetset(tmpctx, db);
test_hash_setnx(tmpctx, db);
}