#include <gtest/gtest.h>
#include <torch/csrc/stable/c/shim.h>
#include <torch/csrc/stable/stableivalue_conversions.h>
#include <limits>
TEST(TorchStableIValue, TestStableIValueUse) {
StableIValue* a = nullptr;
ASSERT_EQ(torch_new_stable_ivalue(&a), AOTI_TORCH_SUCCESS);
ASSERT_NE(a, nullptr);
*a = std::numeric_limits<StableIValue>::max();
EXPECT_EQ(torch_delete_stable_ivalue(a), AOTI_TORCH_SUCCESS);
StableIValue* b = nullptr;
ASSERT_EQ(torch_delete_stable_ivalue(b), AOTI_TORCH_FAILURE);
StableIValue** ret_value = nullptr;
ASSERT_EQ(torch_new_stable_ivalue(ret_value), AOTI_TORCH_FAILURE);
}
TEST(TorchStableIValue, TestStableIValueLargeAmount) {
StableIValue* v = nullptr;
const size_t total_allocation_bytes = 10'000'000;
const size_t number_of_allocations =
total_allocation_bytes / sizeof(StableIValue);
for (size_t i = 0; i < number_of_allocations; i++) {
ASSERT_EQ(torch_new_stable_ivalue(&v), AOTI_TORCH_SUCCESS);
ASSERT_NE(v, nullptr);
ASSERT_EQ(torch_delete_stable_ivalue(v), AOTI_TORCH_SUCCESS);
v = nullptr;
}
}
TEST(TorchStableIValue, TestStringRoundtrip) {
const std::string our_string = "Hello World!";
const StableIValue as_stable_ivalue = torch::stable::detail::from(our_string);
const std::string string_back =
torch::stable::detail::to<std::string>(as_stable_ivalue);
ASSERT_EQ(string_back, our_string);
}