Skip to content

Commit

Permalink
Add more coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre Delaunay committed Apr 5, 2024
1 parent 6de4a03 commit c437fad
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 11 deletions.
18 changes: 7 additions & 11 deletions src/ast/values/value.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ struct Query {
};


using ValueDeleter = void(*)(Value);
using ValueDeleter = void(*)(Value&);

//
// Simple dynamic value that holds small value on the stack
Expand Down Expand Up @@ -169,8 +169,6 @@ struct Value {
return *as<T*>(error);
}

bool is_object() const { return tag >= int(meta::ValueTypes::Max); }

template <typename T>
static bool is_allocated() {
return !is_small<T>();
Expand Down Expand Up @@ -257,7 +255,7 @@ T const Getter<T>::get(Value const& v, GetterError& err) {
using NoPointer = std::remove_const_t<std::remove_pointer_t<NoConst>>;

if constexpr (std::is_reference<T>::value) {
return *v.as<NoConst*>(err);
return *v.as<NoConst const*>(err);
}
else {
err.failed = false;
Expand All @@ -267,6 +265,7 @@ T const Getter<T>::get(Value const& v, GetterError& err) {
return *ptr;
}

// is this possible ? we are returning a copy anyway
// Storing int*, we want int
if (v.tag == meta::type_id<NoConst*>()) {
NoConst const* const* ptr = v.pointer<NoConst const*>();
Expand All @@ -276,7 +275,7 @@ T const Getter<T>::get(Value const& v, GetterError& err) {
// Storing int, we want int*
if constexpr (std::is_pointer_v<NoConst>){
if (v.tag == meta::type_id<NoPointer>()) {
NoPointer const* ptr = v.pointer<NoPointer>();
NoPointer const* ptr = v.pointer<NoPointer const>();
return ptr;
}
}
Expand Down Expand Up @@ -457,16 +456,13 @@ struct Interop<R (O::*)(Args...) const> {

#define KIWI_WRAP(fun) Function(Interop<decltype((&fun))>::template wrapper<(&fun)>)

// ---
void free_value(Value val, void (*deleter)(void*) = nullptr);

// Specialization for C object that have a custom free
using FreeFun = void(*)(void*);


template <typename T, FreeFun free_fun = std::free>
struct _destructor {
static void free(Value v) {
static void free(Value& v) {
if (v.value.obj == nullptr) {
return;
}
Expand All @@ -488,7 +484,7 @@ struct _destructor {

template <FreeFun free_fun>
struct _custom_free {
static void free(Value v) {
static void free(Value& v) {
free_fun(v.value.obj);

// NOTE: this only nullify current value so other copy of this value
Expand Down Expand Up @@ -518,7 +514,7 @@ Tuple<Value, ValueDeleter> _new_object(int _typeid, Args... args) {
return std::make_tuple(Value(_typeid, memory), _destructor<T>::free);
}

inline void noop_destructor(Value v) {}
inline void noop_destructor(Value& v) {}

template <typename T, typename... Args>
Tuple<Value, ValueDeleter> _new_value(int _typeid, Args... args) {
Expand Down
99 changes: 99 additions & 0 deletions tests/value_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,104 @@ TEST_CASE("Value_ErrorHandling")
Value::reset_error();
}

TEST_CASE("Value_ErrorHandling_2")
{

#define CASE(T, TT) \
{ \
std::cout << #T << " " << meta::type_id<T>() << "\n"; \
REQUIRE(v.is_valid<T>() == false); \
v.as<T>(); \
REQUIRE(Value::has_error() == true); \
REQUIRE(Value::global_err.requested_type_id == meta::type_id<TT>()); \
REQUIRE(Value::global_err.value_type_id == meta::type_id<Rectangle>()); \
Value::reset_error(); \
}

{
#define TRY(X) \
X(int , int) \
X(int const , int const) \
X(int const*, int const*)

// X(int* const, int* const)
// X(int const&, int*)
// X(Rectangle*const*, Rectangle*const*) \
//

auto [vv, deleter] = make_value<Rectangle>(Point2D(1, 1), Point2D(2, 2));
Value const v = vv;
TRY(CASE)
deleter(vv);
#undef TRY
}
{
#define TRY(X) \
X(int , int) \
X(int* , int*) \
X(int& , int*) \
X(int const , int const) \
X(int const*, int const*) \
X(int const&, int*) \
X(Rectangle*const*, Rectangle*const*) \
X(int* const, int* const)

auto [vv, deleter] = make_value<Rectangle>(Point2D(1, 1), Point2D(2, 2));
Value v = vv;
TRY(CASE)
deleter(vv);
#undef TRY
}
#undef CASE

#define CASE(T, TT) \
{ \
std::cout << #T << " " << meta::type_id<T>() << "\n"; \
REQUIRE(v.is_valid<T>() == true); \
v.as<T>(); \
}

{
#define TRY(X) \
X(Rectangle , int) \
X(Rectangle const , int) \
X(Rectangle const*, int) \
X(Rectangle const&, int)

auto [vv, deleter] = make_value<Rectangle>(Point2D(1, 1), Point2D(2, 2));
Value const v = vv;
TRY(CASE)
deleter(vv);
#undef TRY
}
{
#define TRY(X) \
X(Rectangle , int) \
X(Rectangle* , int) \
X(Rectangle& , int) \
X(Rectangle const , int) \
X(Rectangle const*, int) \
X(Rectangle const&, int) \
X(Rectangle*const , int)


auto [vv, deleter] = make_value<Rectangle>(Point2D(1, 1), Point2D(2, 2));
Value v = vv;
TRY(CASE)
deleter(vv);
#undef TRY
}

#undef CASE
#undef TRY
}


// REQUIRE(Value::has_error() == false); \
// REQUIRE(Value::global_err.requested_type_id == meta::type_id<TT>()); \
// REQUIRE(Value::global_err.value_type_id == meta::type_id<Rectangle>()); \
// Value::reset_error(); \



Expand Down Expand Up @@ -387,4 +485,5 @@ TEST_CASE("Value_Array Wrapping") {
REQUIRE(Value::has_error() == false);

deleter(value);
deleter(value);
}

0 comments on commit c437fad

Please sign in to comment.