Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 4 additions & 11 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ endif (BUILD_ESP)

project(WARDuino VERSION 0.6.1)

list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake")

set(WARDUINO_VERSION_STRING "${PROJECT_VERSION}")
configure_file(src/config.h.in include/warduino/config.h)

Expand Down Expand Up @@ -104,16 +106,7 @@ if (BUILD_UNITTEST)
endif (CMAKE_COMPILER_IS_GNUCXX)


include(FetchContent)
FetchContent_Declare(
googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG release-1.12.1
)

# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)
include(Doctest)

set(PATH_TO_UNIT_TEST ${PROJECT_SOURCE_DIR}/tests/unit)

Expand All @@ -129,7 +122,7 @@ if (BUILD_UNITTEST)
get_filename_component(TEST_NAME ${TEST_FILE} NAME_WE)
message(DEBUG "Add executable for " ${TEST_FILE})
add_executable(${TEST_NAME} ${TEST_FILE} ${SOURCE_FILES} ${SHARED_SRC})
target_link_libraries(${TEST_NAME} gtest_main)
target_link_libraries(${TEST_NAME} PRIVATE doctest::doctest)
target_include_directories(${TEST_NAME} PRIVATE ${EXTERNAL_LIB_HEADERS} "${PROJECT_BINARY_DIR}/include")
add_test(${TEST_NAME} ${TEST_NAME})
endforeach ()
Expand Down
20 changes: 20 additions & 0 deletions cmake/Doctest.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
include_guard(GLOBAL)

find_package(doctest CONFIG QUIET)

if (NOT doctest_FOUND)
include(FetchContent)

FetchContent_Declare(
doctest
GIT_REPOSITORY https://github.com/doctest/doctest.git
GIT_TAG v2.4.12
)

FetchContent_MakeAvailable(doctest)
endif ()

if (NOT TARGET doctest::doctest)
message(FATAL_ERROR "Doctest target doctest::doctest was not found")
endif ()

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
65 changes: 65 additions & 0 deletions tests/unit/parsing.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include <doctest/doctest.h>

#include <cmath>

#include "../../src/Utils/util.h"

TEST_CASE("Test: leb128 unsigned encoding") {
SUBCASE("Decodes single-byte values") {
uint8_t bytes[] = {0x00, 0x7f};
uint8_t *pos = bytes;

CHECK(read_LEB(&pos, 32) == 0);
CHECK(pos == bytes + 1);

CHECK(read_LEB(&pos, 32) == 127);
CHECK(pos == bytes + 2);
}

SUBCASE("Decodes multi-byte values") {
uint8_t bytes[] = {0x80, 0x01, 0xe5, 0x8e, 0x26};
uint8_t *pos = bytes;

CHECK(read_LEB(&pos, 32) == 128);
CHECK(pos == bytes + 2);

// Example from the LEB128 specification: 624485 -> E5 8E 26
CHECK(read_LEB(&pos, 32) == 624485);
CHECK(pos == bytes + 5);
}
}

TEST_CASE("Test: leb128 signed encoding") {
SUBCASE("Decodes single-byte values") {
uint8_t bytes[] = {0x00, 0x7f};
uint8_t *pos = bytes;

CHECK(read_LEB_signed(&pos, 32) == 0);
CHECK(pos == bytes + 1);

CHECK(read_LEB_signed(&pos, 32) == -1);
CHECK(pos == bytes + 2);
}
}


TEST_CASE("Test: wasm args parsing") {
uint32_t params[] = {F32, F32};
uint32_t results[] = {I32};
Type function_type = {FUNC, 2, params, 1, results, 0};

// 01 00 80 7f -> NaN (0x7f800001), 00 00 80 7f -> +Infinity
uint8_t data[] = {0x01, 0x00, 0x80, 0x7f, 0x00, 0x00, 0x80, 0x7f};

StackValue *args = readWasmArgs(function_type, data);

REQUIRE(args != nullptr);
CHECK(args[0].value_type == F32);
CHECK(std::isnan(args[0].value.f32));

CHECK(args[1].value_type == F32);
CHECK(std::isinf(args[1].value.f32));

delete[] args;
}
45 changes: 45 additions & 0 deletions tests/unit/virtualaddresses.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include <doctest/doctest.h>

#include "../../src/Utils/util.h"

TEST_CASE("Test: to virtual address conversion") {
uint8_t wasm_bytes[] = {0xaa, 0xbb, 0xcc, 0xdd, 0xee};
Module module{};
module.bytes = wasm_bytes;
module.byte_count = sizeof(wasm_bytes);

SUBCASE("Start of module maps to zero") {
CHECK(toVirtualAddress(module.bytes, &module) == 0);
}

SUBCASE("Middle byte maps to its offset") {
CHECK(toVirtualAddress(module.bytes + 3, &module) == 3);
}

SUBCASE("End pointer maps to byte_count") {
CHECK(toVirtualAddress(module.bytes + module.byte_count, &module) ==
module.byte_count);
}
}

TEST_CASE("Test: to physical address conversion") {
uint8_t wasm_bytes[] = {0xaa, 0xbb, 0xcc, 0xdd, 0xee};
Module module{};
module.bytes = wasm_bytes;
module.byte_count = sizeof(wasm_bytes);

SUBCASE("Virtual zero maps to start pointer") {
CHECK(toPhysicalAddress(0, &module) == module.bytes);
}

SUBCASE("Virtual middle offset maps to expected physical pointer") {
CHECK(toPhysicalAddress(2, &module) == module.bytes + 2);
}

SUBCASE("Last valid virtual offset maps to final byte") {
CHECK(toPhysicalAddress(module.byte_count - 1, &module) ==
module.bytes + module.byte_count - 1);
}

}
Loading