|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +#include "gandiva/gdv_function_stubs.h" |
| 19 | + |
| 20 | +#include <gmock/gmock.h> |
| 21 | +#include <gtest/gtest.h> |
| 22 | + |
| 23 | +#include "gandiva/execution_context.h" |
| 24 | + |
| 25 | +namespace gandiva { |
| 26 | + |
| 27 | +TEST(TestGdvFnStubs, TestCastINT) { |
| 28 | + gandiva::ExecutionContext ctx; |
| 29 | + |
| 30 | + int64_t ctx_ptr = reinterpret_cast<int64_t>(&ctx); |
| 31 | + |
| 32 | + EXPECT_EQ(gdv_fn_castINT_utf8(ctx_ptr, "-45", 3), -45); |
| 33 | + EXPECT_EQ(gdv_fn_castINT_utf8(ctx_ptr, "0", 1), 0); |
| 34 | + EXPECT_EQ(gdv_fn_castINT_utf8(ctx_ptr, "2147483647", 10), 2147483647); |
| 35 | + EXPECT_EQ(gdv_fn_castINT_utf8(ctx_ptr, "02147483647", 11), 2147483647); |
| 36 | + EXPECT_EQ(gdv_fn_castINT_utf8(ctx_ptr, "-2147483648", 11), -2147483648LL); |
| 37 | + EXPECT_EQ(gdv_fn_castINT_utf8(ctx_ptr, "-02147483648", 12), -2147483648LL); |
| 38 | + EXPECT_EQ(gdv_fn_castINT_utf8(ctx_ptr, " 12 ", 4), 12); |
| 39 | + |
| 40 | + gdv_fn_castINT_utf8(ctx_ptr, "2147483648", 10); |
| 41 | + EXPECT_THAT(ctx.get_error(), |
| 42 | + ::testing::HasSubstr("Failed to cast the string 2147483648 to int32")); |
| 43 | + ctx.Reset(); |
| 44 | + |
| 45 | + gdv_fn_castINT_utf8(ctx_ptr, "-2147483649", 11); |
| 46 | + EXPECT_THAT(ctx.get_error(), |
| 47 | + ::testing::HasSubstr("Failed to cast the string -2147483649 to int32")); |
| 48 | + ctx.Reset(); |
| 49 | + |
| 50 | + gdv_fn_castINT_utf8(ctx_ptr, "12.34", 5); |
| 51 | + EXPECT_THAT(ctx.get_error(), |
| 52 | + ::testing::HasSubstr("Failed to cast the string 12.34 to int32")); |
| 53 | + ctx.Reset(); |
| 54 | + |
| 55 | + gdv_fn_castINT_utf8(ctx_ptr, "abc", 3); |
| 56 | + EXPECT_THAT(ctx.get_error(), |
| 57 | + ::testing::HasSubstr("Failed to cast the string abc to int32")); |
| 58 | + ctx.Reset(); |
| 59 | + |
| 60 | + gdv_fn_castINT_utf8(ctx_ptr, "", 0); |
| 61 | + EXPECT_THAT(ctx.get_error(), |
| 62 | + ::testing::HasSubstr("Failed to cast the string to int32")); |
| 63 | + ctx.Reset(); |
| 64 | + |
| 65 | + gdv_fn_castINT_utf8(ctx_ptr, "-", 1); |
| 66 | + EXPECT_THAT(ctx.get_error(), |
| 67 | + ::testing::HasSubstr("Failed to cast the string - to int32")); |
| 68 | + ctx.Reset(); |
| 69 | +} |
| 70 | + |
| 71 | +TEST(TestGdvFnStubs, TestCastBIGINT) { |
| 72 | + gandiva::ExecutionContext ctx; |
| 73 | + |
| 74 | + int64_t ctx_ptr = reinterpret_cast<int64_t>(&ctx); |
| 75 | + |
| 76 | + EXPECT_EQ(gdv_fn_castBIGINT_utf8(ctx_ptr, "-45", 3), -45); |
| 77 | + EXPECT_EQ(gdv_fn_castBIGINT_utf8(ctx_ptr, "0", 1), 0); |
| 78 | + EXPECT_EQ(gdv_fn_castBIGINT_utf8(ctx_ptr, "9223372036854775807", 19), |
| 79 | + 9223372036854775807LL); |
| 80 | + EXPECT_EQ(gdv_fn_castBIGINT_utf8(ctx_ptr, "09223372036854775807", 20), |
| 81 | + 9223372036854775807LL); |
| 82 | + EXPECT_EQ(gdv_fn_castBIGINT_utf8(ctx_ptr, "-9223372036854775808", 20), |
| 83 | + -9223372036854775807LL - 1); |
| 84 | + EXPECT_EQ(gdv_fn_castBIGINT_utf8(ctx_ptr, "-009223372036854775808", 22), |
| 85 | + -9223372036854775807LL - 1); |
| 86 | + EXPECT_EQ(gdv_fn_castBIGINT_utf8(ctx_ptr, " 12 ", 4), 12); |
| 87 | + |
| 88 | + gdv_fn_castBIGINT_utf8(ctx_ptr, "9223372036854775808", 19); |
| 89 | + EXPECT_THAT( |
| 90 | + ctx.get_error(), |
| 91 | + ::testing::HasSubstr("Failed to cast the string 9223372036854775808 to int64")); |
| 92 | + ctx.Reset(); |
| 93 | + |
| 94 | + gdv_fn_castBIGINT_utf8(ctx_ptr, "-9223372036854775809", 20); |
| 95 | + EXPECT_THAT( |
| 96 | + ctx.get_error(), |
| 97 | + ::testing::HasSubstr("Failed to cast the string -9223372036854775809 to int64")); |
| 98 | + ctx.Reset(); |
| 99 | + |
| 100 | + gdv_fn_castBIGINT_utf8(ctx_ptr, "12.34", 5); |
| 101 | + EXPECT_THAT(ctx.get_error(), |
| 102 | + ::testing::HasSubstr("Failed to cast the string 12.34 to int64")); |
| 103 | + ctx.Reset(); |
| 104 | + |
| 105 | + gdv_fn_castBIGINT_utf8(ctx_ptr, "abc", 3); |
| 106 | + EXPECT_THAT(ctx.get_error(), |
| 107 | + ::testing::HasSubstr("Failed to cast the string abc to int64")); |
| 108 | + ctx.Reset(); |
| 109 | + |
| 110 | + gdv_fn_castBIGINT_utf8(ctx_ptr, "", 0); |
| 111 | + EXPECT_THAT(ctx.get_error(), |
| 112 | + ::testing::HasSubstr("Failed to cast the string to int64")); |
| 113 | + ctx.Reset(); |
| 114 | + |
| 115 | + gdv_fn_castBIGINT_utf8(ctx_ptr, "-", 1); |
| 116 | + EXPECT_THAT(ctx.get_error(), |
| 117 | + ::testing::HasSubstr("Failed to cast the string - to int64")); |
| 118 | + ctx.Reset(); |
| 119 | +} |
| 120 | + |
| 121 | +TEST(TestGdvFnStubs, TestCastFloat4) { |
| 122 | + gandiva::ExecutionContext ctx; |
| 123 | + |
| 124 | + int64_t ctx_ptr = reinterpret_cast<int64_t>(&ctx); |
| 125 | + |
| 126 | + EXPECT_EQ(gdv_fn_castFLOAT4_utf8(ctx_ptr, "-45.34", 6), -45.34f); |
| 127 | + EXPECT_EQ(gdv_fn_castFLOAT4_utf8(ctx_ptr, "0", 1), 0.0f); |
| 128 | + EXPECT_EQ(gdv_fn_castFLOAT4_utf8(ctx_ptr, "5", 1), 5.0f); |
| 129 | + EXPECT_EQ(gdv_fn_castFLOAT4_utf8(ctx_ptr, " 3.4 ", 5), 3.4f); |
| 130 | + |
| 131 | + gdv_fn_castFLOAT4_utf8(ctx_ptr, "", 0); |
| 132 | + EXPECT_THAT(ctx.get_error(), |
| 133 | + ::testing::HasSubstr("Failed to cast the string to float")); |
| 134 | + ctx.Reset(); |
| 135 | + |
| 136 | + gdv_fn_castFLOAT4_utf8(ctx_ptr, "e", 1); |
| 137 | + EXPECT_THAT(ctx.get_error(), |
| 138 | + ::testing::HasSubstr("Failed to cast the string e to float")); |
| 139 | + ctx.Reset(); |
| 140 | +} |
| 141 | + |
| 142 | +TEST(TestGdvFnStubs, TestCastFloat8) { |
| 143 | + gandiva::ExecutionContext ctx; |
| 144 | + |
| 145 | + int64_t ctx_ptr = reinterpret_cast<int64_t>(&ctx); |
| 146 | + |
| 147 | + EXPECT_EQ(gdv_fn_castFLOAT8_utf8(ctx_ptr, "-45.34", 6), -45.34); |
| 148 | + EXPECT_EQ(gdv_fn_castFLOAT8_utf8(ctx_ptr, "0", 1), 0.0); |
| 149 | + EXPECT_EQ(gdv_fn_castFLOAT8_utf8(ctx_ptr, "5", 1), 5.0); |
| 150 | + EXPECT_EQ(gdv_fn_castFLOAT8_utf8(ctx_ptr, " 3.4 ", 5), 3.4); |
| 151 | + |
| 152 | + gdv_fn_castFLOAT8_utf8(ctx_ptr, "", 0); |
| 153 | + EXPECT_THAT(ctx.get_error(), |
| 154 | + ::testing::HasSubstr("Failed to cast the string to double")); |
| 155 | + ctx.Reset(); |
| 156 | + |
| 157 | + gdv_fn_castFLOAT8_utf8(ctx_ptr, "e", 1); |
| 158 | + EXPECT_THAT(ctx.get_error(), |
| 159 | + ::testing::HasSubstr("Failed to cast the string e to double")); |
| 160 | + ctx.Reset(); |
| 161 | +} |
| 162 | + |
| 163 | +} // namespace gandiva |
0 commit comments