package encoding; import minsecurity.crypto.TestSM2; import org.junit.Test; import org.junit.runner.RunWith; import org.slf4j.LoggerFactory; import java.math.BigInteger; import static org.junit.Assert.*; /* * @Author: feng Zhao * @Description: * @Version: 1.0.0 * @Date: 12:28 2021/3/12 * @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室 */ public class VlIntTest { private static final org.slf4j.Logger logger = LoggerFactory.getLogger(VlIntTest.class); // @Test // public void size() { // } // // @Test // public void createVlInt() { // } // // @Test // public void createVlIntByBigInteger() { // } @Test public void getVlIntBytes() { byte[] a = new byte[]{1,2,3,4}; VlInt vlInt_byte = new VlInt(a); byte[] b = new byte[]{1,2,3,5}; assertArrayEquals(vlInt_byte.getVlIntBytes(),a); assertFalse(vlInt_byte.getVlIntBytes().equals(b)); } @Test public void getVlIntValue2Int() throws VlIntException { //vlint(int)的size小于4时,测试是否能准确转为int输出 int a = 222; int a2 = 22222222; byte[] b1 = new byte[]{0x00,0x7f, (byte) 0xff}; //32767 byte[] b2 = new byte[]{0x00,(byte) 0x80, 0x00}; //32768 byte[] b3 = new byte[]{0x00,(byte) 0xff, (byte) 0xff};//65535 VlInt vlInt_int1 = new VlInt(a); VlInt vlInt_int2 = new VlInt(a2); VlInt vlInt_b1 = new VlInt(b1); VlInt vlInt_b2 = new VlInt(b2); VlInt vlInt_b3 = new VlInt(b3); //测试size大小 assertTrue(vlInt_int1.size()<4); assertFalse(vlInt_int2.size()<4); //测试VlIntValue是否转为int assertEquals(vlInt_int1.getVlIntValue2Int(),a); assertEquals(vlInt_b1.getVlIntValue2Int(),32767); assertEquals(vlInt_b2.getVlIntValue2Int(),32768); assertEquals(vlInt_b3.getVlIntValue2Int(),65535); } //error:以bytes[]形式构造的VlInt,2^31-1 到 2^32-1之间的数会出错 @Test public void getVlIntValue2Long() throws VlIntException { long l1 = new Long(0); long l2 = new Long(1); long l3 = new Long(4294967295L); //2^32-1 byte[] b1 = new byte[]{0x00,0x7f,(byte) 0xff,(byte) 0xff,(byte) 0xff};//2^31-1 byte[] b2 = new byte[]{0x00,(byte) 0x80,0x00,0x00,0x00};//2^31 byte[] b3 = new byte[]{0,0,0,0,-128,0,0,0}; BigInteger bigInteger = new BigInteger(b2); System.out.println(bigInteger.longValue()); System.out.println(new BigInteger(b3).longValue()); VlInt vlInt_long1 = new VlInt(l1); // size = 1 VlInt vlInt_long2 = new VlInt(l2); // size = 1 VlInt vlInt_long3 = new VlInt(l3); // size = 5 VlInt vlInt_long4 = new VlInt(b1); VlInt vlInt_long5 = new VlInt(b2); System.out.println(vlInt_long2.toString()); System.out.println(vlInt_long3.toString()); System.out.println(vlInt_long4.toString()); System.out.println(vlInt_long5.toString()); //以VlInt(long)形式构造的VlInt assertEquals(vlInt_long1.getVlIntValue2Long(),l1); assertEquals(vlInt_long2.getVlIntValue2Long(),l2); assertEquals(vlInt_long3.getVlIntValue2Long(),l3); ///以VlInt(bytes[])形式构造的VlInt assertEquals(vlInt_long4.getVlIntValue2Long(),2147483647L);//2^31-1 assertEquals(vlInt_long5.getVlIntValue2Long(),2147483648L);//2^31 assertEquals(vlInt_long5.getVlIntValue2Long(),2147483648L);//2^31 byte[] bytes1 = new byte[]{127,-128,-128,-128,-128,-128,-128,-128}; VlInt vlInt_6 = new VlInt(new BigInteger("21474836481")); assertEquals(vlInt_6.getVlIntValue2Long(), new BigInteger("21474836481").longValue()); } //error:以bytes[]形式构造的VlInt,在大于2^31-1的数会出错 @Test public void getVlIntValue() { BigInteger b_0 = BigInteger.valueOf(2147483647L);//2^31-1 BigInteger b_1 = BigInteger.valueOf(2147483648L);//2^31 BigInteger b = BigInteger.valueOf(4294967296L);//2^32 //2^31-1 byte[] b0 = new byte[]{0x00,0x00,0x00,0x00,0x00,0x7f,(byte) 0xff,(byte) 0xff,(byte) 0xff};//2147483647 byte[] b1 = new byte[]{0x00,0x00,0x00,0x00,0x00,(byte) 0x80,0x00,0x00,0x00}; //2^32-1 byte[] b2 = new byte[]{0x00,0x00,0x00,0x00,0x00,(byte) 0xff,(byte) 0xff,(byte) 0xff,(byte) 0xff}; byte[] b3 = new byte[]{0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00};//2^32 VlInt vlInt_bigInteger = new VlInt(b); VlInt vlInt_b0 = new VlInt(b0); VlInt vlInt_b1 = new VlInt(b1); //以VlInt(BigInteger)形式构造的VlInt assertEquals(vlInt_bigInteger.getVlIntValue(),b); ///以VlInt(bytes[])形式构造的VlInt assertEquals(vlInt_b0.getVlIntValue(),b_0); assertEquals(vlInt_b1.getVlIntValue(),b_1); } @Test public void isEqual() { int a1 = 1; int a2 = 65535; int a2_test = 66666; long a3 = 4294967295L; long a4 = 4294967296L; VlInt vlInt1 = new VlInt(a1); //size = 1 VlInt vlInt_test = new VlInt(a1); VlInt vlInt2 = new VlInt(a2); //size = 3 VlInt vlInt3 = new VlInt(a3); //size = 5 VlInt vlInt4 = new VlInt(a4); //size = 9 assertTrue(vlInt1.isEqual(vlInt_test)); assertTrue(vlInt2.isEqual(a2)); assertFalse(vlInt2.isEqual(a2_test)); assertTrue(vlInt3.isEqual(a3)); assertTrue(vlInt4.isEqual(a4)); } @Test public void compareTo() {//小于:-1 大于:1 等于0 //param:vlInt VlInt vlInt1 = new VlInt(1); VlInt vlInt2 = new VlInt(1); VlInt vlInt3 = new VlInt(4294967296L); assertEquals(vlInt1.compareTo(vlInt2),0); assertEquals(vlInt1.compareTo(vlInt3),-1); //param:int int a1 = 1,a2 = 2; assertEquals(vlInt1.compareTo(a1),0); assertEquals(vlInt1.compareTo(a2),-1); //param:long long l1=4294967295L,l2=4294967296L; VlInt v = new VlInt(l2); assertEquals(v.compareTo(l1),1); assertEquals(v.compareTo(l2),0); } //todo:暂没考虑两者差、两者和 越界int最大值的问题 @Test public void subtract_add() { int a1 = 0,a2 = 65535; long l = 4294967296L; int s1 = a2-a1,s2 = a1-a2; long r1 = l-a1,r2 = l - (long)a2; //差值越界int最大值 VlInt vlInt1 = new VlInt(a1); VlInt vlInt2 = new VlInt(a2); VlInt vlInt3 = new VlInt(l); logger.debug(vlInt1.toString()); logger.debug(vlInt2.toString()); logger.debug(vlInt3.toString()); assertEquals(vlInt2.subtract(vlInt1),s1); assertEquals(vlInt1.subtract(vlInt2),s2); //差为负数 //error:差值越界int最大值 assertEquals(vlInt3.subtract(vlInt2), r2); int sum1 = a1+a2; long sum2 = a2+l; //param:VlInt assertEquals(vlInt1.add(vlInt2),sum1); assertEquals(vlInt2.add(vlInt3),sum2); //两者之和为long,越界int最大值 //param:int assertEquals(vlInt1.add(a2),sum1); } // @Test // public void bigInteger2vlintBytes(){ // BigInteger a1 = BigInteger.valueOf(0); // BigInteger a2 = BigInteger.valueOf(240); // BigInteger a3 = BigInteger.valueOf(242); // BigInteger a4 = BigInteger.valueOf(256); // BigInteger a5 = BigInteger.valueOf(65535); // BigInteger a6 = BigInteger.valueOf(65536); // BigInteger a7 = BigInteger.valueOf(4294967295L); // BigInteger a8 = BigInteger.valueOf(4294967296L); // byte[] b1 = new byte[]{0x00}; //0:size=1 // byte[] b2 = new byte[]{(byte)0xf0}; //240<241:size=1 // byte[] b3 = new byte[]{(byte)0xf2}; //242:size=3 // byte[] b4 =new byte[]{0x01, 0x00}; //256:size=3 // byte[] b5 = new byte[]{(byte) 0xff, (byte) 0xff};//65535<2^16:size=3 // byte[] b6 = new byte[]{0x00,0x01,0x00,0x00}; //65536:size=5 // //4294967295<2^32:size=5 // byte[] b7 = new byte[]{(byte) 0xff,(byte) 0xff,(byte) 0xff,(byte) 0xff}; // //4294967296>=2^32:size=9 // byte[] b8 = new byte[]{0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00}; // // // } // // @Test // public void vlintBytes2bigInteger(){ // // } }