1212import java .io .InputStream ;
1313import java .math .BigDecimal ;
1414import java .math .BigInteger ;
15+ import java .util .Arrays ;
1516import java .util .TimeZone ;
1617import java .util .function .BiConsumer ;
18+ import java .util .function .Consumer ;
1719
1820public class ClickHouseBinaryFormatReaderTest {
21+
1922 @ Test
2023 public void testReadingNumbers () throws IOException {
2124 ByteArrayOutputStream out = new ByteArrayOutputStream ();
2225
23- String [] names = new String []{"a" , "b" , "c" , "d" , "e" , "f" , "g" , "h" , "i" , "j" };
24- String [] types = new String []{"Int8" , "Int16" , "Int32" , "Int64" , "UInt8" , "UInt16" , "UInt32" , "UInt64" , "Float32" , "Float64" };
26+ String [] names = new String []{ "a" , "b" , "c" , "d" , "e" , "f" , "g" , "h" , "i" , "j" , "k" , "l" , "m" , "n" , "o" , "p" ,
27+ "q" , "r" };
28+ String [] types = new String []{"Int8" , "Int16" , "Int32" , "Int64" , "UInt8" , "UInt16" , "UInt32" , "UInt64" ,
29+ "Int128" , "Int256" , "UInt128" , "UInt256" , "Float32" , "Float64" ,
30+ "Decimal32(3)" , "Decimal64(3)" , "Decimal128(4)" , "Decimal256(4)" };
31+
2532
2633 BinaryStreamUtils .writeVarInt (out , names .length );
2734 for (String name : names ) {
@@ -31,17 +38,25 @@ public void testReadingNumbers() throws IOException {
3138 BinaryStreamUtils .writeString (out , type );
3239 }
3340
34- BinaryStreamUtils .writeInt8 (out , 1 );
35- BinaryStreamUtils .writeInt16 (out , 2 );
36- BinaryStreamUtils .writeInt32 (out , 3 );
37- BinaryStreamUtils .writeInt64 (out , 4 );
38- BinaryStreamUtils .writeUnsignedInt8 (out , 5 );
39- BinaryStreamUtils .writeUnsignedInt16 (out , 6 );
40- BinaryStreamUtils .writeUnsignedInt32 (out , 7 );
41- BinaryStreamUtils .writeUnsignedInt64 (out , 8 );
42- BinaryStreamUtils .writeFloat32 (out , 9.0f );
43- BinaryStreamUtils .writeFloat64 (out , 10.0 );
44-
41+ final int testValue = 120 ;
42+ BinaryStreamUtils .writeInt8 (out , testValue );
43+ BinaryStreamUtils .writeInt16 (out , testValue );
44+ BinaryStreamUtils .writeInt32 (out , testValue );
45+ BinaryStreamUtils .writeInt64 (out , testValue );
46+ BinaryStreamUtils .writeUnsignedInt8 (out , testValue );
47+ BinaryStreamUtils .writeUnsignedInt16 (out , testValue );
48+ BinaryStreamUtils .writeUnsignedInt32 (out , testValue );
49+ BinaryStreamUtils .writeUnsignedInt64 (out , testValue );
50+ BinaryStreamUtils .writeInt128 ( out , new BigInteger (String .valueOf (testValue )));
51+ BinaryStreamUtils .writeInt256 ( out , new BigInteger (String .valueOf (testValue )));
52+ BinaryStreamUtils .writeUnsignedInt128 (out , new BigInteger (String .valueOf (testValue )));
53+ BinaryStreamUtils .writeUnsignedInt256 (out , new BigInteger (String .valueOf (testValue )));
54+ BinaryStreamUtils .writeFloat32 (out , testValue );
55+ BinaryStreamUtils .writeFloat64 (out , testValue );
56+ BinaryStreamUtils .writeDecimal32 (out , BigDecimal .valueOf (testValue ), 3 );
57+ BinaryStreamUtils .writeDecimal64 (out , BigDecimal .valueOf (testValue ), 3 );
58+ BinaryStreamUtils .writeDecimal128 (out , BigDecimal .valueOf (testValue ), 4 );
59+ BinaryStreamUtils .writeDecimal256 (out , BigDecimal .valueOf (testValue ), 4 );
4560
4661 InputStream in = new ByteArrayInputStream (out .toByteArray ());
4762 QuerySettings querySettings = new QuerySettings ().setUseTimeZone (TimeZone .getTimeZone ("UTC" ).toZoneId ().getId ());
@@ -53,46 +68,28 @@ public void testReadingNumbers() throws IOException {
5368 for (int i = 0 ; i < names .length ; i ++) {
5469 String name = names [i ];
5570 Assert .assertEquals (reader .getBoolean (name ), Boolean .TRUE );
71+ Assert .assertEquals (reader .getByte (name ), (byte )testValue );
72+ Assert .assertEquals (reader .getShort (name ), (short )testValue );
73+ Assert .assertEquals (reader .getInteger (name ), (int )testValue );
74+ Assert .assertEquals (reader .getLong (name ), (long )testValue );
5675
57- if (types [i ].equalsIgnoreCase ("int8" )) {
58- Assert .assertEquals (reader .getByte (name ), (i + 1 ));
59- }
60- if (types [i ].equalsIgnoreCase ("int8" ) || types [i ].equalsIgnoreCase ("int16" )) {
61- Assert .assertEquals (reader .getShort (name ), (i + 1 ));
62- }
63-
64- if (types [i ].equalsIgnoreCase ("int8" ) || types [i ].equalsIgnoreCase ("int16" ) || types [i ].equalsIgnoreCase ("int32" )) {
65- Assert .assertEquals (reader .getInteger (name ), (i + 1 ));
66- }
67-
68- if (types [i ].equalsIgnoreCase ("int8" ) || types [i ].equalsIgnoreCase ("int16" ) || types [i ].equalsIgnoreCase ("int32" ) || types [i ].equalsIgnoreCase ("int64" )) {
69- Assert .assertEquals (reader .getLong (name ), (i + 1 ));
70- }
71-
72- if (types [i ].equalsIgnoreCase ("int8" ) || types [i ].equalsIgnoreCase ("int16" ) || types [i ].equalsIgnoreCase ("int32" ) || types [i ].equalsIgnoreCase ("int64" ) || types [i ].equalsIgnoreCase ("uint8" )) {
73- Assert .assertEquals (reader .getFloat (name ), (i + 1 ));
74- }
76+ Assert .assertEquals (reader .getFloat (name ), (float ) testValue );
77+ Assert .assertEquals (reader .getDouble (name ), (double ) testValue );
78+ Assert .assertEquals (reader .getBigInteger (name ), BigInteger .valueOf ((testValue )));
7579
76- if (types [i ].equalsIgnoreCase ("int8" ) || types [i ].equalsIgnoreCase ("int16" ) || types [i ].equalsIgnoreCase ("int32" ) || types [i ].equalsIgnoreCase ("int64" ) || types [i ].equalsIgnoreCase ("uint8" ) || types [i ].equalsIgnoreCase ("uint16" )) {
77- Assert .assertEquals (reader .getDouble (name ), (i + 1 ));
78- }
79-
80- if (types [i ].equalsIgnoreCase ("int8" ) || types [i ].equalsIgnoreCase ("int16" ) || types [i ].equalsIgnoreCase ("int32" ) || types [i ].equalsIgnoreCase ("int64" ) || types [i ].equalsIgnoreCase ("uint8" ) || types [i ].equalsIgnoreCase ("uint16" ) || types [i ].equalsIgnoreCase ("uint32" )) {
81- Assert .assertEquals (reader .getBigInteger (name ), BigInteger .valueOf ((i + 1 )));
82- }
83-
84- if (types [i ].equalsIgnoreCase ("int8" ) || types [i ].equalsIgnoreCase ("int16" ) || types [i ].equalsIgnoreCase ("int32" ) || types [i ].equalsIgnoreCase ("int64" ) || types [i ].equalsIgnoreCase ("uint8" ) || types [i ].equalsIgnoreCase ("uint16" ) || types [i ].equalsIgnoreCase ("uint32" ) || types [i ].equalsIgnoreCase ("uint64" )) {
85- Assert .assertTrue (reader .getBigDecimal (name ).compareTo (BigDecimal .valueOf ((i + 1.0f ))) == 0 );
86- }
80+ Assert .assertTrue (reader .getBigDecimal (name ).compareTo (BigDecimal .valueOf ((testValue ))) == 0 );
8781 }
8882 }
8983
9084 @ Test
9185 public void testReadingNumbersWithOverflow () throws IOException {
9286 ByteArrayOutputStream out = new ByteArrayOutputStream ();
9387
94- String [] names = new String []{"a" , "b" , "c" , "d" , "e" , "f" , "g" , "h" , "i" , "j" };
95- String [] types = new String []{"Int8" , "Int16" , "Int32" , "Int64" , "UInt8" , "UInt16" , "UInt32" , "UInt64" , "Float32" , "Float64" };
88+ String [] names = new String []{ "a" , "b" , "c" , "d" , "e" , "f" , "g" , "h" , "i" , "j" , "k" , "l" , "m" , "n" , "o" , "p" ,
89+ "q" , "r" };
90+ String [] types = new String []{"Int8" , "Int16" , "Int32" , "Int64" , "UInt8" , "UInt16" , "UInt32" , "UInt64" ,
91+ "Int128" , "Int256" , "UInt128" , "UInt256" , "Float32" , "Float64" ,
92+ "Decimal32(3)" , "Decimal64(3)" , "Decimal128(4)" , "Decimal256(4)" };
9693
9794 BinaryStreamUtils .writeVarInt (out , names .length );
9895 for (String name : names ) {
@@ -102,16 +99,24 @@ public void testReadingNumbersWithOverflow() throws IOException {
10299 BinaryStreamUtils .writeString (out , type );
103100 }
104101
105- BinaryStreamUtils .writeInt8 (out , 127 ); // Max value for Int8
106- BinaryStreamUtils .writeInt16 (out , 2000 );
107- BinaryStreamUtils .writeInt32 (out , 300000 );
108- BinaryStreamUtils .writeInt64 (out , 4000000000L );
109- BinaryStreamUtils .writeUnsignedInt8 (out , 255 );
110- BinaryStreamUtils .writeUnsignedInt16 (out , 60000 );
111- BinaryStreamUtils .writeUnsignedInt32 (out , 4000000000L );
112- BinaryStreamUtils .writeUnsignedInt64 (out , new BigInteger ("18000044073709551615" ));
113- BinaryStreamUtils .writeFloat32 (out , 9.0f );
114- BinaryStreamUtils .writeFloat64 (out , 10.0 );
102+ BinaryStreamUtils .writeInt8 (out , 127 ); // a
103+ BinaryStreamUtils .writeInt16 (out , 2000 ); // b
104+ BinaryStreamUtils .writeInt32 (out , 300000 ); // c
105+ BinaryStreamUtils .writeInt64 (out , 4000000000L ); // d
106+ BinaryStreamUtils .writeUnsignedInt8 (out , 255 ); // e
107+ BinaryStreamUtils .writeUnsignedInt16 (out , 60000 ); // f
108+ BinaryStreamUtils .writeUnsignedInt32 (out , 4000000000L ); // g
109+ BinaryStreamUtils .writeUnsignedInt64 (out , new BigInteger ("18000044073709551615" )); // h
110+ BinaryStreamUtils .writeInt128 (out , new BigInteger ("18000044073709551615" )); // i
111+ BinaryStreamUtils .writeInt256 (out , new BigInteger ("18000044073709551615" )); // j
112+ BinaryStreamUtils .writeUnsignedInt128 (out , new BigInteger ("18000044073709551615" )); // k
113+ BinaryStreamUtils .writeUnsignedInt256 (out , new BigInteger ("18000044073709551615" )); // l
114+ BinaryStreamUtils .writeFloat32 (out , 900000.123f ); // m
115+ BinaryStreamUtils .writeFloat64 (out , 1000000.333 ); // n
116+ BinaryStreamUtils .writeDecimal32 (out , BigDecimal .valueOf (100000 ), 3 ); // o
117+ BinaryStreamUtils .writeDecimal64 (out , BigDecimal .valueOf (10000000.10000 ), 3 ); // p
118+ BinaryStreamUtils .writeDecimal128 (out , BigDecimal .valueOf (1000000000.1000000 ), 4 ); // q
119+ BinaryStreamUtils .writeDecimal256 (out , BigDecimal .valueOf (1000000000.1000000 ), 4 ); // r
115120
116121
117122 InputStream in = new ByteArrayInputStream (out .toByteArray ());
@@ -121,25 +126,18 @@ public void testReadingNumbersWithOverflow() throws IOException {
121126
122127 reader .next ();
123128
124- for (int i = 0 ; i < names .length ; i ++) {
125- String name = names [i ];
126- String type = types [i ];
127-
128- if (type .equalsIgnoreCase ("int16" )) {
129- Assert .expectThrows (ArithmeticException .class , () -> reader .getByte (name )) ;
130- } else if (type .equalsIgnoreCase ("int32" )) {
131- Assert .expectThrows (ArithmeticException .class , () -> reader .getShort (name )) ;
132- } else if (type .equalsIgnoreCase ("int64" )) {
133- Assert .expectThrows (ArithmeticException .class , () -> reader .getInteger (name )) ;
134- } else if (type .equalsIgnoreCase ("uint8" )) {
135- Assert .expectThrows (ArithmeticException .class , () -> reader .getByte (name )) ;
136- } else if (type .equalsIgnoreCase ("uint16" )) {
137- Assert .expectThrows (ArithmeticException .class , () -> reader .getShort (name )) ;
138- } else if (type .equalsIgnoreCase ("uint32" )) {
139- Assert .expectThrows (ArithmeticException .class , () -> reader .getInteger (name )) ;
140- } else if (type .equalsIgnoreCase ("uint64" )) {
141- Assert .expectThrows (ArithmeticException .class , () -> reader .getLong (name )) ;
142- }
143- }
129+ Consumer <String > byteConsumer = name -> Assert .expectThrows (ArithmeticException .class , () -> reader .getByte (name ));
130+ Consumer <String > shortConsumer = name -> Assert .expectThrows (ArithmeticException .class , () -> reader .getShort (name ));
131+ Consumer <String > integerConsumer = name -> Assert .expectThrows (ArithmeticException .class , () -> reader .getInteger (name ));
132+ Consumer <String > longConsumer = name -> Assert .expectThrows (ArithmeticException .class , () -> reader .getLong (name ));
133+ Consumer <String > floatConsumer = name -> Assert .expectThrows (ArithmeticException .class , () -> reader .getFloat (name ));
134+ Consumer <String > doubleConsumer = name -> Assert .expectThrows (ArithmeticException .class , () -> reader .getDouble (name ));
135+
136+ Arrays .stream ("b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r" .split ("," )).forEach (byteConsumer );
137+ Arrays .stream ("c,d,f,g,h,i,j,k,l,m,n,o,p,q,r" .split ("," )).forEach (shortConsumer );
138+ Arrays .stream ("d,g,h,i,j,k,l" .split ("," )).forEach (integerConsumer );
139+ Arrays .stream ("h,i,j,k,l" .split ("," )).forEach (longConsumer );
140+ Arrays .stream ("h,i,j,k,l,n,p,q,r" .split ("," )).forEach (floatConsumer );
141+ Arrays .stream ("h,i,j,k,l,p,q,r" .split ("," )).forEach (doubleConsumer );
144142 }
145143}
0 commit comments