|
1 | 1 | package ru.yandex.clickhouse.response.parser; |
2 | 2 |
|
3 | 3 | import java.sql.SQLException; |
4 | | - |
| 4 | +import org.testng.annotations.DataProvider; |
5 | 5 | import org.testng.annotations.Test; |
6 | 6 |
|
7 | 7 | import ru.yandex.clickhouse.response.ByteFragment; |
|
13 | 13 | import static org.testng.Assert.fail; |
14 | 14 |
|
15 | 15 | public class ClickHouseValueParserTest { |
| 16 | + /** |
| 17 | + * Generates test data for floats. |
| 18 | + */ |
| 19 | + @DataProvider(name = "float_test_data") |
| 20 | + public Object[][] floatTestData() { |
| 21 | + return new Object [][] { |
| 22 | + {"100.0", 100.0f}, |
| 23 | + {"NaN", Float.NaN}, |
| 24 | + {"Infinity", Float.POSITIVE_INFINITY}, |
| 25 | + {"+Infinity", Float.POSITIVE_INFINITY}, |
| 26 | + {"-Infinity", Float.NEGATIVE_INFINITY}, |
| 27 | + {"nan", Float.NaN}, |
| 28 | + {"inf", Float.POSITIVE_INFINITY}, |
| 29 | + {"+inf", Float.POSITIVE_INFINITY}, |
| 30 | + {"-inf", Float.NEGATIVE_INFINITY} |
| 31 | + }; |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Generates test data for doubles. |
| 36 | + */ |
| 37 | + @DataProvider(name = "double_test_data") |
| 38 | + public Object[][] doubleTestData() { |
| 39 | + return new Object [][] { |
| 40 | + {"100.0", 100.0d}, |
| 41 | + {"Infinity", Double.POSITIVE_INFINITY}, |
| 42 | + {"+Infinity", Double.POSITIVE_INFINITY}, |
| 43 | + {"-Infinity", Double.NEGATIVE_INFINITY}, |
| 44 | + {"nan", Double.NaN}, |
| 45 | + {"inf", Double.POSITIVE_INFINITY}, |
| 46 | + {"+inf", Double.POSITIVE_INFINITY}, |
| 47 | + {"-inf", Double.NEGATIVE_INFINITY} |
| 48 | + }; |
| 49 | + } |
16 | 50 |
|
17 | 51 | @Test(groups = "unit") |
18 | 52 | public void testParseInt() throws Exception { |
@@ -161,4 +195,65 @@ public void testParseBoolean() throws SQLException { |
161 | 195 | assertFalse(ClickHouseValueParser.parseBoolean(ByteFragment.fromString(" true"), columnInfo)); |
162 | 196 | } |
163 | 197 |
|
| 198 | + @Test (dataProvider = "float_test_data") |
| 199 | + public void testParseFloat(String byteFragmentString, Float expectedValue) throws SQLException { |
| 200 | + ClickHouseColumnInfo columnInfo = ClickHouseColumnInfo.parse("Float32", "columnName", null); |
| 201 | + float floatDelta = 0.001f; |
| 202 | + if (expectedValue.isNaN()) { |
| 203 | + assertTrue(Float.isNaN(ClickHouseValueParser.parseFloat( |
| 204 | + ByteFragment.fromString(byteFragmentString), columnInfo) |
| 205 | + )); |
| 206 | + } else { |
| 207 | + assertEquals(ClickHouseValueParser.parseFloat( |
| 208 | + ByteFragment.fromString(byteFragmentString), columnInfo), expectedValue, floatDelta |
| 209 | + ); |
| 210 | + } |
| 211 | + } |
| 212 | + |
| 213 | + @Test (dataProvider = "double_test_data") |
| 214 | + public void testParseDouble(String byteFragmentString, Double expectedValue) throws SQLException { |
| 215 | + ClickHouseColumnInfo columnInfo = ClickHouseColumnInfo.parse("Float64", "columnName", null); |
| 216 | + double doubleDelta = 0.001; |
| 217 | + if (expectedValue.isNaN()) { |
| 218 | + assertTrue(Double.isNaN(ClickHouseValueParser.parseDouble( |
| 219 | + ByteFragment.fromString(byteFragmentString), columnInfo) |
| 220 | + )); |
| 221 | + } else { |
| 222 | + assertEquals(ClickHouseValueParser.parseDouble( |
| 223 | + ByteFragment.fromString(byteFragmentString), columnInfo), expectedValue, doubleDelta |
| 224 | + ); |
| 225 | + } |
| 226 | + } |
| 227 | + |
| 228 | + @Test (dataProvider = "float_test_data") |
| 229 | + public void testGetParserFloat(String byteFragmentString, Float expectedValue) throws SQLException { |
| 230 | + ClickHouseColumnInfo columnInfo = ClickHouseColumnInfo.parse("Float32", "columnName", null); |
| 231 | + float floatDelta = 0.001f; |
| 232 | + |
| 233 | + if (expectedValue.isNaN()) { |
| 234 | + assertTrue(Float.isNaN(ClickHouseValueParser.getParser(Float.class).parse( |
| 235 | + ByteFragment.fromString(byteFragmentString), columnInfo, null) |
| 236 | + )); |
| 237 | + } else { |
| 238 | + assertEquals(ClickHouseValueParser.getParser(Float.class).parse( |
| 239 | + ByteFragment.fromString(byteFragmentString), columnInfo, null), expectedValue, floatDelta |
| 240 | + ); |
| 241 | + } |
| 242 | + } |
| 243 | + |
| 244 | + @Test (dataProvider = "double_test_data") |
| 245 | + public void testGetParserDouble(String byteFragmentString, Double expectedValue) throws SQLException { |
| 246 | + ClickHouseColumnInfo columnInfo = ClickHouseColumnInfo.parse("Float64", "columnName", null); |
| 247 | + double doubleDelta = 0.001d; |
| 248 | + |
| 249 | + if (expectedValue.isNaN()) { |
| 250 | + assertTrue(Double.isNaN(ClickHouseValueParser.getParser(Double.class).parse( |
| 251 | + ByteFragment.fromString(byteFragmentString), columnInfo, null) |
| 252 | + )); |
| 253 | + } else { |
| 254 | + assertEquals(ClickHouseValueParser.getParser(Double.class).parse( |
| 255 | + ByteFragment.fromString(byteFragmentString), columnInfo, null), expectedValue, doubleDelta |
| 256 | + ); |
| 257 | + } |
| 258 | + } |
164 | 259 | } |
0 commit comments