Skip to content

Commit

Permalink
feat: add BitmapUtil unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
Planeswalker23 committed Mar 2, 2022
1 parent e4a559e commit e1226db
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ logs
.DS_Store

#test
test/
#test/

# 字节码文件
*.class
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class BitmapUtil {
* @param key 键
* @return String
*/
private String get(String key) {
public String get(String key) {
String result = redisTemplate.opsForValue().get(key);
log.info("Bitmap GET operation successfully. Result following: key is {{}}, value is {{}}.", key, result);
return result;
Expand Down Expand Up @@ -126,4 +126,18 @@ public Long bitPos(String key, boolean value, long start, long end) {
log.info("Bitmap BITPOS operation successfully. Result following: key is {{}}, value is {{}}, result is {{}}, startByteIndex is {}, endByteIndex is {}.", key, value, result, start, end);
return result;
}

/**
* 根据key获取整个位图的值,并转化为8位 0-1 String
*
* @param key 键
* @return String
*/
public String getBitmapUsingString(String key) {
String redisResult = redisTemplate.opsForValue().get(key);
byte[] byteResult = redisResult == null ? null : redisResult.getBytes();
String result = ByteUtil.getStringFormByteArray(byteResult);
log.info("Bitmap GET operation successfully. Result following: key is {{}}, value is {{}}.", key, result);
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package io.walkers.planes.pandora.redis.sign.util;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

/**
* @author planeswalker23
* @date 2022/3/2
*/
@SpringBootTest
public class BitmapUtilTest {

@Autowired
private BitmapUtil bitmapUtil;

private final String key = "key";

@Test
public void coreMethod() {
// 01100001
String a = "a";
bitmapUtil.set(key, a);
String result = bitmapUtil.get(key);
Assertions.assertEquals(a, result);

// SETBIT 0 0 : 0
Boolean setBitResult = bitmapUtil.setBit(key, 0L, false);
Assertions.assertEquals(Boolean.FALSE, setBitResult);

// GETBIT 0 2 : 1
Boolean getBitResult = bitmapUtil.getBit(key, 2L);
Assertions.assertEquals(Boolean.TRUE, getBitResult);

// BITCOUNT key : 3
Long countResult1 = bitmapUtil.bitCountTrue(key);
Assertions.assertEquals(3L, countResult1);
// BITCOUNT key 0 1 : 3
Long countResult2 = bitmapUtil.bitCountTrue(key, 0L, 1L);
Assertions.assertEquals(3L, countResult2);
// BITCOUNT key 1 2 : 0
Long countResult3 = bitmapUtil.bitCountTrue(key, 1L, 2L);
Assertions.assertEquals(0L, countResult3);

// BITPOS key 1 : 1
Long countPos1Result = bitmapUtil.bitPos(key, true);
Assertions.assertEquals(1L, countPos1Result);

// BITPOS key 1 0 1: 1
Long countPos0Result = bitmapUtil.bitPos(key, true, 0L, 1L);
Assertions.assertEquals(1L, countPos0Result);

// BITPOS key 1 1 2: -1
Long countPos0Result2 = bitmapUtil.bitPos(key, true, 1L, 2L);
Assertions.assertEquals(-1L, countPos0Result2);

}
}
9 changes: 9 additions & 0 deletions redis/src/test/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#Redis
spring.redis.host=127.0.0.1
spring.redis.port=6379

spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:db
spring.h2.console.enabled=true

spring.jpa.show-sql=true

0 comments on commit e1226db

Please sign in to comment.