|
| 1 | +/* Unit tests for valkeyClusterGetSlotByKey. */ |
| 2 | + |
| 3 | +#include "cluster.h" |
| 4 | +#include "test_utils.h" |
| 5 | + |
| 6 | +#include <assert.h> |
| 7 | + |
| 8 | +void test_slot_by_key_basic(void) { |
| 9 | + /* A simple key should produce a consistent slot. */ |
| 10 | + unsigned int slot = valkeyClusterGetSlotByKey((char *)"foo", 3); |
| 11 | + assert(slot == valkeyClusterGetSlotByKey((char *)"foo", 3)); |
| 12 | +} |
| 13 | + |
| 14 | +void test_slot_by_key_with_hashtag(void) { |
| 15 | + /* Keys with the same hash tag should map to the same slot. */ |
| 16 | + unsigned int slot1 = valkeyClusterGetSlotByKey((char *)"{tag}key1", 9); |
| 17 | + unsigned int slot2 = valkeyClusterGetSlotByKey((char *)"{tag}key2", 9); |
| 18 | + assert(slot1 == slot2); |
| 19 | +} |
| 20 | + |
| 21 | +void test_slot_by_key_binary_safe(void) { |
| 22 | + /* Key with embedded null: "ke\0y" (length 4) should produce a |
| 23 | + * different slot than "ke" (length 2, what strlen would give). */ |
| 24 | + unsigned int slot_full = valkeyClusterGetSlotByKey((char *)"ke\0y", 4); |
| 25 | + unsigned int slot_truncated = valkeyClusterGetSlotByKey((char *)"ke", 2); |
| 26 | + assert(slot_full != slot_truncated); |
| 27 | +} |
| 28 | + |
| 29 | +void test_slot_by_key_empty(void) { |
| 30 | + /* An empty string is a valid key. */ |
| 31 | + unsigned int slot = valkeyClusterGetSlotByKey((char *)"", 0); |
| 32 | + assert(slot == 0); |
| 33 | +} |
| 34 | + |
| 35 | +int main(void) { |
| 36 | + test_slot_by_key_basic(); |
| 37 | + test_slot_by_key_with_hashtag(); |
| 38 | + test_slot_by_key_binary_safe(); |
| 39 | + test_slot_by_key_empty(); |
| 40 | + return 0; |
| 41 | +} |
0 commit comments