Repository: apache/arrow
File: cpp/src/gandiva/precompiled/string_ops_test.cc (C++)

Code before:

  const char* out_str = repeat_utf8_int32(ctx_ptr, "abc", 3, 2, &out_len);
  EXPECT_EQ(std::string(out_str, out_len), "abcabc");
  EXPECT_FALSE(ctx.has_error());

  out_str = repeat_utf8_int32(ctx_ptr, "a", 1, 5, &out_len);
  EXPECT_EQ(std::string(out_str, out_len), "aaaaa");
  EXPECT_FALSE(ctx.has_error());

  out_str = repeat_utf8_int32(ctx_ptr, "", 0, 10, &out_len);
  EXPECT_EQ(std::string(out_str, out_len), "");
  EXPECT_FALSE(ctx.has_error());

  out_str = repeat_utf8_int32(ctx_ptr, "", -20, 10, &out_len);
  EXPECT_EQ(std::string(out_str, out_len), "");
  EXPECT_FALSE(ctx.has_error());

  out_str = repeat_utf8_int32(ctx_ptr, "a", 1, -10, &out_len);
  EXPECT_EQ(std::string(out_str, out_len), "");
  EXPECT_THAT(ctx.get_error(), ::testing::HasSubstr("Repeat number can't be negative"));
  ctx.Reset();

  out_str = repeat_utf8_int32(ctx_ptr, "aa", 2,
                              std::numeric_limits<int32_t>::max() / 2 + 1, &out_len);
  EXPECT_EQ(std::string(out_str, out_len), "");
  EXPECT_THAT(ctx.get_error(), ::testing::HasSubstr("Would overflow maximum output size"));
  ctx.Reset();
}

TEST(TestStringOps, TestCastBoolToVarchar) {
  gandiva::ExecutionContext ctx;
  uint64_t ctx_ptr = reinterpret_cast<gdv_int64>(&ctx);
  gdv_int32 out_len = 0;

  const char* out_str = castVARCHAR_bool_int64(ctx_ptr, true, 2, &out_len);
  EXPECT_EQ(std::string(out_str, out_len), "tr");
  EXPECT_FALSE(ctx.has_error());

  out_str = castVARCHAR_bool_int64(ctx_ptr, true, 7, &out_len);
  EXPECT_EQ(std::string(out_str, out_len), "true");
  EXPECT_FALSE(ctx.has_error());

  out_str = castVARCHAR_bool_int64(ctx_ptr, false, 4, &out_len);
  EXPECT_EQ(std::string(out_str, out_len), "fals");
  EXPECT_FALSE(ctx.has_error());

  out_str = castVARCHAR_bool_int64(ctx_ptr, false, 5, &out_len);
  EXPECT_EQ(std::string(out_str, out_len), "false");
  EXPECT_FALSE(ctx.has_error());

  castVARCHAR_bool_int64(ctx_ptr, true, -3, &out_len);

Review comment (bug):
```suggestion
  EXPECT_THAT(ctx.get_error(),
              ::testing::HasSubstr("Would overflow maximum output size"));
```

Code after:

  const char* out_str = repeat_utf8_int32(ctx_ptr, "abc", 3, 2, &out_len);
  EXPECT_EQ(std::string(out_str, out_len), "abcabc");
  EXPECT_FALSE(ctx.has_error());

  out_str = repeat_utf8_int32(ctx_ptr, "a", 1, 5, &out_len);
  EXPECT_EQ(std::string(out_str, out_len), "aaaaa");
  EXPECT_FALSE(ctx.has_error());

  out_str = repeat_utf8_int32(ctx_ptr, "", 0, 10, &out_len);
  EXPECT_EQ(std::string(out_str, out_len), "");
  EXPECT_FALSE(ctx.has_error());

  out_str = repeat_utf8_int32(ctx_ptr, "", -20, 10, &out_len);
  EXPECT_EQ(std::string(out_str, out_len), "");
  EXPECT_FALSE(ctx.has_error());

  out_str = repeat_utf8_int32(ctx_ptr, "a", 1, -10, &out_len);
  EXPECT_EQ(std::string(out_str, out_len), "");
  EXPECT_THAT(ctx.get_error(), ::testing::HasSubstr("Repeat number can't be negative"));
  ctx.Reset();

  out_str = repeat_utf8_int32(ctx_ptr, "aa", 2,
                              std::numeric_limits<int32_t>::max() / 2 + 1, &out_len);
  EXPECT_EQ(std::string(out_str, out_len), "");
  EXPECT_THAT(ctx.get_error(),
              ::testing::HasSubstr("Would overflow maximum output size"));
  ctx.Reset();
}

TEST(TestStringOps, TestCastBoolToVarchar) {
  gandiva::ExecutionContext ctx;
  uint64_t ctx_ptr = reinterpret_cast<gdv_int64>(&ctx);
  gdv_int32 out_len = 0;

  const char* out_str = castVARCHAR_bool_int64(ctx_ptr, true, 2, &out_len);
  EXPECT_EQ(std::string(out_str, out_len), "tr");
  EXPECT_FALSE(ctx.has_error());

  out_str = castVARCHAR_bool_int64(ctx_ptr, true, 7, &out_len);
  EXPECT_EQ(std::string(out_str, out_len), "true");
  EXPECT_FALSE(ctx.has_error());

  out_str = castVARCHAR_bool_int64(ctx_ptr, false, 4, &out_len);
  EXPECT_EQ(std::string(out_str, out_len), "fals");
  EXPECT_FALSE(ctx.has_error());

  out_str = castVARCHAR_bool_int64(ctx_ptr, false, 5, &out_len);
  EXPECT_EQ(std::string(out_str, out_len), "false");
  EXPECT_FALSE(ctx.has_error());