@@ -1872,4 +1872,230 @@ TEST(TestStringOps, TestEltFunction) {
18721872 EXPECT_EQ (out_vality, true );
18731873}
18741874
1875+ TEST (TestStringOps, TestToHex) {
1876+ gandiva::ExecutionContext ctx;
1877+ uint64_t ctx_ptr = reinterpret_cast <int64_t >(&ctx);
1878+ int32_t out_len = 0 ;
1879+ int32_t in_len = 0 ;
1880+ const char * out_str;
1881+
1882+ in_len = 10 ;
1883+ char in_str[] = {0x54 , 0x65 , 0x73 , 0x74 , 0x53 , 0x74 , 0x72 , 0x69 , 0x6E , 0x67 };
1884+ out_str = to_hex_binary (ctx_ptr, in_str, in_len, &out_len);
1885+ std::string output = std::string (out_str, out_len);
1886+ EXPECT_EQ (out_len, 2 * in_len);
1887+ EXPECT_EQ (output, " 54657374537472696E67" );
1888+
1889+ in_len = 0 ;
1890+ out_str = to_hex_binary (ctx_ptr, " " , in_len, &out_len);
1891+ output = std::string (out_str, out_len);
1892+ EXPECT_EQ (out_len, 0 );
1893+ EXPECT_EQ (output, " " );
1894+
1895+ in_len = 1 ;
1896+ char in_str_one_char[] = {0x54 };
1897+ out_str = to_hex_binary (ctx_ptr, in_str_one_char, in_len, &out_len);
1898+ output = std::string (out_str, out_len);
1899+ EXPECT_EQ (out_len, 2 * in_len);
1900+ EXPECT_EQ (output, " 54" );
1901+
1902+ in_len = 16 ;
1903+ char in_str_spaces[] = {0x54 , 0x65 , 0x73 , 0x74 , 0x20 , 0x77 , 0x69 , 0x74 ,
1904+ 0x68 , 0x20 , 0x73 , 0x70 , 0x61 , 0x63 , 0x65 , 0x73 };
1905+ out_str = to_hex_binary (ctx_ptr, in_str_spaces, in_len, &out_len);
1906+ output = std::string (out_str, out_len);
1907+ EXPECT_EQ (output, " 54657374207769746820737061636573" );
1908+
1909+ in_len = 20 ;
1910+ char in_str_break_line[] = {0x54 , 0x65 , 0x78 , 0x74 , 0x20 , 0x77 , 0x69 , 0x74 , 0x68 , 0x0A ,
1911+ 0x62 , 0x72 , 0x65 , 0x61 , 0x6B , 0x20 , 0x6C , 0x69 , 0x6E , 0x65 };
1912+ out_str = to_hex_binary (ctx_ptr, in_str_break_line, in_len, &out_len);
1913+ output = std::string (out_str, out_len);
1914+ EXPECT_EQ (out_len, 2 * in_len);
1915+ EXPECT_EQ (output, " 5465787420776974680A627265616B206C696E65" );
1916+
1917+ in_len = 27 ;
1918+ char in_str_with_num[] = {0x54 , 0x65 , 0x73 , 0x74 , 0x20 , 0x77 , 0x69 , 0x74 , 0x68 ,
1919+ 0x20 , 0x6E , 0x75 , 0x6D , 0x62 , 0x65 , 0x72 , 0x73 , 0x20 ,
1920+ 0x31 , 0x20 , 0x2B , 0x20 , 0x31 , 0x20 , 0x3D , 0x20 , 0x32 };
1921+ out_str = to_hex_binary (ctx_ptr, in_str_with_num, in_len, &out_len);
1922+ output = std::string (out_str, out_len);
1923+ EXPECT_EQ (out_len, 2 * in_len);
1924+ EXPECT_EQ (output, " 546573742077697468206E756D626572732031202B2031203D2032" );
1925+
1926+ in_len = 22 ;
1927+ char in_str_with_tabs[] = {0x09 , 0x0A , 0x09 , 0x0A , 0x09 , 0x0A , 0x09 , 0x0A ,
1928+ 0x0A , 0x0A , 0x09 , 0x20 , 0x61 , 0x20 , 0x6C , 0x65 ,
1929+ 0x74 , 0x74 , 0x40 , 0x5D , 0x65 , 0x72 };
1930+ out_str = to_hex_binary (ctx_ptr, in_str_with_tabs, in_len, &out_len);
1931+ output = std::string (out_str, out_len);
1932+ EXPECT_EQ (out_len, 2 * in_len);
1933+ EXPECT_EQ (output, " 090A090A090A090A0A0A092061206C657474405D6572" );
1934+
1935+ in_len = 22 ;
1936+ const char * binary_string =
1937+ " \x09\x0A\x09\x0A\x09\x0A\x09\x0A\x0A\x0A\x09\x20\x61\x20\x6C\x65\x74\x74\x40\x5D "
1938+ " \x65\x72 " ;
1939+ out_str = to_hex_binary (ctx_ptr, binary_string, in_len, &out_len);
1940+ output = std::string (out_str, out_len);
1941+ EXPECT_EQ (out_len, 2 * in_len);
1942+ EXPECT_EQ (output, " 090A090A090A090A0A0A092061206C657474405D6572" );
1943+ }
1944+
1945+ TEST (TestStringOps, TestToHexInt64) {
1946+ gandiva::ExecutionContext ctx;
1947+ uint64_t ctx_ptr = reinterpret_cast <int64_t >(&ctx);
1948+ int32_t out_len = 0 ;
1949+ const char * out_str;
1950+
1951+ int64_t max_data = INT64_MAX ;
1952+ out_str = to_hex_int64 (ctx_ptr, max_data, &out_len);
1953+ std::string output = std::string (out_str, out_len);
1954+ EXPECT_FALSE (ctx.has_error ());
1955+ EXPECT_EQ (out_len, 16 );
1956+ EXPECT_EQ (output, " 7FFFFFFFFFFFFFFF" );
1957+ ctx.Reset ();
1958+
1959+ int64_t min_data = INT64_MIN ;
1960+ out_str = to_hex_int64 (ctx_ptr, min_data, &out_len);
1961+ output = std::string (out_str, out_len);
1962+ EXPECT_FALSE (ctx.has_error ());
1963+ EXPECT_EQ (out_len, 16 );
1964+ EXPECT_EQ (output, " 8000000000000000" );
1965+ ctx.Reset ();
1966+
1967+ int64_t zero_data = 0 ;
1968+ out_str = to_hex_int64 (ctx_ptr, zero_data, &out_len);
1969+ output = std::string (out_str, out_len);
1970+ EXPECT_FALSE (ctx.has_error ());
1971+ EXPECT_EQ (out_len, 1 );
1972+ EXPECT_EQ (output, " 0" );
1973+ ctx.Reset ();
1974+
1975+ int64_t minus_zero_data = -0 ;
1976+ out_str = to_hex_int64 (ctx_ptr, minus_zero_data, &out_len);
1977+ output = std::string (out_str, out_len);
1978+ EXPECT_FALSE (ctx.has_error ());
1979+ EXPECT_EQ (out_len, 1 );
1980+ EXPECT_EQ (output, " 0" );
1981+ ctx.Reset ();
1982+
1983+ int64_t minus_one_data = -1 ;
1984+ out_str = to_hex_int64 (ctx_ptr, minus_one_data, &out_len);
1985+ output = std::string (out_str, out_len);
1986+ EXPECT_FALSE (ctx.has_error ());
1987+ EXPECT_EQ (out_len, 16 );
1988+ EXPECT_EQ (output, " FFFFFFFFFFFFFFFF" );
1989+ ctx.Reset ();
1990+
1991+ int64_t one_data = 1 ;
1992+ out_str = to_hex_int64 (ctx_ptr, one_data, &out_len);
1993+ output = std::string (out_str, out_len);
1994+ EXPECT_FALSE (ctx.has_error ());
1995+ EXPECT_EQ (out_len, 1 );
1996+ EXPECT_EQ (output, " 1" );
1997+ ctx.Reset ();
1998+ }
1999+
2000+ TEST (TestStringOps, TestToHexInt32) {
2001+ gandiva::ExecutionContext ctx;
2002+ uint64_t ctx_ptr = reinterpret_cast <int64_t >(&ctx);
2003+ int32_t out_len = 0 ;
2004+ const char * out_str;
2005+
2006+ int32_t max_data = INT32_MAX ;
2007+ out_str = to_hex_int32 (ctx_ptr, max_data, &out_len);
2008+ std::string output = std::string (out_str, out_len);
2009+ EXPECT_FALSE (ctx.has_error ());
2010+ EXPECT_EQ (out_len, 8 );
2011+ EXPECT_EQ (output, " 7FFFFFFF" );
2012+ ctx.Reset ();
2013+
2014+ int32_t min_data = INT32_MIN ;
2015+ out_str = to_hex_int32 (ctx_ptr, min_data, &out_len);
2016+ output = std::string (out_str, out_len);
2017+ EXPECT_FALSE (ctx.has_error ());
2018+ EXPECT_EQ (out_len, 8 );
2019+ EXPECT_EQ (output, " 80000000" );
2020+ ctx.Reset ();
2021+
2022+ int32_t zero_data = 0 ;
2023+ out_str = to_hex_int32 (ctx_ptr, zero_data, &out_len);
2024+ output = std::string (out_str, out_len);
2025+ EXPECT_FALSE (ctx.has_error ());
2026+ EXPECT_EQ (out_len, 1 );
2027+ EXPECT_EQ (output, " 0" );
2028+ ctx.Reset ();
2029+
2030+ int32_t minus_zero_data = -0 ;
2031+ out_str = to_hex_int32 (ctx_ptr, minus_zero_data, &out_len);
2032+ output = std::string (out_str, out_len);
2033+ EXPECT_FALSE (ctx.has_error ());
2034+ EXPECT_EQ (out_len, 1 );
2035+ EXPECT_EQ (output, " 0" );
2036+ ctx.Reset ();
2037+
2038+ int32_t minus_one_data = -1 ;
2039+ out_str = to_hex_int32 (ctx_ptr, minus_one_data, &out_len);
2040+ output = std::string (out_str, out_len);
2041+ EXPECT_FALSE (ctx.has_error ());
2042+ EXPECT_EQ (out_len, 8 );
2043+ EXPECT_EQ (output, " FFFFFFFF" );
2044+ ctx.Reset ();
2045+
2046+ int32_t one_data = 1 ;
2047+ out_str = to_hex_int32 (ctx_ptr, one_data, &out_len);
2048+ output = std::string (out_str, out_len);
2049+ EXPECT_FALSE (ctx.has_error ());
2050+ EXPECT_EQ (out_len, 1 );
2051+ EXPECT_EQ (output, " 1" );
2052+ ctx.Reset ();
2053+ }
2054+
2055+ TEST (TestStringOps, TestFromHex) {
2056+ gandiva::ExecutionContext ctx;
2057+ uint64_t ctx_ptr = reinterpret_cast <gdv_int64>(&ctx);
2058+ gdv_int32 out_len = 0 ;
2059+ const char * out_str;
2060+
2061+ out_str = from_hex_utf8 (ctx_ptr, " 414243" , 6 , &out_len);
2062+ std::string output = std::string (out_str, out_len);
2063+ EXPECT_EQ (output, " ABC" );
2064+
2065+ out_str = from_hex_utf8 (ctx_ptr, " " , 0 , &out_len);
2066+ output = std::string (out_str, out_len);
2067+ EXPECT_EQ (output, " " );
2068+
2069+ out_str = from_hex_utf8 (ctx_ptr, " 41" , 2 , &out_len);
2070+ output = std::string (out_str, out_len);
2071+ EXPECT_EQ (output, " A" );
2072+
2073+ out_str = from_hex_utf8 (ctx_ptr, " 6d6D" , 4 , &out_len);
2074+ output = std::string (out_str, out_len);
2075+ EXPECT_EQ (output, " mm" );
2076+
2077+ out_str = from_hex_utf8 (ctx_ptr, " 6f6d" , 4 , &out_len);
2078+ output = std::string (out_str, out_len);
2079+ EXPECT_EQ (output, " om" );
2080+
2081+ out_str = from_hex_utf8 (ctx_ptr, " 4f4D" , 4 , &out_len);
2082+ output = std::string (out_str, out_len);
2083+ EXPECT_EQ (output, " OM" );
2084+
2085+ out_str = from_hex_utf8 (ctx_ptr, " T" , 1 , &out_len);
2086+ output = std::string (out_str, out_len);
2087+ EXPECT_EQ (output, " " );
2088+ EXPECT_THAT (
2089+ ctx.get_error (),
2090+ ::testing::HasSubstr (" Error parsing hex string, length was not a multiple of" ));
2091+ ctx.Reset ();
2092+
2093+ out_str = from_hex_utf8 (ctx_ptr, " \\ x41\\ x42\\ x43" , 12 , &out_len);
2094+ output = std::string (out_str, out_len);
2095+ EXPECT_EQ (output, " " );
2096+ EXPECT_THAT (
2097+ ctx.get_error (),
2098+ ::testing::HasSubstr (" Error parsing hex string, one or more bytes are not valid." ));
2099+ ctx.Reset ();
2100+ }
18752101} // namespace gandiva
0 commit comments