diff options
| author | Mitja Felicijan <mitja.felicijan@gmail.com> | 2026-01-21 22:40:55 +0100 |
|---|---|---|
| committer | Mitja Felicijan <mitja.felicijan@gmail.com> | 2026-01-21 22:40:55 +0100 |
| commit | 5d8dfe892a2ea89f706ee140c3bdcfd89fe03fda (patch) | |
| tree | 1acdfa5220cd13b7be43a2a01368e80d306473ca /examples/redis-unstable/tests/modules/reply.c | |
| parent | c7ab12bba64d9c20ccd79b132dac475f7bc3923e (diff) | |
| download | crep-5d8dfe892a2ea89f706ee140c3bdcfd89fe03fda.tar.gz | |
Add Redis source code for testing
Diffstat (limited to 'examples/redis-unstable/tests/modules/reply.c')
| -rw-r--r-- | examples/redis-unstable/tests/modules/reply.c | 214 |
1 files changed, 214 insertions, 0 deletions
diff --git a/examples/redis-unstable/tests/modules/reply.c b/examples/redis-unstable/tests/modules/reply.c new file mode 100644 index 0000000..c5baa66 --- /dev/null +++ b/examples/redis-unstable/tests/modules/reply.c | |||
| @@ -0,0 +1,214 @@ | |||
| 1 | /* | ||
| 2 | * A module the tests RM_ReplyWith family of commands | ||
| 3 | */ | ||
| 4 | |||
| 5 | #include "redismodule.h" | ||
| 6 | #include <math.h> | ||
| 7 | |||
| 8 | int rw_string(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { | ||
| 9 | if (argc != 2) return RedisModule_WrongArity(ctx); | ||
| 10 | |||
| 11 | return RedisModule_ReplyWithString(ctx, argv[1]); | ||
| 12 | } | ||
| 13 | |||
| 14 | int rw_cstring(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { | ||
| 15 | REDISMODULE_NOT_USED(argv); | ||
| 16 | if (argc != 1) return RedisModule_WrongArity(ctx); | ||
| 17 | |||
| 18 | return RedisModule_ReplyWithSimpleString(ctx, "A simple string"); | ||
| 19 | } | ||
| 20 | |||
| 21 | int rw_int(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { | ||
| 22 | if (argc != 2) return RedisModule_WrongArity(ctx); | ||
| 23 | |||
| 24 | long long integer; | ||
| 25 | if (RedisModule_StringToLongLong(argv[1], &integer) != REDISMODULE_OK) | ||
| 26 | return RedisModule_ReplyWithError(ctx, "Arg cannot be parsed as an integer"); | ||
| 27 | |||
| 28 | return RedisModule_ReplyWithLongLong(ctx, integer); | ||
| 29 | } | ||
| 30 | |||
| 31 | /* When one argument is given, it is returned as a double, | ||
| 32 | * when two arguments are given, it returns a/b. */ | ||
| 33 | int rw_double(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { | ||
| 34 | if (argc==1) | ||
| 35 | return RedisModule_ReplyWithDouble(ctx, NAN); | ||
| 36 | |||
| 37 | if (argc != 2 && argc != 3) return RedisModule_WrongArity(ctx); | ||
| 38 | |||
| 39 | double dbl, dbl2; | ||
| 40 | if (RedisModule_StringToDouble(argv[1], &dbl) != REDISMODULE_OK) | ||
| 41 | return RedisModule_ReplyWithError(ctx, "Arg cannot be parsed as a double"); | ||
| 42 | if (argc == 3) { | ||
| 43 | if (RedisModule_StringToDouble(argv[2], &dbl2) != REDISMODULE_OK) | ||
| 44 | return RedisModule_ReplyWithError(ctx, "Arg cannot be parsed as a double"); | ||
| 45 | dbl /= dbl2; | ||
| 46 | } | ||
| 47 | |||
| 48 | return RedisModule_ReplyWithDouble(ctx, dbl); | ||
| 49 | } | ||
| 50 | |||
| 51 | int rw_longdouble(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { | ||
| 52 | if (argc != 2) return RedisModule_WrongArity(ctx); | ||
| 53 | |||
| 54 | long double longdbl; | ||
| 55 | if (RedisModule_StringToLongDouble(argv[1], &longdbl) != REDISMODULE_OK) | ||
| 56 | return RedisModule_ReplyWithError(ctx, "Arg cannot be parsed as a double"); | ||
| 57 | |||
| 58 | return RedisModule_ReplyWithLongDouble(ctx, longdbl); | ||
| 59 | } | ||
| 60 | |||
| 61 | int rw_bignumber(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { | ||
| 62 | if (argc != 2) return RedisModule_WrongArity(ctx); | ||
| 63 | |||
| 64 | size_t bignum_len; | ||
| 65 | const char *bignum_str = RedisModule_StringPtrLen(argv[1], &bignum_len); | ||
| 66 | |||
| 67 | return RedisModule_ReplyWithBigNumber(ctx, bignum_str, bignum_len); | ||
| 68 | } | ||
| 69 | |||
| 70 | int rw_array(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { | ||
| 71 | if (argc != 2) return RedisModule_WrongArity(ctx); | ||
| 72 | |||
| 73 | long long integer; | ||
| 74 | if (RedisModule_StringToLongLong(argv[1], &integer) != REDISMODULE_OK) | ||
| 75 | return RedisModule_ReplyWithError(ctx, "Arg cannot be parsed as a integer"); | ||
| 76 | |||
| 77 | RedisModule_ReplyWithArray(ctx, integer); | ||
| 78 | for (int i = 0; i < integer; ++i) { | ||
| 79 | RedisModule_ReplyWithLongLong(ctx, i); | ||
| 80 | } | ||
| 81 | |||
| 82 | return REDISMODULE_OK; | ||
| 83 | } | ||
| 84 | |||
| 85 | int rw_map(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { | ||
| 86 | if (argc != 2) return RedisModule_WrongArity(ctx); | ||
| 87 | |||
| 88 | long long integer; | ||
| 89 | if (RedisModule_StringToLongLong(argv[1], &integer) != REDISMODULE_OK) | ||
| 90 | return RedisModule_ReplyWithError(ctx, "Arg cannot be parsed as a integer"); | ||
| 91 | |||
| 92 | RedisModule_ReplyWithMap(ctx, integer); | ||
| 93 | for (int i = 0; i < integer; ++i) { | ||
| 94 | RedisModule_ReplyWithLongLong(ctx, i); | ||
| 95 | RedisModule_ReplyWithDouble(ctx, i * 1.5); | ||
| 96 | } | ||
| 97 | |||
| 98 | return REDISMODULE_OK; | ||
| 99 | } | ||
| 100 | |||
| 101 | int rw_set(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { | ||
| 102 | if (argc != 2) return RedisModule_WrongArity(ctx); | ||
| 103 | |||
| 104 | long long integer; | ||
| 105 | if (RedisModule_StringToLongLong(argv[1], &integer) != REDISMODULE_OK) | ||
| 106 | return RedisModule_ReplyWithError(ctx, "Arg cannot be parsed as a integer"); | ||
| 107 | |||
| 108 | RedisModule_ReplyWithSet(ctx, integer); | ||
| 109 | for (int i = 0; i < integer; ++i) { | ||
| 110 | RedisModule_ReplyWithLongLong(ctx, i); | ||
| 111 | } | ||
| 112 | |||
| 113 | return REDISMODULE_OK; | ||
| 114 | } | ||
| 115 | |||
| 116 | int rw_attribute(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { | ||
| 117 | if (argc != 2) return RedisModule_WrongArity(ctx); | ||
| 118 | |||
| 119 | long long integer; | ||
| 120 | if (RedisModule_StringToLongLong(argv[1], &integer) != REDISMODULE_OK) | ||
| 121 | return RedisModule_ReplyWithError(ctx, "Arg cannot be parsed as a integer"); | ||
| 122 | |||
| 123 | if (RedisModule_ReplyWithAttribute(ctx, integer) != REDISMODULE_OK) { | ||
| 124 | return RedisModule_ReplyWithError(ctx, "Attributes aren't supported by RESP 2"); | ||
| 125 | } | ||
| 126 | |||
| 127 | for (int i = 0; i < integer; ++i) { | ||
| 128 | RedisModule_ReplyWithLongLong(ctx, i); | ||
| 129 | RedisModule_ReplyWithDouble(ctx, i * 1.5); | ||
| 130 | } | ||
| 131 | |||
| 132 | RedisModule_ReplyWithSimpleString(ctx, "OK"); | ||
| 133 | return REDISMODULE_OK; | ||
| 134 | } | ||
| 135 | |||
| 136 | int rw_bool(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { | ||
| 137 | REDISMODULE_NOT_USED(argv); | ||
| 138 | if (argc != 1) return RedisModule_WrongArity(ctx); | ||
| 139 | |||
| 140 | RedisModule_ReplyWithArray(ctx, 2); | ||
| 141 | RedisModule_ReplyWithBool(ctx, 0); | ||
| 142 | return RedisModule_ReplyWithBool(ctx, 1); | ||
| 143 | } | ||
| 144 | |||
| 145 | int rw_null(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { | ||
| 146 | REDISMODULE_NOT_USED(argv); | ||
| 147 | if (argc != 1) return RedisModule_WrongArity(ctx); | ||
| 148 | |||
| 149 | return RedisModule_ReplyWithNull(ctx); | ||
| 150 | } | ||
| 151 | |||
| 152 | int rw_error(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { | ||
| 153 | REDISMODULE_NOT_USED(argv); | ||
| 154 | if (argc != 1) return RedisModule_WrongArity(ctx); | ||
| 155 | |||
| 156 | return RedisModule_ReplyWithError(ctx, "An error"); | ||
| 157 | } | ||
| 158 | |||
| 159 | int rw_error_format(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { | ||
| 160 | if (argc != 3) return RedisModule_WrongArity(ctx); | ||
| 161 | |||
| 162 | return RedisModule_ReplyWithErrorFormat(ctx, | ||
| 163 | RedisModule_StringPtrLen(argv[1], NULL), | ||
| 164 | RedisModule_StringPtrLen(argv[2], NULL)); | ||
| 165 | } | ||
| 166 | |||
| 167 | int rw_verbatim(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { | ||
| 168 | if (argc != 2) return RedisModule_WrongArity(ctx); | ||
| 169 | |||
| 170 | size_t verbatim_len; | ||
| 171 | const char *verbatim_str = RedisModule_StringPtrLen(argv[1], &verbatim_len); | ||
| 172 | |||
| 173 | return RedisModule_ReplyWithVerbatimString(ctx, verbatim_str, verbatim_len); | ||
| 174 | } | ||
| 175 | |||
| 176 | int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { | ||
| 177 | REDISMODULE_NOT_USED(argv); | ||
| 178 | REDISMODULE_NOT_USED(argc); | ||
| 179 | if (RedisModule_Init(ctx, "replywith", 1, REDISMODULE_APIVER_1) != REDISMODULE_OK) | ||
| 180 | return REDISMODULE_ERR; | ||
| 181 | |||
| 182 | if (RedisModule_CreateCommand(ctx,"rw.string",rw_string,"",0,0,0) != REDISMODULE_OK) | ||
| 183 | return REDISMODULE_ERR; | ||
| 184 | if (RedisModule_CreateCommand(ctx,"rw.cstring",rw_cstring,"",0,0,0) != REDISMODULE_OK) | ||
| 185 | return REDISMODULE_ERR; | ||
| 186 | if (RedisModule_CreateCommand(ctx,"rw.bignumber",rw_bignumber,"",0,0,0) != REDISMODULE_OK) | ||
| 187 | return REDISMODULE_ERR; | ||
| 188 | if (RedisModule_CreateCommand(ctx,"rw.int",rw_int,"",0,0,0) != REDISMODULE_OK) | ||
| 189 | return REDISMODULE_ERR; | ||
| 190 | if (RedisModule_CreateCommand(ctx,"rw.double",rw_double,"",0,0,0) != REDISMODULE_OK) | ||
| 191 | return REDISMODULE_ERR; | ||
| 192 | if (RedisModule_CreateCommand(ctx,"rw.longdouble",rw_longdouble,"",0,0,0) != REDISMODULE_OK) | ||
| 193 | return REDISMODULE_ERR; | ||
| 194 | if (RedisModule_CreateCommand(ctx,"rw.array",rw_array,"",0,0,0) != REDISMODULE_OK) | ||
| 195 | return REDISMODULE_ERR; | ||
| 196 | if (RedisModule_CreateCommand(ctx,"rw.map",rw_map,"",0,0,0) != REDISMODULE_OK) | ||
| 197 | return REDISMODULE_ERR; | ||
| 198 | if (RedisModule_CreateCommand(ctx,"rw.attribute",rw_attribute,"",0,0,0) != REDISMODULE_OK) | ||
| 199 | return REDISMODULE_ERR; | ||
| 200 | if (RedisModule_CreateCommand(ctx,"rw.set",rw_set,"",0,0,0) != REDISMODULE_OK) | ||
| 201 | return REDISMODULE_ERR; | ||
| 202 | if (RedisModule_CreateCommand(ctx,"rw.bool",rw_bool,"",0,0,0) != REDISMODULE_OK) | ||
| 203 | return REDISMODULE_ERR; | ||
| 204 | if (RedisModule_CreateCommand(ctx,"rw.null",rw_null,"",0,0,0) != REDISMODULE_OK) | ||
| 205 | return REDISMODULE_ERR; | ||
| 206 | if (RedisModule_CreateCommand(ctx,"rw.error",rw_error,"",0,0,0) != REDISMODULE_OK) | ||
| 207 | return REDISMODULE_ERR; | ||
| 208 | if (RedisModule_CreateCommand(ctx,"rw.error_format",rw_error_format,"",0,0,0) != REDISMODULE_OK) | ||
| 209 | return REDISMODULE_ERR; | ||
| 210 | if (RedisModule_CreateCommand(ctx,"rw.verbatim",rw_verbatim,"",0,0,0) != REDISMODULE_OK) | ||
| 211 | return REDISMODULE_ERR; | ||
| 212 | |||
| 213 | return REDISMODULE_OK; | ||
| 214 | } | ||
