MCPcopy Create free account
hub / github.com/Snapchat/KeyDB / TestStringTruncate

Function TestStringTruncate

tests/modules/basics.c:158–208  ·  view source on GitHub ↗

TEST.STRING.TRUNCATE -- Test truncating an existing string object. */

Source from the content-addressed store, hash-verified

156
157/* TEST.STRING.TRUNCATE -- Test truncating an existing string object. */
158int TestStringTruncate(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
159 RedisModule_AutoMemory(ctx);
160 REDISMODULE_NOT_USED(argv);
161 REDISMODULE_NOT_USED(argc);
162
163 RedisModule_Call(ctx, "SET", "cc", "foo", "abcde");
164 RedisModuleKey *k = RedisModule_OpenKey(ctx, RedisModule_CreateStringPrintf(ctx, "foo"), REDISMODULE_READ | REDISMODULE_WRITE);
165 if (!k) return failTest(ctx, "Could not create key");
166
167 size_t len = 0;
168 char* s;
169
170 /* expand from 5 to 8 and check null pad */
171 if (REDISMODULE_ERR == RedisModule_StringTruncate(k, 8)) {
172 return failTest(ctx, "Could not truncate string value (8)");
173 }
174 s = RedisModule_StringDMA(k, &len, REDISMODULE_READ);
175 if (!s) {
176 return failTest(ctx, "Failed to read truncated string (8)");
177 } else if (len != 8) {
178 return failTest(ctx, "Failed to expand string value (8)");
179 } else if (0 != strncmp(s, "abcde\0\0\0", 8)) {
180 return failTest(ctx, "Failed to null pad string value (8)");
181 }
182
183 /* shrink from 8 to 4 */
184 if (REDISMODULE_ERR == RedisModule_StringTruncate(k, 4)) {
185 return failTest(ctx, "Could not truncate string value (4)");
186 }
187 s = RedisModule_StringDMA(k, &len, REDISMODULE_READ);
188 if (!s) {
189 return failTest(ctx, "Failed to read truncated string (4)");
190 } else if (len != 4) {
191 return failTest(ctx, "Failed to shrink string value (4)");
192 } else if (0 != strncmp(s, "abcd", 4)) {
193 return failTest(ctx, "Failed to truncate string value (4)");
194 }
195
196 /* shrink to 0 */
197 if (REDISMODULE_ERR == RedisModule_StringTruncate(k, 0)) {
198 return failTest(ctx, "Could not truncate string value (0)");
199 }
200 s = RedisModule_StringDMA(k, &len, REDISMODULE_READ);
201 if (!s) {
202 return failTest(ctx, "Failed to read truncated string (0)");
203 } else if (len != 0) {
204 return failTest(ctx, "Failed to shrink string value to (0)");
205 }
206
207 return RedisModule_ReplyWithSimpleString(ctx, "OK");
208}
209
210int NotifyCallback(RedisModuleCtx *ctx, int type, const char *event,
211 RedisModuleString *key) {

Callers

nothing calls this directly

Calls 1

failTestFunction · 0.85

Tested by

no test coverage detected