diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 21b54e6..b37cfbe 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -16,9 +16,9 @@ jobs: with: submodules: true - name: Configure - run: mkdir build && cd build && cmake .. + run: mkdir build && cd build && cmake -DCMAKE_BUILD_TYPE=DEBUG .. - name: Build run: cmake --build build - # - name: Test - # run: cd build && ./unit_test + - name: Test + run: cd build && ctest --verbose diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..e1434da --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "picotest"] + path = picotest + url = https://github.com/h2o/picotest.git diff --git a/CMakeLists.txt b/CMakeLists.txt index d61a30d..b77c112 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,9 +1,15 @@ cmake_minimum_required(VERSION 3.1) -project(tlp) +project(tlp C) + +IF(NOT CMAKE_BUILD_TYPE) + SET(CMAKE_BUILD_TYPE Release) +ENDIF() set(CMAKE_C_FLAGS "-Wall -O -g") +find_package(Threads REQUIRED) + # libtlp add_library(tlp STATIC lib/libtlp.c) target_include_directories(tlp PUBLIC include) @@ -11,19 +17,19 @@ install(TARGETS tlp DESTINATION /usr/local/lib) install(FILES include/libtlp.h include/tlp.h DESTINATION /usr/local/include) # test -add_executable(test_dma_read test/test_dma_read.c) +add_executable(test_dma_read snippet/test_dma_read.c) target_link_libraries(test_dma_read tlp) -add_executable(test_dma_write test/test_dma_write.c) +add_executable(test_dma_write snippet/test_dma_write.c) target_link_libraries(test_dma_write tlp) -add_executable(test_msg_bar4 test/test_msg_bar4.c) +add_executable(test_msg_bar4 snippet/test_msg_bar4.c) target_link_libraries(test_msg_bar4 tlp) -add_executable(test_msg_msix test/test_msg_msix.c) +add_executable(test_msg_msix snippet/test_msg_msix.c) target_link_libraries(test_msg_msix tlp) -add_executable(test_msg_devid test/test_msg_devid.c) +add_executable(test_msg_devid snippet/test_msg_devid.c) target_link_libraries(test_msg_devid tlp) # apps @@ -37,7 +43,7 @@ add_executable(dma_write apps/dma_write.c) target_link_libraries(dma_write tlp) add_executable(psmem apps/psmem.c) -target_link_libraries(psmem tlp pthread) +target_link_libraries(psmem tlp Threads::Threads) add_executable(process-list apps/process-list.c) target_link_libraries(process-list tlp) @@ -49,7 +55,7 @@ add_executable(codedump apps/codedump.c) target_link_libraries(codedump tlp) add_executable(tlpperf apps/tlpperf.c) -target_link_libraries(tlpperf tlp pthread) +target_link_libraries(tlpperf tlp Threads::Threads) add_executable(pcie_cfg_read apps/pcie_cfg_read.c) target_link_libraries(pcie_cfg_read tlp) @@ -57,3 +63,20 @@ target_link_libraries(pcie_cfg_read tlp) add_executable(pcie_cfg_write apps/pcie_cfg_write.c) target_link_libraries(pcie_cfg_write tlp) +# unittest +add_executable(unit_test test/test.c picotest/picotest.c) +target_include_directories(unit_test PRIVATE picotest) +target_link_libraries(unit_test tlp) + +enable_testing() +add_test(test unit_test) + +IF(CMAKE_BUILD_TYPE MATCHES Release) +add_custom_command( + TARGET unit_test + COMMENT "Run tests" + POST_BUILD + COMMAND unit_test +) +ENDIF() + diff --git a/picotest b/picotest new file mode 160000 index 0000000..6906d90 --- /dev/null +++ b/picotest @@ -0,0 +1 @@ +Subproject commit 6906d90b39684b8b2c18db5b0c7412128140655d diff --git a/test/.gitignore b/snippet/.gitignore similarity index 100% rename from test/.gitignore rename to snippet/.gitignore diff --git a/test/setup-netns.sh b/snippet/setup-netns.sh similarity index 100% rename from test/setup-netns.sh rename to snippet/setup-netns.sh diff --git a/test/test_dma_read.c b/snippet/test_dma_read.c similarity index 100% rename from test/test_dma_read.c rename to snippet/test_dma_read.c diff --git a/test/test_dma_write.c b/snippet/test_dma_write.c similarity index 100% rename from test/test_dma_write.c rename to snippet/test_dma_write.c diff --git a/test/test_msg_bar4.c b/snippet/test_msg_bar4.c similarity index 100% rename from test/test_msg_bar4.c rename to snippet/test_msg_bar4.c diff --git a/test/test_msg_devid.c b/snippet/test_msg_devid.c similarity index 100% rename from test/test_msg_devid.c rename to snippet/test_msg_devid.c diff --git a/test/test_msg_msix.c b/snippet/test_msg_msix.c similarity index 100% rename from test/test_msg_msix.c rename to snippet/test_msg_msix.c diff --git a/test/util.h b/snippet/util.h similarity index 100% rename from test/util.h rename to snippet/util.h diff --git a/test/test.c b/test/test.c new file mode 100644 index 0000000..4b8b28b --- /dev/null +++ b/test/test.c @@ -0,0 +1,257 @@ +#include "picotest.h" + +#include + +#include + +static void +calculate_fstdw_lstdw(uintptr_t addr, size_t count, int *result_fst, int *result_lst) +{ + *result_fst = tlp_calculate_fstdw(addr, count); + *result_lst = tlp_calculate_lstdw(addr, count); + //note("result_fst=0x%x, result_lst=0x%x", *result_fst, *result_lst); +} + +static void test_tlp_calculate_fstdw_lstdw(void) +{ + int result_fst, result_lst; + + /* zero-length read and write */ + calculate_fstdw_lstdw(0x0, 0, &result_fst, &result_lst); + ok(result_fst == 0 && result_lst == 0); + + calculate_fstdw_lstdw(0x0, 4093, &result_fst, &result_lst); + ok(result_fst == 0xf && result_lst == 0x1); + + calculate_fstdw_lstdw(0x0, 4094, &result_fst, &result_lst); + ok(result_fst == 0xf && result_lst == 0x3); + + calculate_fstdw_lstdw(0x0, 4095, &result_fst, &result_lst); + ok(result_fst == 0xf && result_lst == 0x7); + + calculate_fstdw_lstdw(0x0, 4096, &result_fst, &result_lst); + ok(result_fst == 0xf && result_lst == 0xf); + + /* zero-length read and write */ + calculate_fstdw_lstdw(0xa0000003, 0, &result_fst, &result_lst); + ok(result_fst == 0x0 && result_lst == 0x0); + + calculate_fstdw_lstdw(0xa0000000, 1, &result_fst, &result_lst); + ok(result_fst == 0x1 && result_lst == 0x0); + + calculate_fstdw_lstdw(0xa0000000, 2, &result_fst, &result_lst); + ok(result_fst == 0x3 && result_lst == 0x0); + + calculate_fstdw_lstdw(0xa0000000, 3, &result_fst, &result_lst); + ok(result_fst == 0x7 && result_lst == 0x0); + + calculate_fstdw_lstdw(0xa0000000, 4, &result_fst, &result_lst); + ok(result_fst == 0xf && result_lst == 0x0); + + calculate_fstdw_lstdw(0xa0000000, 5, &result_fst, &result_lst); + ok(result_fst == 0xf && result_lst == 0x1); + + calculate_fstdw_lstdw(0xa0000000, 6, &result_fst, &result_lst); + ok(result_fst == 0xf && result_lst == 0x3); + + calculate_fstdw_lstdw(0xa0000000, 7, &result_fst, &result_lst); + ok(result_fst == 0xf && result_lst == 0x7); + + calculate_fstdw_lstdw(0xa0000000, 8, &result_fst, &result_lst); + ok(result_fst == 0xf && result_lst == 0xf); + + calculate_fstdw_lstdw(0xa0000000, 16, &result_fst, &result_lst); + ok(result_fst == 0xf && result_lst == 0xf); + + calculate_fstdw_lstdw(0x3, 9, &result_fst, &result_lst); + ok(result_fst == 0x8 && result_lst == 0xf); + + calculate_fstdw_lstdw(0x3, 10, &result_fst, &result_lst); + ok(result_fst == 0x8 && result_lst == 0x1); + + calculate_fstdw_lstdw(0x3, 11, &result_fst, &result_lst); + ok(result_fst == 0x8 && result_lst == 0x3); + + calculate_fstdw_lstdw(0x3, 12, &result_fst, &result_lst); + ok(result_fst == 0x8 && result_lst == 0x7); + + calculate_fstdw_lstdw(0x3, 13, &result_fst, &result_lst); + ok(result_fst == 0x8 && result_lst == 0xf); + +// calculate_fstdw_lstdw(0xfffffffffffffff0, 16, &result_fst, &result_lst); +// ok(result_fst == 0xf && result_lst == 0xf); +// +// calculate_fstdw_lstdw(0xffffffffffffffe0, 32, &result_fst, &result_lst); +// ok(result_fst == 0xf && result_lst == 0xf); +} + +static void test_tlp_calculate_length(void) +{ + int result_length; + + result_length = tlp_calculate_length(0x0, 0); + ok(result_length == 0); + +// result_length = tlp_calculate_length(0x1, 0); +// ok(result_length == 0); +// +// result_length = tlp_calculate_length(0x2, 0); +// ok(result_length == 0); +// +// result_length = tlp_calculate_length(0x3, 0); +// ok(result_length == 0); + + result_length = tlp_calculate_length(0x0, 1); + ok(result_length == 1); + + result_length = tlp_calculate_length(0x0, 2); + ok(result_length == 1); + + result_length = tlp_calculate_length(0x0, 3); + ok(result_length == 1); + + result_length = tlp_calculate_length(0x0, 4); + ok(result_length == 1); + + result_length = tlp_calculate_length(0x0, 5); + ok(result_length == 2); + + result_length = tlp_calculate_length(0x3, 2); + ok(result_length == 2); + + result_length = tlp_calculate_length(0x3, 7); + ok(result_length == 3); + + result_length = tlp_calculate_length(0x0, 4089); + ok(result_length == 1023); + + result_length = tlp_calculate_length(0x0, 4090); + ok(result_length == 1023); + + result_length = tlp_calculate_length(0x0, 4091); + ok(result_length == 1023); + + result_length = tlp_calculate_length(0x0, 4092); + ok(result_length == 1023); + +// result_length = tlp_calculate_length(0x0, 4093); +// ok(result_length == 0); +// +// result_length = tlp_calculate_length(0x0, 4094); +// ok(result_length == 0); +// +// result_length = tlp_calculate_length(0x0, 4095); +// ok(result_length == 0); +// +// result_length = tlp_calculate_length(0x0, 4096); +// ok(result_length == 0); +} + +static void test_tlp_mr_addr(void) +{ + struct tlp_mr { + struct tlp_mr_hdr mh; + uintptr_t addr; + }; + + uintptr_t result_addr; + struct tlp_mr mr = {}; + + /* TLP_FMT_3DW */ + tlp_set_fmt(mr.mh.tlp.fmt_type, TLP_FMT_3DW, TLP_FMT_W_DATA); + + mr.addr = htobe32(0x0); + mr.mh.fstdw = 0xf; + result_addr = tlp_mr_addr(&mr.mh); + //note("%" PRIxPTR ", %" PRIxPTR, be32toh(mr.addr), result_addr); + ok(result_addr == be32toh(mr.addr) + 0); + + mr.addr = htobe32(0x0); + mr.mh.fstdw = 0xe; + result_addr = tlp_mr_addr(&mr.mh); + ok(result_addr == be32toh(mr.addr) + 1); + + mr.addr = htobe32(0x0); + mr.mh.fstdw = 0xc; + result_addr = tlp_mr_addr(&mr.mh); + ok(result_addr == be32toh(mr.addr) + 2); + + mr.addr = htobe32(0x0); + mr.mh.fstdw = 0x8; + result_addr = tlp_mr_addr(&mr.mh); + ok(result_addr == be32toh(mr.addr) + 3); + + mr.addr = htobe32(0x1); + mr.mh.fstdw = 0xf; + result_addr = tlp_mr_addr(&mr.mh); + ok(result_addr == be32toh(mr.addr) + 0); + + mr.addr = htobe32(0x1); + mr.mh.fstdw = 0xe; + result_addr = tlp_mr_addr(&mr.mh); + ok(result_addr == be32toh(mr.addr) + 1); + + mr.addr = htobe32(0x1); + mr.mh.fstdw = 0xc; + result_addr = tlp_mr_addr(&mr.mh); + ok(result_addr == be32toh(mr.addr) + 2); + + mr.addr = htobe32(0x1); + mr.mh.fstdw = 0x8; + result_addr = tlp_mr_addr(&mr.mh); + ok(result_addr == be32toh(mr.addr) + 3); + + /* TLP_FMT_4DW */ + tlp_set_fmt(mr.mh.tlp.fmt_type, TLP_FMT_4DW, TLP_FMT_W_DATA); + + mr.addr = htobe64(0x0); + mr.mh.fstdw = 0xf; + result_addr = tlp_mr_addr(&mr.mh); + ok(result_addr == be64toh(mr.addr) + 0); + + mr.addr = htobe64(0x0); + mr.mh.fstdw = 0xe; + result_addr = tlp_mr_addr(&mr.mh); + ok(result_addr == be64toh(mr.addr) + 1); + + mr.addr = htobe64(0x0); + mr.mh.fstdw = 0xc; + result_addr = tlp_mr_addr(&mr.mh); + ok(result_addr == be64toh(mr.addr) + 2); + + mr.addr = htobe64(0x0); + mr.mh.fstdw = 0x8; + result_addr = tlp_mr_addr(&mr.mh); + ok(result_addr == be64toh(mr.addr) + 3); + + mr.addr = htobe64(0x1); + mr.mh.fstdw = 0xf; + result_addr = tlp_mr_addr(&mr.mh); + ok(result_addr == be64toh(mr.addr) + 0); + + mr.addr = htobe64(0x1); + mr.mh.fstdw = 0xe; + result_addr = tlp_mr_addr(&mr.mh); + ok(result_addr == be64toh(mr.addr) + 1); + + mr.addr = htobe64(0x1); + mr.mh.fstdw = 0xc; + result_addr = tlp_mr_addr(&mr.mh); + ok(result_addr == be64toh(mr.addr) + 2); + + mr.addr = htobe64(0x1); + mr.mh.fstdw = 0x8; + result_addr = tlp_mr_addr(&mr.mh); + ok(result_addr == be64toh(mr.addr) + 3); +} + +int main(int argc, char **argv) +{ + subtest("tlp_calculate_fstdw_lstdw", test_tlp_calculate_fstdw_lstdw); + subtest("tlp_calculate_length", test_tlp_calculate_length); + subtest("tlp_calculate_length", test_tlp_calculate_length); + subtest("tlp_mr_addr", test_tlp_mr_addr); + + return done_testing(); +} +