|
| 1 | +package redis.clients.jedis.commands.commandobjects; |
| 2 | + |
| 3 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 4 | +import static org.hamcrest.Matchers.contains; |
| 5 | +import static org.hamcrest.Matchers.equalTo; |
| 6 | + |
| 7 | +import java.util.List; |
| 8 | + |
| 9 | +import org.junit.Test; |
| 10 | +import redis.clients.jedis.RedisProtocol; |
| 11 | +import redis.clients.jedis.args.BitCountOption; |
| 12 | +import redis.clients.jedis.args.BitOP; |
| 13 | +import redis.clients.jedis.params.BitPosParams; |
| 14 | + |
| 15 | +/** |
| 16 | + * Tests related to <a href="https://redis.io/commands/?group=bitmap">Bitmap</a> commands. |
| 17 | + */ |
| 18 | +public class CommandObjectsBitmapCommandsTest extends CommandObjectsStandaloneTestBase { |
| 19 | + |
| 20 | + public CommandObjectsBitmapCommandsTest(RedisProtocol protocol) { |
| 21 | + super(protocol); |
| 22 | + } |
| 23 | + |
| 24 | + @Test |
| 25 | + public void testSetbitAndGetbit() { |
| 26 | + String key = "bitKey"; |
| 27 | + long offset = 10; |
| 28 | + |
| 29 | + Boolean initialValue = exec(commandObjects.getbit(key, offset)); |
| 30 | + assertThat(initialValue, equalTo(false)); |
| 31 | + |
| 32 | + Boolean setbit = exec(commandObjects.setbit(key, offset, true)); |
| 33 | + assertThat(setbit, equalTo(false)); // original value returned |
| 34 | + |
| 35 | + Boolean finalValue = exec(commandObjects.getbit(key, offset)); |
| 36 | + assertThat(finalValue, equalTo(true)); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + public void testSetbitAndGetbitBinary() { |
| 41 | + byte[] key = "bitKeyBytes".getBytes(); |
| 42 | + long offset = 10; |
| 43 | + |
| 44 | + Boolean initialValue = exec(commandObjects.getbit(key, offset)); |
| 45 | + assertThat(initialValue, equalTo(false)); |
| 46 | + |
| 47 | + Boolean setbit = exec(commandObjects.setbit(key, offset, true)); |
| 48 | + assertThat(setbit, equalTo(false)); // original value returned |
| 49 | + |
| 50 | + Boolean finalValue = exec(commandObjects.getbit(key, offset)); |
| 51 | + assertThat(finalValue, equalTo(true)); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void testBitcount() { |
| 56 | + String key = "bitcountKey"; |
| 57 | + byte[] keyBytes = key.getBytes(); |
| 58 | + |
| 59 | + // Set some bits |
| 60 | + exec(commandObjects.setbit(key, 1, true)); |
| 61 | + exec(commandObjects.setbit(key, 2, true)); |
| 62 | + exec(commandObjects.setbit(key, 7, true)); // This makes 1 byte with 3 bits set |
| 63 | + exec(commandObjects.setbit(key, 8, true)); // Next byte, first bit set |
| 64 | + |
| 65 | + Long bitcountFullString = exec(commandObjects.bitcount(key)); |
| 66 | + assertThat(bitcountFullString, equalTo(4L)); |
| 67 | + |
| 68 | + Long bitcountFirstByte = exec(commandObjects.bitcount(key, 0, 0)); |
| 69 | + assertThat(bitcountFirstByte, equalTo(3L)); |
| 70 | + |
| 71 | + Long bitcountFullStringBinary = exec(commandObjects.bitcount(keyBytes)); |
| 72 | + assertThat(bitcountFullStringBinary, equalTo(4L)); |
| 73 | + |
| 74 | + Long bitcountFirstByteBinary = exec(commandObjects.bitcount(keyBytes, 0, 0)); |
| 75 | + assertThat(bitcountFirstByteBinary, equalTo(3L)); |
| 76 | + |
| 77 | + Long bitcountFirstSixBits = exec(commandObjects.bitcount(key, 0, 5, BitCountOption.BIT)); |
| 78 | + assertThat(bitcountFirstSixBits, equalTo(2L)); |
| 79 | + |
| 80 | + Long bitcountFirstSixBitsBinary = exec(commandObjects.bitcount(keyBytes, 0, 5, BitCountOption.BIT)); |
| 81 | + assertThat(bitcountFirstSixBitsBinary, equalTo(2L)); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void testBitpos() { |
| 86 | + String key = "bitposKey"; |
| 87 | + byte[] keyBytes = key.getBytes(); |
| 88 | + |
| 89 | + // Set some bits |
| 90 | + exec(commandObjects.setbit(key, 10, true)); |
| 91 | + exec(commandObjects.setbit(key, 22, true)); |
| 92 | + exec(commandObjects.setbit(key, 30, true)); |
| 93 | + |
| 94 | + Long firstSetBit = exec(commandObjects.bitpos(key, true)); |
| 95 | + assertThat(firstSetBit, equalTo(10L)); |
| 96 | + |
| 97 | + Long firstUnsetBit = exec(commandObjects.bitpos(key, false)); |
| 98 | + assertThat(firstUnsetBit, equalTo(0L)); |
| 99 | + |
| 100 | + BitPosParams params = new BitPosParams(15, 25).modifier(BitCountOption.BIT); |
| 101 | + |
| 102 | + Long firstSetBitInRange = exec(commandObjects.bitpos(key, true, params)); |
| 103 | + assertThat(firstSetBitInRange, equalTo(22L)); |
| 104 | + |
| 105 | + Long firstUnsetBitInRange = exec(commandObjects.bitpos(key, false, params)); |
| 106 | + assertThat(firstUnsetBitInRange, equalTo(15L)); |
| 107 | + |
| 108 | + Long firstSetBitBinary = exec(commandObjects.bitpos(keyBytes, true)); |
| 109 | + assertThat(firstSetBitBinary, equalTo(10L)); |
| 110 | + |
| 111 | + Long firstUnsetBitBinary = exec(commandObjects.bitpos(keyBytes, false)); |
| 112 | + assertThat(firstUnsetBitBinary, equalTo(0L)); |
| 113 | + |
| 114 | + Long firstSetBitInRangeBinary = exec(commandObjects.bitpos(keyBytes, true, params)); |
| 115 | + assertThat(firstSetBitInRangeBinary, equalTo(22L)); |
| 116 | + |
| 117 | + Long firstUnsetBitInRangeBinary = exec(commandObjects.bitpos(keyBytes, false, params)); |
| 118 | + assertThat(firstUnsetBitInRangeBinary, equalTo(15L)); |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + public void testBitfield() { |
| 123 | + String key = "bitfieldKey"; |
| 124 | + |
| 125 | + List<Long> bitfieldResult = exec(commandObjects.bitfield( |
| 126 | + key, "INCRBY", "i5", "100", "7", "GET", "i5", "100")); |
| 127 | + |
| 128 | + // Contains the result of the INCRBY operation, and the result of the GET operation. |
| 129 | + assertThat(bitfieldResult, contains(7L, 7L)); |
| 130 | + |
| 131 | + List<Long> bitfieldRoResult = exec(commandObjects.bitfieldReadonly( |
| 132 | + key, "GET", "i4", "100")); |
| 133 | + assertThat(bitfieldRoResult, contains(3L)); |
| 134 | + } |
| 135 | + |
| 136 | + @Test |
| 137 | + public void testBitfieldBinary() { |
| 138 | + byte[] key = "bitfieldKeyBytes".getBytes(); |
| 139 | + |
| 140 | + List<Long> bitfieldResult = exec(commandObjects.bitfield(key, |
| 141 | + "INCRBY".getBytes(), "i5".getBytes(), "100".getBytes(), "7".getBytes(), |
| 142 | + "GET".getBytes(), "i5".getBytes(), "100".getBytes())); |
| 143 | + |
| 144 | + // Contains the result of the INCRBY operation, and the result of the GET operation. |
| 145 | + assertThat(bitfieldResult, contains(7L, 7L)); |
| 146 | + |
| 147 | + List<Long> bitfieldRoResult = exec(commandObjects.bitfieldReadonly(key, |
| 148 | + "GET".getBytes(), "i4".getBytes(), "100".getBytes())); |
| 149 | + assertThat(bitfieldRoResult, contains(3L)); |
| 150 | + } |
| 151 | + |
| 152 | + @Test |
| 153 | + public void testBitop() { |
| 154 | + String srcKey1 = "srcKey1"; |
| 155 | + String srcKey2 = "srcKey2"; |
| 156 | + String destKey = "destKey"; |
| 157 | + |
| 158 | + // Set some bits |
| 159 | + exec(commandObjects.setbit(srcKey1, 1, true)); |
| 160 | + exec(commandObjects.setbit(srcKey1, 2, true)); |
| 161 | + exec(commandObjects.setbit(srcKey1, 3, true)); |
| 162 | + |
| 163 | + exec(commandObjects.setbit(srcKey2, 1, true)); |
| 164 | + exec(commandObjects.setbit(srcKey2, 3, true)); |
| 165 | + |
| 166 | + Long bitopResult = exec(commandObjects.bitop(BitOP.AND, destKey, srcKey1, srcKey2)); |
| 167 | + assertThat(bitopResult, equalTo(1L)); // 1 byte stored |
| 168 | + |
| 169 | + assertThat(exec(commandObjects.getbit(destKey, 1)), equalTo(true)); |
| 170 | + assertThat(exec(commandObjects.getbit(destKey, 2)), equalTo(false)); |
| 171 | + assertThat(exec(commandObjects.getbit(destKey, 3)), equalTo(true)); |
| 172 | + } |
| 173 | + |
| 174 | + @Test |
| 175 | + public void testBitopBinary() { |
| 176 | + byte[] srcKey1 = "srcKey1".getBytes(); |
| 177 | + byte[] srcKey2 = "srcKey2".getBytes(); |
| 178 | + byte[] destKey = "destKey".getBytes(); |
| 179 | + |
| 180 | + // Set some bits |
| 181 | + exec(commandObjects.setbit(srcKey1, 1, true)); |
| 182 | + exec(commandObjects.setbit(srcKey1, 2, true)); |
| 183 | + exec(commandObjects.setbit(srcKey1, 3, true)); |
| 184 | + |
| 185 | + exec(commandObjects.setbit(srcKey2, 1, true)); |
| 186 | + exec(commandObjects.setbit(srcKey2, 3, true)); |
| 187 | + |
| 188 | + Long bitopResult = exec(commandObjects.bitop(BitOP.XOR, destKey, srcKey1, srcKey2)); |
| 189 | + assertThat(bitopResult, equalTo(1L)); // 1 byte stored |
| 190 | + |
| 191 | + assertThat(exec(commandObjects.getbit(new String(destKey), 1)), equalTo(false)); |
| 192 | + assertThat(exec(commandObjects.getbit(new String(destKey), 2)), equalTo(true)); |
| 193 | + assertThat(exec(commandObjects.getbit(new String(destKey), 3)), equalTo(false)); |
| 194 | + } |
| 195 | +} |
0 commit comments