gitout

A static git page generator
git clone https://git.bracken.jp/gitout.git
Log | Files | Refs | README | LICENSE

commit c696d99756724a4ad0f9c645451e2e24edcef809
parent 9d5fc3466564cbf736f9ac9b79802a15d5a6c902
Author: Chris Bracken <chris@bracken.jp>
Date:   Sun, 29 Jun 2025 09:17:59 -0700

Use utest.h instead of googletest

utest is a minimal header-only test framework that does the important
bits of what googletest does.

Cloned utest from https://github.com/sheredom/utest.h.git at commit
a91a8404a0abeb34aa8362847fe8a3acd5b5bcbd.

Diffstat:
D.gitmodules | 3---
MBUILD.gn | 9++++++---
Agitout_tests.c | 17+++++++++++++++++
Agitout_tests_main.c | 3+++
Dsecondary/third_party/googletest/BUILD.gn | 384-------------------------------------------------------------------------------
Asecondary/third_party/utest/BUILD.gn | 36++++++++++++++++++++++++++++++++++++
Dthird_party/googletest | 1-
Athird_party/utest/.clang-format | 3+++
Athird_party/utest/.github/FUNDING.yml | 1+
Athird_party/utest/.github/workflows/cmake.yml | 107+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Athird_party/utest/.github/workflows/sanitizers.yml | 65+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Athird_party/utest/.gitignore | 2++
Athird_party/utest/LICENSE | 24++++++++++++++++++++++++
Athird_party/utest/README.md | 702+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Athird_party/utest/appveyor.yml | 55+++++++++++++++++++++++++++++++++++++++++++++++++++++++
Athird_party/utest/test/CMakeLists.txt | 132+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Athird_party/utest/test/main.c | 111+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Athird_party/utest/test/side_effects.c | 138+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Athird_party/utest/test/side_effects.cpp | 136+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Athird_party/utest/test/stdint_include.c | 32++++++++++++++++++++++++++++++++
Athird_party/utest/test/subprocess.h | 1186+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Athird_party/utest/test/test.c | 268+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Athird_party/utest/test/test.cpp | 303+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Athird_party/utest/test/test11.c | 260+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Athird_party/utest/test/test11.cpp | 352+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Athird_party/utest/test/test14.cpp | 352+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Athird_party/utest/test/test17.cpp | 352+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Athird_party/utest/test/test99.c | 260+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Athird_party/utest/test/test_no_exceptions.cpp | 261+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Athird_party/utest/test/type_printers.c | 5+++++
Athird_party/utest/test/type_printers.cpp | 2++
Athird_party/utest/test/type_printers.inc | 52++++++++++++++++++++++++++++++++++++++++++++++++++++
Athird_party/utest/utest.h | 1719+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Dutils_test.cc | 29-----------------------------
Autils_tests.c | 27+++++++++++++++++++++++++++
35 files changed, 6969 insertions(+), 420 deletions(-)

diff --git a/.gitmodules b/.gitmodules @@ -1,3 +0,0 @@ -[submodule "third_party/googletest"] - path = third_party/googletest - url = https://github.com/google/googletest.git diff --git a/BUILD.gn b/BUILD.gn @@ -55,11 +55,14 @@ executable("gitout_index") { executable("gitout_tests") { testonly = true - sources = [ "utils_test.cc" ] + sources = [ + "gitout_tests.c", + "gitout_tests_main.c", + "utils_tests.c", + ] deps = [ ":gitout_srcs", - "//third_party/googletest:gtest", - "//third_party/googletest:gtest_main", + "//third_party/utest:utest_headers", ] } diff --git a/gitout_tests.c b/gitout_tests.c @@ -0,0 +1,17 @@ +#include "gitout.h" + +#include "utest.h" + +UTEST(gitout_options_create, ReturnsNullIfNoRepoSpecified) { + int argc = 1; + const char* argv[1] = { "path/to/gitout" }; + GitoutOptions* options = gitout_options_create(argc, argv); + EXPECT_EQ(NULL, options); +} + +UTEST(gitout_options_create, ReturnsInitializedOptions) { + int argc = 2; + const char* argv[2] = { "path/to/gitout", "some/path/myrepo" }; + GitoutOptions* options = gitout_options_create(argc, argv); + ASSERT_NE(NULL, options); +} diff --git a/gitout_tests_main.c b/gitout_tests_main.c @@ -0,0 +1,3 @@ +#include "utest.h" + +UTEST_MAIN() diff --git a/secondary/third_party/googletest/BUILD.gn b/secondary/third_party/googletest/BUILD.gn @@ -1,384 +0,0 @@ -config("gtest_private_config") { - visibility = [ ":*" ] - include_dirs = [ "googletest" ] - cflags_cc = [ - "-Wno-covered-switch-default", - "-Wno-deprecated", - "-Wno-double-promotion", - "-Wno-exit-time-destructors", - "-Wno-float-equal", - "-Wno-gnu-zero-variadic-macro-arguments", - "-Wno-missing-prototypes", - "-Wno-missing-variable-declarations", - "-Wno-old-style-cast", - "-Wno-sign-conversion", - "-Wno-switch-enum", - "-Wno-unreachable-code-break", - "-Wno-unused-member-function", - "-Wno-unused-private-field", - "-Wno-unused-template", - "-Wno-used-but-marked-unused", - "-Wno-zero-as-null-pointer-constant", - "-Wno-extra-semi-stmt", - ] - ldflags = [ "-lpthread" ] -} - -config("gtest_config") { - include_dirs = [ "googletest/include" ] - cflags_cc = [ - "-Wno-global-constructors", - "-Wno-missing-noreturn", - "-Wno-padded", - "-Wno-shift-sign-overflow", - "-Wno-thread-safety", # googletest lacks locking annotations. - "-Wno-undef", - "-Wno-weak-vtables", - ] - ldflags = [ "-lpthread" ] -} - -source_set("gtest_internal_headers") { - visibility = [ ":*" ] - testonly = true - sources = [ - "googletest/include/gtest/gtest-assertion-result.h", - "googletest/include/gtest/gtest-death-test.h", - "googletest/include/gtest/gtest-matchers.h", - "googletest/include/gtest/gtest-message.h", - "googletest/include/gtest/gtest-param-test.h", - "googletest/include/gtest/gtest-printers.h", - "googletest/include/gtest/gtest-spi.h", - "googletest/include/gtest/gtest-test-part.h", - "googletest/include/gtest/gtest-typed-test.h", - "googletest/include/gtest/gtest_pred_impl.h", - "googletest/include/gtest/gtest_prod.h", - "googletest/include/gtest/internal/custom/gtest-port.h", - "googletest/include/gtest/internal/custom/gtest-printers.h", - "googletest/include/gtest/internal/custom/gtest.h", - "googletest/include/gtest/internal/gtest-death-test-internal.h", - "googletest/include/gtest/internal/gtest-filepath.h", - "googletest/include/gtest/internal/gtest-internal.h", - "googletest/include/gtest/internal/gtest-param-util.h", - "googletest/include/gtest/internal/gtest-port-arch.h", - "googletest/include/gtest/internal/gtest-port.h", - "googletest/include/gtest/internal/gtest-string.h", - "googletest/include/gtest/internal/gtest-type-util.h", - "googletest/src/gtest-internal-inl.h", - ] -} - -source_set("gtest_test_headers") { - visibility = [ ":*" ] - testonly = true - sources = [ "googletest/include/gtest/gtest-spi.h" ] -} - -static_library("gtest") { - testonly = true - public = [ "googletest/include/gtest/gtest.h" ] - sources = [ - "googletest/src/gtest-all.cc", - "googletest/src/gtest-assertion-result.cc", - "googletest/src/gtest-death-test.cc", - "googletest/src/gtest-filepath.cc", - "googletest/src/gtest-matchers.cc", - "googletest/src/gtest-port.cc", - "googletest/src/gtest-printers.cc", - "googletest/src/gtest-test-part.cc", - "googletest/src/gtest-typed-test.cc", - "googletest/src/gtest.cc", - ] - sources -= [ "googletest/src/gtest-all.cc" ] - public_configs = [ ":gtest_config" ] - configs += [ ":gtest_private_config" ] - deps = [ - ":gtest_internal_headers", - ":gtest_test_headers", - ] -} - -static_library("gtest_main") { - testonly = true - sources = [ "googletest/src/gtest_main.cc" ] - public_deps = [ ":gtest" ] -} - -executable("gtest_all_test") { - testonly = true - sources = [ - "googletest/test/googletest-death-test-test.cc", - "googletest/test/googletest-filepath-test.cc", - "googletest/test/googletest-message-test.cc", - "googletest/test/googletest-options-test.cc", - "googletest/test/googletest-param-test2-test.cc", - "googletest/test/googletest-port-test.cc", - "googletest/test/googletest-printers-test.cc", - "googletest/test/googletest-test-part-test.cc", - "googletest/test/gtest-typed-test2_test.cc", - "googletest/test/gtest-typed-test_test.cc", - "googletest/test/gtest_dirs_test.cc", - "googletest/test/gtest_main_unittest.cc", - "googletest/test/gtest_pred_impl_unittest.cc", - "googletest/test/gtest_skip_test.cc", - "googletest/test/gtest_sole_header_test.cc", - "googletest/test/gtest_unittest.cc", - ] - configs += [ ":gtest_private_config" ] - deps = [ - ":gtest", - ":gtest_internal_headers", - ":gtest_main", - ":gtest_test_headers", - ] -} - -executable("gtest_environment_test") { - testonly = true - sources = [ "googletest/test/gtest_environment_test.cc" ] - configs += [ ":gtest_private_config" ] - deps = [ - ":gtest", - ":gtest_internal_headers", - ] -} - -executable("gtest_listener_test") { - testonly = true - sources = [ "googletest/test/googletest-listener-test.cc" ] - deps = [ ":gtest" ] - cflags_cc = [ - "-Wno-missing-prototypes", - "-Wno-missing-variable-declarations", - ] -} - -executable("gtest_no_test") { - testonly = true - sources = [ "googletest/test/gtest_no_test_unittest.cc" ] - deps = [ ":gtest" ] -} - -executable("gtest_param_test") { - testonly = true - sources = [ - "googletest/test/googletest-param-test-test.cc", - "googletest/test/googletest-param-test-test.h", - "googletest/test/googletest-param-test2-test.cc", - ] - configs += [ ":gtest_private_config" ] - deps = [ ":gtest" ] -} - -executable("gtest_premature_exit_test") { - testonly = true - sources = [ "googletest/test/gtest_premature_exit_test.cc" ] - configs += [ ":gtest_private_config" ] - deps = [ ":gtest" ] -} - -executable("gtest_repeat_test") { - testonly = true - sources = [ "googletest/test/gtest_repeat_test.cc" ] - configs += [ ":gtest_private_config" ] - deps = [ - ":gtest", - ":gtest_internal_headers", - ] -} - -executable("gtest_sole_header_test") { - testonly = true - sources = [ "googletest/test/gtest_sole_header_test.cc" ] - deps = [ - ":gtest", - ":gtest_main", - ] -} - -executable("gtest_stress_test") { - testonly = true - sources = [ "googletest/test/gtest_stress_test.cc" ] - configs += [ ":gtest_private_config" ] - deps = [ - ":gtest", - ":gtest_internal_headers", - ] -} - -executable("gtest_unittest_api_test") { - testonly = true - sources = [ "googletest/test/gtest-unittest-api_test.cc" ] - configs += [ ":gtest_private_config" ] - deps = [ ":gtest" ] -} - -group("gtest_all_tests") { - testonly = true - deps = [ - ":gtest_all_test", - ":gtest_environment_test", - ":gtest_listener_test", - ":gtest_no_test", - ":gtest_param_test", - ":gtest_premature_exit_test", - ":gtest_repeat_test", - ":gtest_sole_header_test", - ":gtest_stress_test", - ":gtest_unittest_api_test", - ] -} - -config("gmock_private_config") { - visibility = [ ":*" ] - include_dirs = [ "googlemock" ] - cflags_cc = [ - "-Wno-deprecated", - "-Wno-double-promotion", - "-Wno-exit-time-destructors", - "-Wno-float-equal", - "-Wno-missing-prototypes", - "-Wno-pedantic", - "-Wno-sign-conversion", - "-Wno-switch-enum", - "-Wno-unused-macros", - "-Wno-unused-parameter", - "-Wno-used-but-marked-unused", - "-Wno-zero-as-null-pointer-constant", - ] -} - -config("gmock_config") { - include_dirs = [ "googlemock/include" ] - - cflags_cc = [ - # The MOCK_METHODn() macros do not specify "override", which triggers this - # warning in users: "error: 'Method' overrides a member function but is not - # marked 'override' [-Werror,-Winconsistent-missing-override]". Suppress - # these warnings until https://github.com/google/googletest/issues/533 is - # fixed. - "-Wno-inconsistent-missing-override", - ] - ldflags = [ "-lpthread" ] -} - -source_set("gmock_internal_headers") { - visibility = [ ":*" ] - testonly = true - sources = [ - "googlemock/include/gmock/gmock-actions.h", - "googlemock/include/gmock/gmock-cardinalities.h", - "googlemock/include/gmock/gmock-generated-actions.h", - "googlemock/include/gmock/gmock-generated-function-mockers.h", - "googlemock/include/gmock/gmock-generated-matchers.h", - "googlemock/include/gmock/gmock-generated-nice-strict.h", - "googlemock/include/gmock/gmock-matchers.h", - "googlemock/include/gmock/gmock-more-actions.h", - "googlemock/include/gmock/gmock-more-matchers.h", - "googlemock/include/gmock/gmock-spec-builders.h", - "googlemock/include/gmock/internal/custom/gmock-generated-actions.h", - "googlemock/include/gmock/internal/custom/gmock-matchers.h", - "googlemock/include/gmock/internal/custom/gmock-port.h", - "googlemock/include/gmock/internal/gmock-generated-internal-utils.h", - "googlemock/include/gmock/internal/gmock-internal-utils.h", - "googlemock/include/gmock/internal/gmock-port.h", - ] -} - -static_library("gmock") { - testonly = true - public = [ "googlemock/include/gmock/gmock.h" ] - sources = [ - "googlemock/src/gmock-all.cc", - "googlemock/src/gmock-cardinalities.cc", - "googlemock/src/gmock-internal-utils.cc", - "googlemock/src/gmock-matchers.cc", - "googlemock/src/gmock-spec-builders.cc", - "googlemock/src/gmock.cc", - ] - sources -= [ "googlemock/src/gmock-all.cc" ] - public_configs = [ ":gmock_config" ] - configs += [ ":gmock_private_config" ] - deps = [ - ":gmock_internal_headers", - ":gtest", - ":gtest_internal_headers", - ] -} - -static_library("gmock_main") { - testonly = true - sources = [ "googlemock/src/gmock_main.cc" ] - configs += [ ":gmock_private_config" ] - public_deps = [ - ":gmock", - ":gtest", - ] -} - -executable("gmock_all_test") { - testonly = true - sources = [ - "googlemock/test/gmock-actions_test.cc", - "googlemock/test/gmock-cardinalities_test.cc", - "googlemock/test/gmock-generated-actions_test.cc", - "googlemock/test/gmock-generated-function-mockers_test.cc", - "googlemock/test/gmock-generated-internal-utils_test.cc", - "googlemock/test/gmock-generated-matchers_test.cc", - "googlemock/test/gmock-internal-utils_test.cc", - "googlemock/test/gmock-matchers_test.cc", - "googlemock/test/gmock-more-actions_test.cc", - "googlemock/test/gmock-nice-strict_test.cc", - "googlemock/test/gmock-port_test.cc", - "googlemock/test/gmock-spec-builders_test.cc", - "googlemock/test/gmock_test.cc", - ] - configs += [ - ":gmock_private_config", - ":gtest_private_config", - ] - deps = [ - ":gmock", - ":gmock_internal_headers", - ":gmock_main", - ":gtest", - ":gtest_internal_headers", - ":gtest_test_headers", - ] -} - -executable("gmock_link_test") { - testonly = true - sources = [ - "googlemock/test/gmock_link2_test.cc", - "googlemock/test/gmock_link_test.cc", - "googlemock/test/gmock_link_test.h", - ] - configs += [ ":gmock_private_config" ] - deps = [ - ":gmock", - ":gmock_main", - ":gtest", - ":gtest_internal_headers", - ] -} - -executable("gmock_stress_test") { - testonly = true - sources = [ "googlemock/test/gmock_stress_test.cc" ] - cflags_cc = [ "-Wno-unused-member-function" ] - configs += [ ":gmock_private_config" ] - deps = [ - ":gmock", - ":gtest", - ] -} - -group("gmock_all_tests") { - testonly = true - deps = [ - ":gmock_all_test", - ":gmock_link_test", - ":gmock_stress_test", - ] -} diff --git a/secondary/third_party/utest/BUILD.gn b/secondary/third_party/utest/BUILD.gn @@ -0,0 +1,36 @@ +config("utest_includes") { + include_dirs = [ "." ] +} + +config("utest_config") { + visibility = [ ":*" ] + cflags_c = [ + "-Wno-covered-switch-default", + "-Wno-deprecated", + "-Wno-double-promotion", + "-Wno-exit-time-destructors", + "-Wno-extra-semi-stmt", + "-Wno-float-equal", + "-Wno-gnu-zero-variadic-macro-arguments", + "-Wno-missing-noreturn", + "-Wno-missing-prototypes", + "-Wno-missing-variable-declarations", + "-Wno-padded", + "-Wno-shift-sign-overflow", + "-Wno-sign-conversion", + "-Wno-switch-enum", + "-Wno-undef", + "-Wno-unreachable-code-break", + "-Wno-used-but-marked-unused", + "-Wno-weak-vtables", + "-Wno-zero-as-null-pointer-constant", + ] + ldflags = [ "-lpthread" ] +} + +source_set("utest_headers") { + testonly = true + public_configs = [ ":utest_includes" ] + configs += [ ":utest_config" ] + sources = [ "utest.h" ] +} diff --git a/third_party/googletest b/third_party/googletest @@ -1 +0,0 @@ -Subproject commit 519beb0e52c842729b4b53731d27c0e0c32ab4a2 diff --git a/third_party/utest/.clang-format b/third_party/utest/.clang-format @@ -0,0 +1,2 @@ +BasedOnStyle: LLVM +SortIncludes: false +\ No newline at end of file diff --git a/third_party/utest/.github/FUNDING.yml b/third_party/utest/.github/FUNDING.yml @@ -0,0 +1 @@ +github: [sheredom] diff --git a/third_party/utest/.github/workflows/cmake.yml b/third_party/utest/.github/workflows/cmake.yml @@ -0,0 +1,107 @@ +name: CMake + +on: + push: + branches: [ master ] + pull_request: + branches: [ master ] + +jobs: + build: + strategy: + matrix: + os: [ubuntu-latest, windows-latest, macos-13, macos-latest] + type: [Debug, RelWithDebInfo, MinSizeRel, Release] + compiler: [default, clang, gcc, tcc] + exclude: + - {os: "macos-13", compiler: "clang"} + - {os: "macos-13", compiler: "gcc"} + - {os: "macos-13", compiler: "tcc"} + - {os: "macos-latest", compiler: "clang"} + - {os: "macos-latest", compiler: "gcc"} + - {os: "macos-latest", compiler: "tcc"} + - {os: "ubuntu-latest", compiler: "default"} + - {os: "ubuntu-latest", compiler: "default"} + - {os: "windows-latest", compiler: "tcc"} + runs-on: ${{ matrix.os }} + + steps: + - uses: actions/checkout@v4 + + - name: Create Build Environment + run: cmake -E make_directory ${{github.workspace}}/build + + - name: Setup dependencies (Ubuntu) + if: startsWith(matrix.os, 'ubuntu') + run: sudo apt-get install -y gcc-10 g++-10 clang tcc + + - name: Setup dependencies (MinGW) + if: matrix.compiler == 'gcc' && startsWith(matrix.os, 'windows') + uses: e-t-l/setup-mingw@patch-1 + + - name: Configure CMake + shell: bash + if: matrix.compiler == 'default' + working-directory: ${{github.workspace}}/build + run: cmake $GITHUB_WORKSPACE/test -DCMAKE_BUILD_TYPE=${{ matrix.type }} + + - name: Configure CMake with GCC (Ubuntu) + shell: bash + if: matrix.compiler == 'gcc' && startsWith(matrix.os, 'ubuntu') + working-directory: ${{github.workspace}}/build + run: cmake $GITHUB_WORKSPACE/test -DCMAKE_BUILD_TYPE=${{ matrix.type }} -DCMAKE_C_COMPILER=gcc-10 -DCMAKE_CXX_COMPILER=g++-10 + + - name: Configure CMake with TCC (Ubuntu) + shell: bash + if: matrix.compiler == 'tcc' && startsWith(matrix.os, 'ubuntu') + working-directory: ${{github.workspace}}/build + run: cmake $GITHUB_WORKSPACE/test -DCMAKE_BUILD_TYPE=${{ matrix.type }} -DCMAKE_C_COMPILER=tcc -DCMAKE_CXX_COMPILER=g++-10 + + - name: Configure CMake with Clang (Ubuntu) + shell: bash + if: (matrix.compiler == 'clang') && startsWith(matrix.os, 'ubuntu') + working-directory: ${{github.workspace}}/build + run: cmake $GITHUB_WORKSPACE/test -DCMAKE_BUILD_TYPE=${{ matrix.type }} -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ + + - name: Configure CMake with MinGW + shell: bash + if: matrix.compiler == 'gcc' && startsWith(matrix.os, 'windows') + working-directory: ${{github.workspace}}/build + run: cmake $GITHUB_WORKSPACE/test -DCMAKE_BUILD_TYPE=${{ matrix.type }} -G "MinGW Makefiles" -DCMAKE_C_COMPILER=x86_64-w64-mingw32-gcc -DCMAKE_CXX_COMPILER=x86_64-w64-mingw32-g++ + + - name: Configure CMake with Clang (Windows) + shell: bash + if: (matrix.compiler == 'clang') && startsWith(matrix.os, 'windows') + working-directory: ${{github.workspace}}/build + run: cmake $GITHUB_WORKSPACE/test -DCMAKE_BUILD_TYPE=${{ matrix.type }} -T ClangCL + + - name: Build + working-directory: ${{github.workspace}}/build + shell: bash + run: cmake --build . --config ${{ matrix.type }} + + - name: Test + working-directory: ${{github.workspace}}/build + shell: bash + run: if [ "${{ matrix.os }}" == "windows-latest" ] && [ "${{ matrix.compiler }}" != "gcc" ]; then cd ${{ matrix.type }}; fi; ./utest_test + + - name: Test Whole Program Optimization + working-directory: ${{github.workspace}}/build + shell: bash + run: if [ "${{ matrix.os }}" == "windows-latest" ] && [ "${{ matrix.compiler }}" != "gcc" ]; then cd ${{ matrix.type }}; fi; ./utest_test_wpo + + - name: Test with Multithreading + working-directory: ${{github.workspace}}/build + shell: bash + if: startsWith(matrix.os, 'windows') + run: if [ "${{ matrix.os }}" == "windows-latest" ] && [ "${{ matrix.compiler }}" != "gcc" ]; then cd ${{ matrix.type }}; fi; ./utest_test_mt + + - name: Test with random order + working-directory: ${{github.workspace}}/build + shell: bash + run: if [ "${{ matrix.os }}" == "windows-latest" ] && [ "${{ matrix.compiler }}" != "gcc" ]; then cd ${{ matrix.type }}; fi; ./utest_test --random-order + + - name: Test with random order and seed + working-directory: ${{github.workspace}}/build + shell: bash + run: if [ "${{ matrix.os }}" == "windows-latest" ] && [ "${{ matrix.compiler }}" != "gcc" ]; then cd ${{ matrix.type }}; fi; ./utest_test --random-order=42 diff --git a/third_party/utest/.github/workflows/sanitizers.yml b/third_party/utest/.github/workflows/sanitizers.yml @@ -0,0 +1,65 @@ +name: Sanitizers + +on: + push: + branches: [ master ] + pull_request: + branches: [ master ] + +jobs: + build: + strategy: + matrix: + os: [ubuntu-latest, macOS-latest] + type: [Debug, RelWithDebInfo, MinSizeRel, Release] + sanitizer: [address, memory, undefined] + exclude: + - {os: "macOS-latest", sanitizer: "memory"} + runs-on: ${{ matrix.os }} + + steps: + - uses: actions/checkout@v4 + + - name: Create Build Environment + run: cmake -E make_directory ${{github.workspace}}/build + + - name: Setup dependencies + if: startsWith(matrix.os, 'ubuntu') + run: sudo apt-get install -y clang + + - name: Configure CMake (macOS) + shell: bash + if: startsWith(matrix.os, 'macOS') + working-directory: ${{github.workspace}}/build + run: cmake $GITHUB_WORKSPACE/test -DCMAKE_BUILD_TYPE=${{ matrix.type }} -DUTEST_USE_SANITIZER=${{ matrix.sanitizer }} + + - name: Configure CMake with Clang (Ubuntu) + shell: bash + if: startsWith(matrix.os, 'ubuntu') + working-directory: ${{github.workspace}}/build + run: cmake $GITHUB_WORKSPACE/test -DCMAKE_BUILD_TYPE=${{ matrix.type }} -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -DUTEST_USE_SANITIZER=${{ matrix.sanitizer }} + + - name: Build + working-directory: ${{github.workspace}}/build + shell: bash + run: cmake --build . --config ${{ matrix.type }} + + - name: Test + working-directory: ${{github.workspace}}/build + shell: bash + run: ./utest_test + + - name: Test Whole Program Optimization + working-directory: ${{github.workspace}}/build + shell: bash + run: ./utest_test_wpo + + - name: Test with random order + working-directory: ${{github.workspace}}/build + shell: bash + run: ./utest_test --random-order + + - name: Test with random order and seed + working-directory: ${{github.workspace}}/build + shell: bash + run: ./utest_test --random-order=42 diff --git a/third_party/utest/.gitignore b/third_party/utest/.gitignore @@ -0,0 +1,2 @@ +*.DS_Store +build diff --git a/third_party/utest/LICENSE b/third_party/utest/LICENSE @@ -0,0 +1,24 @@ +This is free and unencumbered software released into the public domain. + +Anyone is free to copy, modify, publish, use, compile, sell, or +distribute this software, either in source code form or as a compiled +binary, for any purpose, commercial or non-commercial, and by any +means. + +In jurisdictions that recognize copyright laws, the author or authors +of this software dedicate any and all copyright interest in the +software to the public domain. We make this dedication for the benefit +of the public at large and to the detriment of our heirs and +successors. We intend this dedication to be an overt act of +relinquishment in perpetuity of all present and future rights to this +software under copyright law. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR +OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. + +For more information, please refer to <http://unlicense.org/> diff --git a/third_party/utest/README.md b/third_party/utest/README.md @@ -0,0 +1,702 @@ +# 🧪 utest.h + +[![Actions Status](https://github.com/sheredom/utest.h/workflows/CMake/badge.svg)](https://github.com/sheredom/utest.h/actions) +[![Build status](https://ci.appveyor.com/api/projects/status/i2u3a0pw4pxprrcv?svg=true)](https://ci.appveyor.com/project/sheredom/utest-h) +[![Sponsor](https://img.shields.io/badge/💜-sponsor-blueviolet)](https://github.com/sponsors/sheredom) + +A simple one header solution to unit testing for C/C++. + +## Usage + +Just `#include "utest.h"` in your code! + +The current supported platforms are Linux, macOS and Windows. + +The current supported compilers are gcc, clang, MSVC's cl.exe, and clang-cl.exe. + +It also works with tcc but with a caveat: the latest release of the tcc compiler (version 0.9.27) lacks a feature for UTEST to work. Make sure to use a tcc that is patched with the constructor attribute extension. Recent Ubuntu and Debian Linux distros ship tcc with that patch already included. If you compile tcc yourself, use the trunk version and it will work as expected. + +## Command Line Options + +utest.h supports some command line options: + +* `--help` to output the help message +* `--filter=<filter>` will filter the test cases to run (useful for re-running one + particular offending test case). +* `--list-tests` will list testnames, one per line. Output names can be passed to `--filter`. +* `--output=<output>` will output an xunit XML file with the test results (that + Jenkins, travis-ci, and appveyor can parse for the test results). +* `--enable-mixed-units` will enable the per-test output to contain mixed units (s/ms/us/ns). +* `--random-order[=<seed>]` will randomize the order that the tests are ran in. If the optional <seed> argument is not provided, then a random starting seed is used. + +## Design + +UTest is a single header library to enable all the fun of unit testing in C and +C++. The library has been designed to provide an output similar to Google's +googletest framework: + +``` +[==========] Running 1 test cases. +[ RUN ] foo.bar +[ OK ] foo.bar (631ns) +[==========] 1 test cases ran. +[ PASSED ] 1 tests. +``` + +## UTEST_MAIN + +In one C or C++ file, you must call the macro UTEST_MAIN: + +```c +UTEST_MAIN() +``` + +This will call into utest.h, instantiate all the testcases and run the unit test +framework. + +Alternatively, if you want to write your own main and call into utest.h, you can +instead, in one C or C++ file call: + +```c +UTEST_STATE(); +``` + +And then when you are ready to call into the utest.h framework do: + +```c +int main(int argc, const char *const argv[]) { + // do your own thing + return utest_main(argc, argv); +} +``` + +## Define a Testcase + +To define a test case to run, you can do the following; + +```c +#include "utest.h" + +UTEST(foo, bar) { + ASSERT_TRUE(1); +} +``` + +The UTEST macro takes two parameters - the first being the set that the test +case belongs to, the second being the name of the test. This allows tests to be +grouped for convenience. + +## Define a Fixtured Testcase + +A fixtured testcase is one in which there is a struct that is instantiated that +can be shared across multiple testcases. + +```c +struct MyTestFixture { + char c; + int i; + float f; +}; + +UTEST_F_SETUP(MyTestFixture) { + utest_fixture->c = 'a'; + utest_fixture->i = 42; + utest_fixture->f = 3.14f; + + // we can even assert and expect in setup! + ASSERT_EQ(42, utest_fixture->i); + EXPECT_TRUE(true); +} + +UTEST_F_TEARDOWN(MyTestFixture) { + // and also assert and expect in teardown! + ASSERT_EQ(13, utest_fixture->i); +} + +UTEST_F(MyTestFixture, a) { + utest_fixture->i = 13; + // teardown will succeed because i is 13... +} + +UTEST_F(MyTestFixture, b) { + utest_fixture->i = 83; + // teardown will fail because i is not 13! +} +``` + +Some things to note that were demonstrated above: +* We have this new implicit variable within our macros - utest_fixture. This is + a pointer to the struct you decided as your fixture (so MyTestFixture in the + above code). +* Instead of specifying a testcase set (like we do with the UTEST macro), we + instead specify the name of the fixture struct we are using. +* Every fixture has to have a `UTEST_F_SETUP` and `UTEST_F_TEARDOWN` macro - + even if they do nothing in the body. +* Multiple testcases (UTEST_F's) can use the same fixture. +* You can use EXPECT_* and ASSERT_* macros within the body of both the fixture's + setup and teardown macros. + +## Define an Indexed Testcase + +Sometimes you want to use the same fixture _and_ testcase repeatedly, but +perhaps subtly change one variable within. This is where indexed testcases come +in. + +```c +struct MyTestIndexedFixture{ + bool x; + bool y; +}; + +UTEST_I_SETUP(MyTestIndexedFixture) { + if (utest_index < 30) { + utest_fixture->x = utest_index & 1; + utest_fixture->y = (utest_index + 1) & 1; + } +} + +UTEST_I_TEARDOWN(MyTestIndexedFixture) { + EXPECT_LE(0, utest_index); +} + +UTEST_I(MyTestIndexedFixture, a, 2) { + ASSERT_TRUE(utest_fixture->x | utest_fixture->y); +} + +UTEST_I(MyTestIndexedFixture, b, 42) { + // this will fail when the index is >= 30 + ASSERT_TRUE(utest_fixture->x | utest_fixture->y); +} +``` + +Note: +* We use UTEST_I_* as the prefix for the setup and teardown functions now. +* We use UTEST_I to declare the testcases. +* We have access to a new variable utest_index in our setup and teardown + functions, that we can use to slightly vary our fixture. +* We provide a number as the third parameter of the UTEST_I macro - this is the + number of times we should run the test case for that index. It must be a + literal. + +## Testing Macros + +Matching what googletest has, we provide two variants of each of the error +checking conditions - ASSERTs and EXPECTs. If an ASSERT fails, the test case +will cease execution, and utest.h will continue with the next test case to be +run. If an EXPECT fails, the remainder of the test case will still be executed, +allowing for further checks to be carried out. + +We currently provide the following macros to be used within UTESTs: + +### ASSERT_TRUE(x) + +Asserts that x evaluates to true (EG. non-zero). + +```c +UTEST(foo, bar) { + int i = 1; + ASSERT_TRUE(i); // pass! + ASSERT_TRUE(42); // pass! + ASSERT_TRUE(0); // fail! +} +``` + +### ASSERT_FALSE(x) + +Asserts that x evaluates to false (EG. zero). + +```c +UTEST(foo, bar) { + int i = 0; + ASSERT_FALSE(i); // pass! + ASSERT_FALSE(1); // fail! +} +``` + +### ASSERT_EQ(x, y) + +Asserts that x and y are equal. + +```c +UTEST(foo, bar) { + int a = 42; + int b = 42; + ASSERT_EQ(a, b); // pass! + ASSERT_EQ(a, 42); // pass! + ASSERT_EQ(42, b); // pass! + ASSERT_EQ(42, 42); // pass! + ASSERT_EQ(a, b + 1); // fail! +} +``` + +### ASSERT_NE(x, y) + +Asserts that x and y are not equal. + +```c +UTEST(foo, bar) { + int a = 42; + int b = 13; + ASSERT_NE(a, b); // pass! + ASSERT_NE(a, 27); // pass! + ASSERT_NE(69, b); // pass! + ASSERT_NE(42, 13); // pass! + ASSERT_NE(a, 42); // fail! +} +``` + +### ASSERT_LT(x, y) + +Asserts that x is less than y. + +```c +UTEST(foo, bar) { + int a = 13; + int b = 42; + ASSERT_LT(a, b); // pass! + ASSERT_LT(a, 27); // pass! + ASSERT_LT(27, b); // pass! + ASSERT_LT(13, 42); // pass! + ASSERT_LT(b, a); // fail! +} +``` + +### ASSERT_LE(x, y) + +Asserts that x is less than or equal to y. + +```c +UTEST(foo, bar) { + int a = 13; + int b = 42; + ASSERT_LE(a, b); // pass! + ASSERT_LE(a, 27); // pass! + ASSERT_LE(a, 13); // pass! + ASSERT_LE(27, b); // pass! + ASSERT_LE(42, b); // pass! + ASSERT_LE(13, 13); // pass! + ASSERT_LE(13, 42); // pass! + ASSERT_LE(b, a); // fail! +} +``` + +### ASSERT_GT(x, y) + +Asserts that x is greater than y. + +```c +UTEST(foo, bar) { + int a = 42; + int b = 13; + ASSERT_GT(a, b); // pass! + ASSERT_GT(a, 27); // pass! + ASSERT_GT(27, b); // pass! + ASSERT_GT(42, 13); // pass! + ASSERT_GT(b, a); // fail! +} +``` + +### ASSERT_GE(x, y) + +Asserts that x is greater than or equal to y. + +```c +UTEST(foo, bar) { + int a = 42; + int b = 13; + ASSERT_GE(a, b); // pass! + ASSERT_GE(a, 27); // pass! + ASSERT_GE(a, 13); // pass! + ASSERT_GE(27, b); // pass! + ASSERT_GE(42, b); // pass! + ASSERT_GE(13, 13); // pass! + ASSERT_GE(42, 13); // pass! + ASSERT_GE(b, a); // fail! +} +``` + +### ASSERT_STREQ(x, y) + +Asserts that the strings x and y are equal. + +```c +UTEST(foo, bar) { + char* a = "foo"; + char* b = "bar"; + ASSERT_STREQ(a, a); // pass! + ASSERT_STREQ(b, b); // pass! + ASSERT_STREQ(a, b); // fail! +} +``` + +### ASSERT_STRNE(x, y) + +Asserts that the strings x and y are not equal. + +```c +UTEST(foo, bar) { + char* a = "foo"; + char* b = "bar"; + ASSERT_STRNE(a, b); // pass! + ASSERT_STRNE(a, a); // fail! +} +``` + +### ASSERT_STRNEQ(x, y) + +Asserts that the strings x and y are equal up to the length of the string x. + +```c +UTEST(foo, bar) { + char* a = "foobar"; + char* b = "foo"; + ASSERT_STRNEQ(a, a); // pass! + ASSERT_STRNEQ(b, b); // pass! + ASSERT_STRNEQ(a, b); // pass! +} +``` + +### ASSERT_STRNNE(x, y) + +Asserts that the strings x and y are not equal up to the length of the string x. + +```c +UTEST(foo, bar) { + char* a = "foobar"; + char* b = "bar"; + ASSERT_STRNNE(a, b); // pass! + ASSERT_STRNNE(a, a); // fail! +} +``` + +### ASSERT_NEAR(x, y, epsilon) + +Asserts that the floating-point values x and y are within epsilon distance of +each other. + +```c +UTEST(foo, bar) { + float a = 42.0f; + float b = 42.01f; + ASSERT_NEAR(a, b, 0.01f); // pass! + ASSERT_NEAR(a, b, 0.001f); // fail! +} +``` + +### ASSERT_EXCEPTION(x, exception_type) + +Asserts that exception_type will be thrown when code x is executed. + +```cpp +void foo(int bar) { + if (bar == 1) + throw std::range_error; +} + +UTEST(foo, bar) { + ASSERT_EXCEPTION(foo(1), std::range_error); // pass! + ASSERT_EXCEPTION(foo(2), std::range_error); // fail! + ASSERT_EXCEPTION(foo(1), std::exception); // fail! +} +``` + +### EXPECT_TRUE(x) + +Expects that x evaluates to true (i.e. non-zero). + +```c +UTEST(foo, bar) { + int i = 1; + EXPECT_TRUE(i); // pass! + EXPECT_TRUE(42); // pass! + EXPECT_TRUE(0); // fail! +} +``` + +### EXPECT_FALSE(x) + +Expects that x evaluates to false (i.e. zero). + +```c +UTEST(foo, bar) { + int i = 0; + EXPECT_FALSE(i); // pass! + EXPECT_FALSE(1); // fail! +} +``` + +### EXPECT_EQ(x, y) + +Expects that x and y are equal. + +```c +UTEST(foo, bar) { + int a = 42; + int b = 42; + EXPECT_EQ(a, b); // pass! + EXPECT_EQ(a, 42); // pass! + EXPECT_EQ(42, b); // pass! + EXPECT_EQ(42, 42); // pass! + EXPECT_EQ(a, b + 1); // fail! +} +``` + +### EXPECT_NE(x, y) + +Expects that x and y are not equal. + +```c +UTEST(foo, bar) { + int a = 42; + int b = 13; + EXPECT_NE(a, b); // pass! + EXPECT_NE(a, 27); // pass! + EXPECT_NE(69, b); // pass! + EXPECT_NE(42, 13); // pass! + EXPECT_NE(a, 42); // fail! +} +``` + +### EXPECT_LT(x, y) + +Expects that x is less than y. + +```c +UTEST(foo, bar) { + int a = 13; + int b = 42; + EXPECT_LT(a, b); // pass! + EXPECT_LT(a, 27); // pass! + EXPECT_LT(27, b); // pass! + EXPECT_LT(13, 42); // pass! + EXPECT_LT(b, a); // fail! +} +``` + +### EXPECT_LE(x, y) + +Expects that x is less than or equal to y. + +```c +UTEST(foo, bar) { + int a = 13; + int b = 42; + EXPECT_LE(a, b); // pass! + EXPECT_LE(a, 27); // pass! + EXPECT_LE(a, 13); // pass! + EXPECT_LE(27, b); // pass! + EXPECT_LE(42, b); // pass! + EXPECT_LE(13, 13); // pass! + EXPECT_LE(13, 42); // pass! + EXPECT_LE(b, a); // fail! +} +``` + +### EXPECT_GT(x, y) + +Expects that x is greater than y. + +```c +UTEST(foo, bar) { + int a = 42; + int b = 13; + EXPECT_GT(a, b); // pass! + EXPECT_GT(a, 27); // pass! + EXPECT_GT(27, b); // pass! + EXPECT_GT(42, 13); // pass! + EXPECT_GT(b, a); // fail! +} +``` + +### EXPECT_GE(x, y) + +Expects that x is greater than or equal to y. + +```c +UTEST(foo, bar) { + int a = 42; + int b = 13; + EXPECT_GE(a, b); // pass! + EXPECT_GE(a, 27); // pass! + EXPECT_GE(a, 13); // pass! + EXPECT_GE(27, b); // pass! + EXPECT_GE(42, b); // pass! + EXPECT_GE(13, 13); // pass! + EXPECT_GE(42, 13); // pass! + EXPECT_GE(b, a); // fail! +} +``` + +### EXPECT_STREQ(x, y) + +Expects that the strings x and y are equal. + +```c +UTEST(foo, bar) { + char* a = "foo"; + char* b = "bar"; + EXPECT_STREQ(a, a); // pass! + EXPECT_STREQ(b, b); // pass! + EXPECT_STREQ(a, b); // fail! +} +``` + +### EXPECT_STRNE(x, y) + +Expects that the strings x and y are not equal. + +```c +UTEST(foo, bar) { + char* a = "foo"; + char* b = "bar"; + EXPECT_STRNE(a, b); // pass! + EXPECT_STRNE(a, a); // fail! +} +``` + +### EXPECT_STRNEQ(x, y) + +Expects that the strings x and y are equal up to the length of the string x. + +```c +UTEST(foo, bar) { + char* a = "foobar"; + char* b = "foo"; + EXPECT_STRNEQ(a, a); // pass! + EXPECT_STRNEQ(b, b); // pass! + EXPECT_STRNEQ(a, b); // pass! +} +``` + +### EXPECT_STRNNE(x, y) + +Expects that the strings x and y are not equal up to the length of the string x. + +```c +UTEST(foo, bar) { + char* a = "foobar"; + char* b = "bar"; + EXPECT_STRNNE(a, b); // pass! + EXPECT_STRNNE(a, a); // fail! +} +``` + +### EXPECT_NEAR(x, y, epsilon) + +Expects that the floating-point values x and y are within epsilon distance of +each other. + +```c +UTEST(foo, bar) { + float a = 42.0f; + float b = 42.01f; + EXPECT_NEAR(a, b, 0.01f); // pass! + EXPECT_NEAR(a, b, 0.001f); // fail! +} +``` + +### EXPECT_EXCEPTION(x, exception_type) + +Expects that exception_type will be thrown when code x is executed. + +```cpp +void foo(int bar) { + if (bar == 1) + throw std::range_error; +} + +UTEST(foo, bar) { + EXPECT_EXCEPTION(foo(1), std::range_error); // pass! + EXPECT_EXCEPTION(foo(2), std::range_error); // fail! + EXPECT_EXCEPTION(foo(1), std::exception); // fail! +} +``` + +### EXPECT_EXCEPTION_WITH_MESSAGE(x, exception_type, exception_message) + +Expects that exception_type will be thrown with message exception_message when code x is executed. + +```cpp +void foo(int bar) { + if (bar == 1) + throw std::range_error("bad bar"); +} + +UTEST(foo, bar) { + EXPECT_EXCEPTION_WITH_MESSAGE(foo(1), std::range_error, "bad bar"); // pass! + EXPECT_EXCEPTION_WITH_MESSAGE(foo(2), std::range_error, "bad bar2"); // fail! + EXPECT_EXCEPTION_WITH_MESSAGE(foo(1), std::exception, "bad bar"); // fail! +} +``` + +### UTEST_SKIP(msg) + +This macro lets you mark a test case as being skipped - eg. that the test case +is not to be executed. The test will stop running as you execute the macro, +report the `msg` as the reason for the skipped, and mark the test as +_'skipped'_. These will be reported at the end of execution before failures, and +skipped test cases will **not** cause the process to exit with a non-zero code. + +```c +UTEST(foo, bar) { + UTEST_SKIP("Need to implement this test!"); +} +``` + +### Testing macros with custom message + +In addition, to give the possibility of having custom messages in the fault +tests, all macros can be used with a suffix called "_MSG", which receives an +extra parameter, which is the string with the custom message to print in case +of failure. + +For example: + +```c +UTEST(foo, bar) { + int i = 1; + EXPECT_TRUE_MSG(i, "custom message"); // pass! + EXPECT_TRUE_MSG(42, "custom message"); // pass! + EXPECT_TRUE_MSG(0, "custom message"); // fail! (with the following output) +} +``` + +``` +test.cpp:42: Failure + Expected : true + Actual : false + Message : custom message +[ FAILED ] foo.bar (8086ns) +``` + +## Types Supported for Checks + +The library supports asserting on any builtin integer, floating-point, or +pointer type. + +## License + +This is free and unencumbered software released into the public domain. + +Anyone is free to copy, modify, publish, use, compile, sell, or +distribute this software, either in source code form or as a compiled +binary, for any purpose, commercial or non-commercial, and by any +means. + +In jurisdictions that recognize copyright laws, the author or authors +of this software dedicate any and all copyright interest in the +software to the public domain. We make this dedication for the benefit +of the public at large and to the detriment of our heirs and +successors. We intend this dedication to be an overt act of +relinquishment in perpetuity of all present and future rights to this +software under copyright law. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR +OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. + +For more information, please refer to <http://unlicense.org/> diff --git a/third_party/utest/appveyor.yml b/third_party/utest/appveyor.yml @@ -0,0 +1,55 @@ +version: '{build}' + +skip_tags: true +skip_branch_with_pr: true + +install: [] + +environment: + matrix: + - VSVERSION: Visual Studio 9 2008 + - VSVERSION: Visual Studio 10 2010 + - VSVERSION: Visual Studio 11 2012 + - VSVERSION: Visual Studio 12 2013 + - VSVERSION: Visual Studio 14 2015 + - VSVERSION: Visual Studio 15 2017 + APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2017 + - VSVERSION: Visual Studio 16 2019 + APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2019 + +platform: + - Win32 + - x64 + +matrix: + exclude: + - platform: x64 + VSVERSION: Visual Studio 9 2008 + # VS 2019 / 64-bit is tested in GitHub Actions instead. + - platform: x64 + VSVERSION: Visual Studio 16 2019 + +configuration: + - Debug + # Removed to reduce configuration explosion. + # - RelWithDebInfo + # - MinSizeRel + - Release + +build_script: + - md build + - cd build + - if NOT "%VSVERSION%"=="Visual Studio 16 2019" if "%PLATFORM%"=="x64" cmake -G "%VSVERSION% Win64" ../test + - if NOT "%VSVERSION%"=="Visual Studio 16 2019" if "%PLATFORM%"=="Win32" cmake -G "%VSVERSION%" ../test + - if "%VSVERSION%"=="Visual Studio 16 2019" cmake -G "%VSVERSION%" -A "%PLATFORM%" ../test + - msbuild /m /p:Configuration="%CONFIGURATION%" /p:Platform="%PLATFORM%" utest.sln + - copy %CONFIGURATION%\utest_test.exe utest_test.exe + - copy %CONFIGURATION%\utest_test_wpo.exe utest_test_wpo.exe + - copy %CONFIGURATION%\utest_test_wpo.exe utest_test_mt.exe + +test_script: + - utest_test.exe + - utest_test_wpo.exe + - utest_test_mt.exe + - utest_test.exe --random-order + - utest_test.exe --random-order=42 diff --git a/third_party/utest/test/CMakeLists.txt b/third_party/utest/test/CMakeLists.txt @@ -0,0 +1,132 @@ +# This is free and unencumbered software released into the public domain. +# +# Anyone is free to copy, modify, publish, use, compile, sell, or +# distribute this software, either in source code form or as a compiled +# binary, for any purpose, commercial or non-commercial, and by any +# means. +# +# In jurisdictions that recognize copyright laws, the author or authors +# of this software dedicate any and all copyright interest in the +# software to the public domain. We make this dedication for the benefit +# of the public at large and to the detriment of our heirs and +# successors. We intend this dedication to be an overt act of +# relinquishment in perpetuity of all present and future rights to this +# software under copyright law. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR +# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +# OTHER DEALINGS IN THE SOFTWARE. +# +# For more information, please refer to <http://unlicense.org/> + +project(utest) +cmake_minimum_required(VERSION 3.15) + +set(UTEST_USE_SANITIZER "" CACHE STRING "Set which Clang Sanitizer to use") + +include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../) + +function(set_flags file msvc gcc clang_extra) + if("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU") + set_source_files_properties("${file}" PROPERTIES COMPILE_FLAGS "${gcc}") + elseif("${CMAKE_C_COMPILER_ID}" STREQUAL "Clang") + if(CMAKE_CXX_COMPILER_FRONTEND_VARIANT STREQUAL "MSVC") + set_source_files_properties("${file}" PROPERTIES COMPILE_FLAGS "${msvc}") + else() + set_source_files_properties("${file}" PROPERTIES COMPILE_FLAGS "${gcc} ${clang_extra}") + endif() + elseif("${CMAKE_C_COMPILER_ID}" STREQUAL "MSVC") + set_source_files_properties("${file}" PROPERTIES COMPILE_FLAGS "${msvc}") + elseif("${CMAKE_C_COMPILER_ID}" STREQUAL "TinyCC") + set_source_files_properties("${file}" PROPERTIES COMPILE_FLAGS "${gcc}") + else() + message(WARNING "Unknown compiler '${CMAKE_C_COMPILER_ID}'!") + endif() +endfunction() + +set(MSVC_WARNINGS "/Wall /WX /wd4514 /wd5045") +set(GCC_WARNINGS "-Wall -Wextra -Werror") +set(CLANG_WARNINGS "-Weverything") + +set_flags(main.c "${MSVC_WARNINGS}" "${GCC_WARNINGS} -std=gnu89" "${CLANG_WARNINGS}") +set_flags(test.c "${MSVC_WARNINGS}" "${GCC_WARNINGS} -std=gnu89" "${CLANG_WARNINGS}") +set_flags(test99.c "${MSVC_WARNINGS}" "${GCC_WARNINGS} -std=c99" "${CLANG_WARNINGS}") + +list(FIND CMAKE_C_COMPILE_FEATURES c_std_11 IDX) +if (${IDX} GREATER -1) + set_flags(test11.c "${MSVC_WARNINGS} /std:c11" "${GCC_WARNINGS} -std=c11" "${CLANG_WARNINGS}") + + if("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU") + set_source_files_properties(type_printers.c PROPERTIES + COMPILE_FLAGS "-std=gnu11") + endif() +endif() + +set_flags(test.cpp "${MSVC_WARNINGS}" "${GCC_WARNINGS} -std=gnu++98" "${CLANG_WARNINGS}") +set_flags(test_no_exceptions.cpp "${MSVC_WARNINGS} /EHs-c-" "${GCC_WARNINGS} -fno-exceptions -std=gnu++98" "${CLANG_WARNINGS}") + +list(FIND CMAKE_CXX_COMPILE_FEATURES cxx_std_11 IDX) +if (${IDX} GREATER -1) + set_flags(test11.cpp "${MSVC_WARNINGS}" "${GCC_WARNINGS} -std=c++11" "${CLANG_WARNINGS}") +endif() + +list(FIND CMAKE_CXX_COMPILE_FEATURES cxx_std_14 IDX) +if (${IDX} GREATER -1) + set_flags(test14.cpp "${MSVC_WARNINGS} /std:c++14" "${GCC_WARNINGS} -std=c++14" "${CLANG_WARNINGS}") +endif() + +list(FIND CMAKE_CXX_COMPILE_FEATURES cxx_std_17 IDX) +if (${IDX} GREATER -1) + set_flags(test17.cpp "${MSVC_WARNINGS} /std:c++17" "${GCC_WARNINGS} -std=c++17" "${CLANG_WARNINGS}") +endif() + +set(SOURCES + ../utest.h + main.c + test.c + test11.c + test99.c + test.cpp + test11.cpp + test14.cpp + test17.cpp + test_no_exceptions.cpp + stdint_include.c + type_printers.c + type_printers.cpp + side_effects.c + side_effects.cpp +) + +add_executable(utest_test ${SOURCES}) + +if(NOT "${UTEST_USE_SANITIZER}" STREQUAL "") + target_compile_options(utest_test PUBLIC -fno-omit-frame-pointer -fsanitize=${UTEST_USE_SANITIZER}) + target_link_options(utest_test PUBLIC -fno-omit-frame-pointer -fsanitize=${UTEST_USE_SANITIZER}) +endif() + +add_executable(utest_test_wpo ${SOURCES}) + +if(NOT "${UTEST_USE_SANITIZER}" STREQUAL "") + target_compile_options(utest_test_wpo PUBLIC -fno-omit-frame-pointer -fsanitize=${UTEST_USE_SANITIZER}) + target_link_options(utest_test_wpo PUBLIC -fno-omit-frame-pointer -fsanitize=${UTEST_USE_SANITIZER}) +endif() + +if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") + target_compile_options(utest_test_wpo PRIVATE "$<$<CONFIG:RELEASE>:/GL>") + target_link_options(utest_test_wpo PRIVATE "$<$<CONFIG:RELEASE>:/LTCG>") +endif() + +add_executable(utest_test_mt ${SOURCES}) + +if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") + if(CMAKE_CXX_COMPILER_FRONTEND_VARIANT STREQUAL "MSVC") + target_compile_options(utest_test_mt PUBLIC "/MT") + endif() +elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") + target_compile_options(utest_test_mt PUBLIC "/MT") +endif() diff --git a/third_party/utest/test/main.c b/third_party/utest/test/main.c @@ -0,0 +1,111 @@ +// This is free and unencumbered software released into the public domain. +// +// Anyone is free to copy, modify, publish, use, compile, sell, or +// distribute this software, either in source code form or as a compiled +// binary, for any purpose, commercial or non-commercial, and by any +// means. +// +// In jurisdictions that recognize copyright laws, the author or authors +// of this software dedicate any and all copyright interest in the +// software to the public domain. We make this dedication for the benefit +// of the public at large and to the detriment of our heirs and +// successors. We intend this dedication to be an overt act of +// relinquishment in perpetuity of all present and future rights to this +// software under copyright law. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR +// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +// +// For more information, please refer to <http://unlicense.org/> + +#include "utest.h" + +// TODO: Fix in subprocess.h! +#if defined(__clang__) +#if __has_warning("-Wunsafe-buffer-usage") +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wunsafe-buffer-usage" +#endif +#endif + +#include "subprocess.h" + +// TODO: Broken under MINGW for some reason. +#if !(defined(__MINGW32__) || defined(__MINGW64__)) +UTEST(utest_cmdline, filter_with_list) { + struct subprocess_s process; + const char *command[3] = {"utest_test", "--list-tests", 0}; + int return_code; + FILE *stdout_file; + size_t index, kndex; + char *hits; + +// 64k should be enough for anyone +#define MAX_CHARS (64 * 1024) + char buffer[MAX_CHARS] = {0}; + + hits = (char *)malloc(utest_state.tests_length); + memset(hits, 0, utest_state.tests_length); + + ASSERT_EQ(0, + subprocess_create(command, subprocess_option_combined_stdout_stderr, + &process)); + + stdout_file = subprocess_stdout(&process); + + for (index = 0; index < utest_state.tests_length; index++) { + if (buffer != fgets(buffer, MAX_CHARS, stdout_file)) { + break; + } + +#if defined(__clang__) +#if __has_warning("-Wdisabled-macro-expansion") +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wdisabled-macro-expansion" +#endif +#endif + + // First wipe out the newlines from the fgets. + for (kndex = 0;; kndex++) { + if ((buffer[kndex] == '\r') || (buffer[kndex] == '\n')) { + buffer[kndex] = '\0'; + break; + } + } + + // Record the hit for listed test. + for (kndex = 0; kndex < utest_state.tests_length; kndex++) { + if (0 == strcmp(buffer, utest_state.tests[kndex].name)) { + ASSERT_EQ(hits[kndex], 0); + hits[kndex] = 1; + break; + } + } + +#if defined(__clang__) +#if __has_warning("-Wdisabled-macro-expansion") +#pragma clang diagnostic pop +#endif +#endif + } + + ASSERT_EQ(0, subprocess_join(&process, &return_code)); + ASSERT_EQ(0, return_code); + + ASSERT_EQ(0, subprocess_destroy(&process)); + + // Run through all the hits and make sure we got exactly one for each. + for (kndex = 0; kndex < utest_state.tests_length; kndex++) { + ASSERT_EQ(hits[kndex], 1); + } + + free(hits); +} +#endif + +UTEST_MAIN() diff --git a/third_party/utest/test/side_effects.c b/third_party/utest/test/side_effects.c @@ -0,0 +1,138 @@ +/* + This is free and unencumbered software released into the public domain. + + Anyone is free to copy, modify, publish, use, compile, sell, or + distribute this software, either in source code form or as a compiled + binary, for any purpose, commercial or non-commercial, and by any + means. + + In jurisdictions that recognize copyright laws, the author or authors + of this software dedicate any and all copyright interest in the + software to the public domain. We make this dedication for the benefit + of the public at large and to the detriment of our heirs and + successors. We intend this dedication to be an overt act of + relinquishment in perpetuity of all present and future rights to this + software under copyright law. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR + OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + OTHER DEALINGS IN THE SOFTWARE. + + For more information, please refer to <http://unlicense.org/> +*/ + +#include "utest.h" + +struct reader { + int offset; +}; + +static int next(struct reader *reader) { + const int current = reader->offset; + reader->offset += 1; + return current; +} + +static const int bools[20] = {0, 1, 0, 1}; +static const int ints[20] = {42, 13, 6, -53}; +static const char *const strings[20] = {"42", "13", "6", "-53"}; + +UTEST(c_side_effects, EXPECT_TRUE_EXPECT_FALSE) { + struct reader reader = {0}; + EXPECT_FALSE(bools[next(&reader)]); + EXPECT_TRUE(bools[next(&reader)]); + EXPECT_FALSE(bools[next(&reader)]); + EXPECT_TRUE(bools[next(&reader)]); +} + +UTEST(c_side_effects, EXPECT_EQ_EXPECT_NE) { + struct reader reader = {0}; + EXPECT_EQ(42, ints[next(&reader)]); + EXPECT_NE(14, ints[next(&reader)]); + EXPECT_EQ(6, ints[next(&reader)]); + EXPECT_NE(52, ints[next(&reader)]); +} + +UTEST(c_side_effects, EXPECT_LT_EXPECT_LE) { + struct reader reader = {0}; + EXPECT_LT(41, ints[next(&reader)]); + EXPECT_LE(13, ints[next(&reader)]); + EXPECT_LT(5, ints[next(&reader)]); + EXPECT_LE(-53, ints[next(&reader)]); +} + +UTEST(c_side_effects, EXPECT_GT_EXPECT_GE) { + struct reader reader = {0}; + EXPECT_GT(43, ints[next(&reader)]); + EXPECT_GE(13, ints[next(&reader)]); + EXPECT_GT(7, ints[next(&reader)]); + EXPECT_GE(-53, ints[next(&reader)]); +} + +UTEST(c_side_effects, EXPECT_STREQ_EXPECT_STRNE) { + struct reader reader = {0}; + EXPECT_STRNE("!42", strings[next(&reader)]); + EXPECT_STREQ("13", strings[next(&reader)]); + EXPECT_STRNE("!6", strings[next(&reader)]); + EXPECT_STREQ("-53", strings[next(&reader)]); +} + +UTEST(c_side_effects, EXPECT_STRNEQ_EXPECT_STRNEQ) { + struct reader reader = {0}; + EXPECT_STRNNE("!42", strings[next(&reader)], 3); + EXPECT_STRNEQ("13", strings[next(&reader)], 2); + EXPECT_STRNNE("!6", strings[next(&reader)], 2); + EXPECT_STRNEQ("-53", strings[next(&reader)], 3); +} + +UTEST(c_side_effects, ASSERT_TRUE_ASSERT_FALSE) { + struct reader reader = {0}; + ASSERT_FALSE(bools[next(&reader)]); + ASSERT_TRUE(bools[next(&reader)]); + ASSERT_FALSE(bools[next(&reader)]); + ASSERT_TRUE(bools[next(&reader)]); +} + +UTEST(c_side_effects, ASSERT_EQ_ASSERT_NE) { + struct reader reader = {0}; + ASSERT_EQ(42, ints[next(&reader)]); + ASSERT_NE(14, ints[next(&reader)]); + ASSERT_EQ(6, ints[next(&reader)]); + ASSERT_NE(52, ints[next(&reader)]); +} + +UTEST(c_side_effects, ASSERT_LT_ASSERT_LE) { + struct reader reader = {0}; + ASSERT_LT(41, ints[next(&reader)]); + ASSERT_LE(13, ints[next(&reader)]); + ASSERT_LT(5, ints[next(&reader)]); + ASSERT_LE(-53, ints[next(&reader)]); +} + +UTEST(c_side_effects, ASSERT_GT_ASSERT_GE) { + struct reader reader = {0}; + ASSERT_GT(43, ints[next(&reader)]); + ASSERT_GE(13, ints[next(&reader)]); + ASSERT_GT(7, ints[next(&reader)]); + ASSERT_GE(-53, ints[next(&reader)]); +} + +UTEST(c_side_effects, ASSERT_STREQ_ASSERT_STRNE) { + struct reader reader = {0}; + ASSERT_STRNE("!42", strings[next(&reader)]); + ASSERT_STREQ("13", strings[next(&reader)]); + ASSERT_STRNE("!6", strings[next(&reader)]); + ASSERT_STREQ("-53", strings[next(&reader)]); +} + +UTEST(c_side_effects, ASSERT_STRNEQ_ASSERT_STRNEQ) { + struct reader reader = {0}; + ASSERT_STRNNE("!42", strings[next(&reader)], 3); + ASSERT_STRNEQ("13", strings[next(&reader)], 2); + ASSERT_STRNNE("!6", strings[next(&reader)], 2); + ASSERT_STRNEQ("-53", strings[next(&reader)], 3); +} diff --git a/third_party/utest/test/side_effects.cpp b/third_party/utest/test/side_effects.cpp @@ -0,0 +1,136 @@ +// This is free and unencumbered software released into the public domain. +// +// Anyone is free to copy, modify, publish, use, compile, sell, or +// distribute this software, either in source code form or as a compiled +// binary, for any purpose, commercial or non-commercial, and by any +// means. +// +// In jurisdictions that recognize copyright laws, the author or authors +// of this software dedicate any and all copyright interest in the +// software to the public domain. We make this dedication for the benefit +// of the public at large and to the detriment of our heirs and +// successors. We intend this dedication to be an overt act of +// relinquishment in perpetuity of all present and future rights to this +// software under copyright law. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR +// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +// +// For more information, please refer to <http://unlicense.org/> + +#include "utest.h" + +struct reader { + int offset; +}; + +static int next(struct reader *reader) { + const int current = reader->offset; + reader->offset += 1; + return current; +} + +static const int bools[20] = {0, 1, 0, 1}; +static const int ints[20] = {42, 13, 6, -53}; +static const char *const strings[20] = {"42", "13", "6", "-53"}; + +UTEST(cpp_side_effects, EXPECT_TRUE_EXPECT_FALSE) { + struct reader reader = {0}; + EXPECT_FALSE(bools[next(&reader)]); + EXPECT_TRUE(bools[next(&reader)]); + EXPECT_FALSE(bools[next(&reader)]); + EXPECT_TRUE(bools[next(&reader)]); +} + +UTEST(cpp_side_effects, EXPECT_EQ_EXPECT_NE) { + struct reader reader = {0}; + EXPECT_EQ(42, ints[next(&reader)]); + EXPECT_NE(14, ints[next(&reader)]); + EXPECT_EQ(6, ints[next(&reader)]); + EXPECT_NE(52, ints[next(&reader)]); +} + +UTEST(cpp_side_effects, EXPECT_LT_EXPECT_LE) { + struct reader reader = {0}; + EXPECT_LT(41, ints[next(&reader)]); + EXPECT_LE(13, ints[next(&reader)]); + EXPECT_LT(5, ints[next(&reader)]); + EXPECT_LE(-53, ints[next(&reader)]); +} + +UTEST(cpp_side_effects, EXPECT_GT_EXPECT_GE) { + struct reader reader = {0}; + EXPECT_GT(43, ints[next(&reader)]); + EXPECT_GE(13, ints[next(&reader)]); + EXPECT_GT(7, ints[next(&reader)]); + EXPECT_GE(-53, ints[next(&reader)]); +} + +UTEST(cpp_side_effects, EXPECT_STREQ_EXPECT_STRNE) { + struct reader reader = {0}; + EXPECT_STRNE("!42", strings[next(&reader)]); + EXPECT_STREQ("13", strings[next(&reader)]); + EXPECT_STRNE("!6", strings[next(&reader)]); + EXPECT_STREQ("-53", strings[next(&reader)]); +} + +UTEST(cpp_side_effects, EXPECT_STRNEQ_EXPECT_STRNEQ) { + struct reader reader = {0}; + EXPECT_STRNNE("!42", strings[next(&reader)], 3); + EXPECT_STRNEQ("13", strings[next(&reader)], 2); + EXPECT_STRNNE("!6", strings[next(&reader)], 2); + EXPECT_STRNEQ("-53", strings[next(&reader)], 3); +} + +UTEST(cpp_side_effects, ASSERT_TRUE_ASSERT_FALSE) { + struct reader reader = {0}; + ASSERT_FALSE(bools[next(&reader)]); + ASSERT_TRUE(bools[next(&reader)]); + ASSERT_FALSE(bools[next(&reader)]); + ASSERT_TRUE(bools[next(&reader)]); +} + +UTEST(cpp_side_effects, ASSERT_EQ_ASSERT_NE) { + struct reader reader = {0}; + ASSERT_EQ(42, ints[next(&reader)]); + ASSERT_NE(14, ints[next(&reader)]); + ASSERT_EQ(6, ints[next(&reader)]); + ASSERT_NE(52, ints[next(&reader)]); +} + +UTEST(cpp_side_effects, ASSERT_LT_ASSERT_LE) { + struct reader reader = {0}; + ASSERT_LT(41, ints[next(&reader)]); + ASSERT_LE(13, ints[next(&reader)]); + ASSERT_LT(5, ints[next(&reader)]); + ASSERT_LE(-53, ints[next(&reader)]); +} + +UTEST(cpp_side_effects, ASSERT_GT_ASSERT_GE) { + struct reader reader = {0}; + ASSERT_GT(43, ints[next(&reader)]); + ASSERT_GE(13, ints[next(&reader)]); + ASSERT_GT(7, ints[next(&reader)]); + ASSERT_GE(-53, ints[next(&reader)]); +} + +UTEST(cpp_side_effects, ASSERT_STREQ_ASSERT_STRNE) { + struct reader reader = {0}; + ASSERT_STRNE("!42", strings[next(&reader)]); + ASSERT_STREQ("13", strings[next(&reader)]); + ASSERT_STRNE("!6", strings[next(&reader)]); + ASSERT_STREQ("-53", strings[next(&reader)]); +} + +UTEST(cpp_side_effects, ASSERT_STRNEQ_ASSERT_STRNEQ) { + struct reader reader = {0}; + ASSERT_STRNNE("!42", strings[next(&reader)], 3); + ASSERT_STRNEQ("13", strings[next(&reader)], 2); + ASSERT_STRNNE("!6", strings[next(&reader)], 2); + ASSERT_STRNEQ("-53", strings[next(&reader)], 3); +} diff --git a/third_party/utest/test/stdint_include.c b/third_party/utest/test/stdint_include.c @@ -0,0 +1,32 @@ +/* + This is free and unencumbered software released into the public domain. + + Anyone is free to copy, modify, publish, use, compile, sell, or + distribute this software, either in source code form or as a compiled + binary, for any purpose, commercial or non-commercial, and by any + means. + + In jurisdictions that recognize copyright laws, the author or authors + of this software dedicate any and all copyright interest in the + software to the public domain. We make this dedication for the benefit + of the public at large and to the detriment of our heirs and + successors. We intend this dedication to be an overt act of + relinquishment in perpetuity of all present and future rights to this + software under copyright law. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR + OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + OTHER DEALINGS IN THE SOFTWARE. + + For more information, please refer to <http://unlicense.org/> +*/ + +#if defined(_MSC_VER) && (_MSC_VER >= 1600) +#include <stdint.h> +#endif + +#include "utest.h" diff --git a/third_party/utest/test/subprocess.h b/third_party/utest/test/subprocess.h @@ -0,0 +1,1186 @@ +/* + The latest version of this library is available on GitHub; + https://github.com/sheredom/subprocess.h +*/ + +/* + This is free and unencumbered software released into the public domain. + + Anyone is free to copy, modify, publish, use, compile, sell, or + distribute this software, either in source code form or as a compiled + binary, for any purpose, commercial or non-commercial, and by any + means. + + In jurisdictions that recognize copyright laws, the author or authors + of this software dedicate any and all copyright interest in the + software to the public domain. We make this dedication for the benefit + of the public at large and to the detriment of our heirs and + successors. We intend this dedication to be an overt act of + relinquishment in perpetuity of all present and future rights to this + software under copyright law. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR + OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + OTHER DEALINGS IN THE SOFTWARE. + + For more information, please refer to <http://unlicense.org/> +*/ + +#ifndef SHEREDOM_SUBPROCESS_H_INCLUDED +#define SHEREDOM_SUBPROCESS_H_INCLUDED + +#if defined(_MSC_VER) +#pragma warning(push, 1) + +/* disable warning: '__cplusplus' is not defined as a preprocessor macro, + * replacing with '0' for '#if/#elif' */ +#pragma warning(disable : 4668) +#endif + +#include <stdio.h> +#include <string.h> + +#if defined(_MSC_VER) +#pragma warning(pop) +#endif + +#if defined(__TINYC__) +#define subprocess_attribute(a) __attribute((a)) +#else +#define subprocess_attribute(a) __attribute__((a)) +#endif + +#if defined(_MSC_VER) +#define subprocess_pure +#define subprocess_weak __inline +#define subprocess_tls __declspec(thread) +#elif defined(__MINGW32__) +#define subprocess_pure subprocess_attribute(pure) +#define subprocess_weak static subprocess_attribute(used) +#define subprocess_tls __thread +#elif defined(__clang__) || defined(__GNUC__) || defined(__TINYC__) +#define subprocess_pure subprocess_attribute(pure) +#define subprocess_weak subprocess_attribute(weak) +#define subprocess_tls __thread +#else +#error Non clang, non gcc, non MSVC, non tcc compiler found! +#endif + +struct subprocess_s; + +enum subprocess_option_e { + // stdout and stderr are the same FILE. + subprocess_option_combined_stdout_stderr = 0x1, + + // The child process should inherit the environment variables of the parent. + subprocess_option_inherit_environment = 0x2, + + // Enable asynchronous reading of stdout/stderr before it has completed. + subprocess_option_enable_async = 0x4, + + // Enable the child process to be spawned with no window visible if supported + // by the platform. + subprocess_option_no_window = 0x8, + + // Search for program names in the PATH variable. Always enabled on Windows. + // Note: this will **not** search for paths in any provided custom environment + // and instead uses the PATH of the spawning process. + subprocess_option_search_user_path = 0x10 +}; + +#if defined(__cplusplus) +extern "C" { +#endif + +/// @brief Create a process. +/// @param command_line An array of strings for the command line to execute for +/// this process. The last element must be NULL to signify the end of the array. +/// The memory backing this parameter only needs to persist until this function +/// returns. +/// @param options A bit field of subprocess_option_e's to pass. +/// @param out_process The newly created process. +/// @return On success zero is returned. +subprocess_weak int subprocess_create(const char *const command_line[], + int options, + struct subprocess_s *const out_process); + +/// @brief Create a process (extended create). +/// @param command_line An array of strings for the command line to execute for +/// this process. The last element must be NULL to signify the end of the array. +/// The memory backing this parameter only needs to persist until this function +/// returns. +/// @param options A bit field of subprocess_option_e's to pass. +/// @param environment An optional array of strings for the environment to use +/// for a child process (each element of the form FOO=BAR). The last element +/// must be NULL to signify the end of the array. +/// @param out_process The newly created process. +/// @return On success zero is returned. +/// +/// If `options` contains `subprocess_option_inherit_environment`, then +/// `environment` must be NULL. +subprocess_weak int +subprocess_create_ex(const char *const command_line[], int options, + const char *const environment[], + struct subprocess_s *const out_process); + +/// @brief Get the standard input file for a process. +/// @param process The process to query. +/// @return The file for standard input of the process. +/// +/// The file returned can be written to by the parent process to feed data to +/// the standard input of the process. +subprocess_pure subprocess_weak FILE * +subprocess_stdin(const struct subprocess_s *const process); + +/// @brief Get the standard output file for a process. +/// @param process The process to query. +/// @return The file for standard output of the process. +/// +/// The file returned can be read from by the parent process to read data from +/// the standard output of the child process. +subprocess_pure subprocess_weak FILE * +subprocess_stdout(const struct subprocess_s *const process); + +/// @brief Get the standard error file for a process. +/// @param process The process to query. +/// @return The file for standard error of the process. +/// +/// The file returned can be read from by the parent process to read data from +/// the standard error of the child process. +/// +/// If the process was created with the subprocess_option_combined_stdout_stderr +/// option bit set, this function will return NULL, and the subprocess_stdout +/// function should be used for both the standard output and error combined. +subprocess_pure subprocess_weak FILE * +subprocess_stderr(const struct subprocess_s *const process); + +/// @brief Wait for a process to finish execution. +/// @param process The process to wait for. +/// @param out_return_code The return code of the returned process (can be +/// NULL). +/// @return On success zero is returned. +/// +/// Joining a process will close the stdin pipe to the process. +subprocess_weak int subprocess_join(struct subprocess_s *const process, + int *const out_return_code); + +/// @brief Destroy a previously created process. +/// @param process The process to destroy. +/// @return On success zero is returned. +/// +/// If the process to be destroyed had not finished execution, it may out live +/// the parent process. +subprocess_weak int subprocess_destroy(struct subprocess_s *const process); + +/// @brief Terminate a previously created process. +/// @param process The process to terminate. +/// @return On success zero is returned. +/// +/// If the process to be destroyed had not finished execution, it will be +/// terminated (i.e killed). +subprocess_weak int subprocess_terminate(struct subprocess_s *const process); + +/// @brief Read the standard output from the child process. +/// @param process The process to read from. +/// @param buffer The buffer to read into. +/// @param size The maximum number of bytes to read. +/// @return The number of bytes actually read into buffer. Can only be 0 if the +/// process has complete. +/// +/// The only safe way to read from the standard output of a process during it's +/// execution is to use the `subprocess_option_enable_async` option in +/// conjuction with this method. +subprocess_weak unsigned +subprocess_read_stdout(struct subprocess_s *const process, char *const buffer, + unsigned size); + +/// @brief Read the standard error from the child process. +/// @param process The process to read from. +/// @param buffer The buffer to read into. +/// @param size The maximum number of bytes to read. +/// @return The number of bytes actually read into buffer. Can only be 0 if the +/// process has complete. +/// +/// The only safe way to read from the standard error of a process during it's +/// execution is to use the `subprocess_option_enable_async` option in +/// conjuction with this method. +subprocess_weak unsigned +subprocess_read_stderr(struct subprocess_s *const process, char *const buffer, + unsigned size); + +/// @brief Returns if the subprocess is currently still alive and executing. +/// @param process The process to check. +/// @return If the process is still alive non-zero is returned. +subprocess_weak int subprocess_alive(struct subprocess_s *const process); + +#if defined(__cplusplus) +#define SUBPROCESS_CAST(type, x) static_cast<type>(x) +#define SUBPROCESS_PTR_CAST(type, x) reinterpret_cast<type>(x) +#define SUBPROCESS_CONST_CAST(type, x) const_cast<type>(x) +#define SUBPROCESS_NULL NULL +#else +#define SUBPROCESS_CAST(type, x) ((type)(x)) +#define SUBPROCESS_PTR_CAST(type, x) ((type)(x)) +#define SUBPROCESS_CONST_CAST(type, x) ((type)(x)) +#define SUBPROCESS_NULL 0 +#endif + +#if !defined(_WIN32) +#include <signal.h> +#include <spawn.h> +#include <stdlib.h> +#include <sys/types.h> +#include <sys/wait.h> +#include <unistd.h> +#endif + +#if defined(_WIN32) + +#if (_MSC_VER < 1920) +#ifdef _WIN64 +typedef __int64 subprocess_intptr_t; +typedef unsigned __int64 subprocess_size_t; +#else +typedef int subprocess_intptr_t; +typedef unsigned int subprocess_size_t; +#endif +#else +#include <inttypes.h> + +typedef intptr_t subprocess_intptr_t; +typedef size_t subprocess_size_t; +#endif + +#ifdef __clang__ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wreserved-identifier" +#endif + +typedef struct _PROCESS_INFORMATION *LPPROCESS_INFORMATION; +typedef struct _SECURITY_ATTRIBUTES *LPSECURITY_ATTRIBUTES; +typedef struct _STARTUPINFOA *LPSTARTUPINFOA; +typedef struct _OVERLAPPED *LPOVERLAPPED; + +#ifdef __clang__ +#pragma clang diagnostic pop +#endif + +#ifdef _MSC_VER +#pragma warning(push, 1) +#endif +#ifdef __MINGW32__ +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wpedantic" +#endif + +struct subprocess_subprocess_information_s { + void *hProcess; + void *hThread; + unsigned long dwProcessId; + unsigned long dwThreadId; +}; + +struct subprocess_security_attributes_s { + unsigned long nLength; + void *lpSecurityDescriptor; + int bInheritHandle; +}; + +struct subprocess_startup_info_s { + unsigned long cb; + char *lpReserved; + char *lpDesktop; + char *lpTitle; + unsigned long dwX; + unsigned long dwY; + unsigned long dwXSize; + unsigned long dwYSize; + unsigned long dwXCountChars; + unsigned long dwYCountChars; + unsigned long dwFillAttribute; + unsigned long dwFlags; + unsigned short wShowWindow; + unsigned short cbReserved2; + unsigned char *lpReserved2; + void *hStdInput; + void *hStdOutput; + void *hStdError; +}; + +struct subprocess_overlapped_s { + uintptr_t Internal; + uintptr_t InternalHigh; + union { + struct { + unsigned long Offset; + unsigned long OffsetHigh; + } DUMMYSTRUCTNAME; + void *Pointer; + } DUMMYUNIONNAME; + + void *hEvent; +}; + +#ifdef __MINGW32__ +#pragma GCC diagnostic pop +#endif +#ifdef _MSC_VER +#pragma warning(pop) +#endif + +__declspec(dllimport) unsigned long __stdcall GetLastError(void); +__declspec(dllimport) int __stdcall SetHandleInformation(void *, unsigned long, + unsigned long); +__declspec(dllimport) int __stdcall CreatePipe(void **, void **, + LPSECURITY_ATTRIBUTES, + unsigned long); +__declspec(dllimport) void *__stdcall CreateNamedPipeA( + const char *, unsigned long, unsigned long, unsigned long, unsigned long, + unsigned long, unsigned long, LPSECURITY_ATTRIBUTES); +__declspec(dllimport) int __stdcall ReadFile(void *, void *, unsigned long, + unsigned long *, LPOVERLAPPED); +__declspec(dllimport) unsigned long __stdcall GetCurrentProcessId(void); +__declspec(dllimport) unsigned long __stdcall GetCurrentThreadId(void); +__declspec(dllimport) void *__stdcall CreateFileA(const char *, unsigned long, + unsigned long, + LPSECURITY_ATTRIBUTES, + unsigned long, unsigned long, + void *); +__declspec(dllimport) void *__stdcall CreateEventA(LPSECURITY_ATTRIBUTES, int, + int, const char *); +__declspec(dllimport) int __stdcall CreateProcessA( + const char *, char *, LPSECURITY_ATTRIBUTES, LPSECURITY_ATTRIBUTES, int, + unsigned long, void *, const char *, LPSTARTUPINFOA, LPPROCESS_INFORMATION); +__declspec(dllimport) int __stdcall CloseHandle(void *); +__declspec(dllimport) unsigned long __stdcall WaitForSingleObject( + void *, unsigned long); +__declspec(dllimport) int __stdcall GetExitCodeProcess( + void *, unsigned long *lpExitCode); +__declspec(dllimport) int __stdcall TerminateProcess(void *, unsigned int); +__declspec(dllimport) unsigned long __stdcall WaitForMultipleObjects( + unsigned long, void *const *, int, unsigned long); +__declspec(dllimport) int __stdcall GetOverlappedResult(void *, LPOVERLAPPED, + unsigned long *, int); + +#if defined(_DLL) +#define SUBPROCESS_DLLIMPORT __declspec(dllimport) +#else +#define SUBPROCESS_DLLIMPORT +#endif + +#ifdef __clang__ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wreserved-identifier" +#endif + +SUBPROCESS_DLLIMPORT int __cdecl _fileno(FILE *); +SUBPROCESS_DLLIMPORT int __cdecl _open_osfhandle(subprocess_intptr_t, int); +SUBPROCESS_DLLIMPORT subprocess_intptr_t __cdecl _get_osfhandle(int); + +#ifndef __MINGW32__ +void *__cdecl _alloca(subprocess_size_t); +#else +#include <malloc.h> +#endif + +#ifdef __clang__ +#pragma clang diagnostic pop +#endif + +#else +typedef size_t subprocess_size_t; +#endif + +#ifdef __clang__ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wpadded" +#endif +struct subprocess_s { + FILE *stdin_file; + FILE *stdout_file; + FILE *stderr_file; + +#if defined(_WIN32) + void *hProcess; + void *hStdInput; + void *hEventOutput; + void *hEventError; +#else + pid_t child; + int return_status; +#endif + + subprocess_size_t alive; +}; +#ifdef __clang__ +#pragma clang diagnostic pop +#endif + +#if defined(_WIN32) +subprocess_weak int subprocess_create_named_pipe_helper(void **rd, void **wr); +int subprocess_create_named_pipe_helper(void **rd, void **wr) { + const unsigned long pipeAccessInbound = 0x00000001; + const unsigned long fileFlagOverlapped = 0x40000000; + const unsigned long pipeTypeByte = 0x00000000; + const unsigned long pipeWait = 0x00000000; + const unsigned long genericWrite = 0x40000000; + const unsigned long openExisting = 3; + const unsigned long fileAttributeNormal = 0x00000080; + const void *const invalidHandleValue = + SUBPROCESS_PTR_CAST(void *, ~(SUBPROCESS_CAST(subprocess_intptr_t, 0))); + struct subprocess_security_attributes_s saAttr = {sizeof(saAttr), + SUBPROCESS_NULL, 1}; + char name[256] = {0}; + static subprocess_tls long index = 0; + const long unique = index++; + +#if defined(_MSC_VER) && _MSC_VER < 1900 +#pragma warning(push, 1) +#pragma warning(disable : 4996) + _snprintf(name, sizeof(name) - 1, + "\\\\.\\pipe\\sheredom_subprocess_h.%08lx.%08lx.%ld", + GetCurrentProcessId(), GetCurrentThreadId(), unique); +#pragma warning(pop) +#else + snprintf(name, sizeof(name) - 1, + "\\\\.\\pipe\\sheredom_subprocess_h.%08lx.%08lx.%ld", + GetCurrentProcessId(), GetCurrentThreadId(), unique); +#endif + + *rd = + CreateNamedPipeA(name, pipeAccessInbound | fileFlagOverlapped, + pipeTypeByte | pipeWait, 1, 4096, 4096, SUBPROCESS_NULL, + SUBPROCESS_PTR_CAST(LPSECURITY_ATTRIBUTES, &saAttr)); + + if (invalidHandleValue == *rd) { + return -1; + } + + *wr = CreateFileA(name, genericWrite, SUBPROCESS_NULL, + SUBPROCESS_PTR_CAST(LPSECURITY_ATTRIBUTES, &saAttr), + openExisting, fileAttributeNormal, SUBPROCESS_NULL); + + if (invalidHandleValue == *wr) { + return -1; + } + + return 0; +} +#endif + +int subprocess_create(const char *const commandLine[], int options, + struct subprocess_s *const out_process) { + return subprocess_create_ex(commandLine, options, SUBPROCESS_NULL, + out_process); +} + +int subprocess_create_ex(const char *const commandLine[], int options, + const char *const environment[], + struct subprocess_s *const out_process) { +#if defined(_WIN32) + int fd; + void *rd, *wr; + char *commandLineCombined; + subprocess_size_t len; + int i, j; + int need_quoting; + unsigned long flags = 0; + const unsigned long startFUseStdHandles = 0x00000100; + const unsigned long handleFlagInherit = 0x00000001; + const unsigned long createNoWindow = 0x08000000; + struct subprocess_subprocess_information_s processInfo; + struct subprocess_security_attributes_s saAttr = {sizeof(saAttr), + SUBPROCESS_NULL, 1}; + char *used_environment = SUBPROCESS_NULL; + struct subprocess_startup_info_s startInfo = {0, + SUBPROCESS_NULL, + SUBPROCESS_NULL, + SUBPROCESS_NULL, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + SUBPROCESS_NULL, + SUBPROCESS_NULL, + SUBPROCESS_NULL, + SUBPROCESS_NULL}; + + startInfo.cb = sizeof(startInfo); + startInfo.dwFlags = startFUseStdHandles; + + if (subprocess_option_no_window == (options & subprocess_option_no_window)) { + flags |= createNoWindow; + } + + if (subprocess_option_inherit_environment != + (options & subprocess_option_inherit_environment)) { + if (SUBPROCESS_NULL == environment) { + used_environment = SUBPROCESS_CONST_CAST(char *, "\0\0"); + } else { + // We always end with two null terminators. + len = 2; + + for (i = 0; environment[i]; i++) { + for (j = 0; '\0' != environment[i][j]; j++) { + len++; + } + + // For the null terminator too. + len++; + } + + used_environment = SUBPROCESS_CAST(char *, _alloca(len)); + + // Re-use len for the insertion position + len = 0; + + for (i = 0; environment[i]; i++) { + for (j = 0; '\0' != environment[i][j]; j++) { + used_environment[len++] = environment[i][j]; + } + + used_environment[len++] = '\0'; + } + + // End with the two null terminators. + used_environment[len++] = '\0'; + used_environment[len++] = '\0'; + } + } else { + if (SUBPROCESS_NULL != environment) { + return -1; + } + } + + if (!CreatePipe(&rd, &wr, SUBPROCESS_PTR_CAST(LPSECURITY_ATTRIBUTES, &saAttr), + 0)) { + return -1; + } + + if (!SetHandleInformation(wr, handleFlagInherit, 0)) { + return -1; + } + + fd = _open_osfhandle(SUBPROCESS_PTR_CAST(subprocess_intptr_t, wr), 0); + + if (-1 != fd) { + out_process->stdin_file = _fdopen(fd, "wb"); + + if (SUBPROCESS_NULL == out_process->stdin_file) { + return -1; + } + } + + startInfo.hStdInput = rd; + + if (options & subprocess_option_enable_async) { + if (subprocess_create_named_pipe_helper(&rd, &wr)) { + return -1; + } + } else { + if (!CreatePipe(&rd, &wr, + SUBPROCESS_PTR_CAST(LPSECURITY_ATTRIBUTES, &saAttr), 0)) { + return -1; + } + } + + if (!SetHandleInformation(rd, handleFlagInherit, 0)) { + return -1; + } + + fd = _open_osfhandle(SUBPROCESS_PTR_CAST(subprocess_intptr_t, rd), 0); + + if (-1 != fd) { + out_process->stdout_file = _fdopen(fd, "rb"); + + if (SUBPROCESS_NULL == out_process->stdout_file) { + return -1; + } + } + + startInfo.hStdOutput = wr; + + if (subprocess_option_combined_stdout_stderr == + (options & subprocess_option_combined_stdout_stderr)) { + out_process->stderr_file = out_process->stdout_file; + startInfo.hStdError = startInfo.hStdOutput; + } else { + if (options & subprocess_option_enable_async) { + if (subprocess_create_named_pipe_helper(&rd, &wr)) { + return -1; + } + } else { + if (!CreatePipe(&rd, &wr, + SUBPROCESS_PTR_CAST(LPSECURITY_ATTRIBUTES, &saAttr), 0)) { + return -1; + } + } + + if (!SetHandleInformation(rd, handleFlagInherit, 0)) { + return -1; + } + + fd = _open_osfhandle(SUBPROCESS_PTR_CAST(subprocess_intptr_t, rd), 0); + + if (-1 != fd) { + out_process->stderr_file = _fdopen(fd, "rb"); + + if (SUBPROCESS_NULL == out_process->stderr_file) { + return -1; + } + } + + startInfo.hStdError = wr; + } + + if (options & subprocess_option_enable_async) { + out_process->hEventOutput = + CreateEventA(SUBPROCESS_PTR_CAST(LPSECURITY_ATTRIBUTES, &saAttr), 1, 1, + SUBPROCESS_NULL); + out_process->hEventError = + CreateEventA(SUBPROCESS_PTR_CAST(LPSECURITY_ATTRIBUTES, &saAttr), 1, 1, + SUBPROCESS_NULL); + } else { + out_process->hEventOutput = SUBPROCESS_NULL; + out_process->hEventError = SUBPROCESS_NULL; + } + + // Combine commandLine together into a single string + len = 0; + for (i = 0; commandLine[i]; i++) { + // for the trailing \0 + len++; + + // Quote the argument if it has a space in it + if (strpbrk(commandLine[i], "\t\v ") != SUBPROCESS_NULL) + len += 2; + + for (j = 0; '\0' != commandLine[i][j]; j++) { + switch (commandLine[i][j]) { + default: + break; + case '\\': + if (commandLine[i][j + 1] == '"') { + len++; + } + + break; + case '"': + len++; + break; + } + len++; + } + } + + commandLineCombined = SUBPROCESS_CAST(char *, _alloca(len)); + + if (!commandLineCombined) { + return -1; + } + + // Gonna re-use len to store the write index into commandLineCombined + len = 0; + + for (i = 0; commandLine[i]; i++) { + if (0 != i) { + commandLineCombined[len++] = ' '; + } + + need_quoting = strpbrk(commandLine[i], "\t\v ") != SUBPROCESS_NULL; + if (need_quoting) { + commandLineCombined[len++] = '"'; + } + + for (j = 0; '\0' != commandLine[i][j]; j++) { + switch (commandLine[i][j]) { + default: + break; + case '\\': + if (commandLine[i][j + 1] == '"') { + commandLineCombined[len++] = '\\'; + } + + break; + case '"': + commandLineCombined[len++] = '\\'; + break; + } + + commandLineCombined[len++] = commandLine[i][j]; + } + if (need_quoting) { + commandLineCombined[len++] = '"'; + } + } + + commandLineCombined[len] = '\0'; + + if (!CreateProcessA( + SUBPROCESS_NULL, + commandLineCombined, // command line + SUBPROCESS_NULL, // process security attributes + SUBPROCESS_NULL, // primary thread security attributes + 1, // handles are inherited + flags, // creation flags + used_environment, // used environment + SUBPROCESS_NULL, // use parent's current directory + SUBPROCESS_PTR_CAST(LPSTARTUPINFOA, + &startInfo), // STARTUPINFO pointer + SUBPROCESS_PTR_CAST(LPPROCESS_INFORMATION, &processInfo))) { + return -1; + } + + out_process->hProcess = processInfo.hProcess; + + out_process->hStdInput = startInfo.hStdInput; + + // We don't need the handle of the primary thread in the called process. + CloseHandle(processInfo.hThread); + + if (SUBPROCESS_NULL != startInfo.hStdOutput) { + CloseHandle(startInfo.hStdOutput); + + if (startInfo.hStdError != startInfo.hStdOutput) { + CloseHandle(startInfo.hStdError); + } + } + + out_process->alive = 1; + + return 0; +#else + int stdinfd[2]; + int stdoutfd[2]; + int stderrfd[2]; + pid_t child; + extern char **environ; + char *const empty_environment[1] = {SUBPROCESS_NULL}; + posix_spawn_file_actions_t actions; + char *const *used_environment; + + if (subprocess_option_inherit_environment == + (options & subprocess_option_inherit_environment)) { + if (SUBPROCESS_NULL != environment) { + return -1; + } + } + + if (0 != pipe(stdinfd)) { + return -1; + } + + if (0 != pipe(stdoutfd)) { + return -1; + } + + if (subprocess_option_combined_stdout_stderr != + (options & subprocess_option_combined_stdout_stderr)) { + if (0 != pipe(stderrfd)) { + return -1; + } + } + + if (environment) { +#ifdef __clang__ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wcast-qual" +#pragma clang diagnostic ignored "-Wold-style-cast" +#endif + used_environment = (char *const *)environment; +#ifdef __clang__ +#pragma clang diagnostic pop +#endif + } else if (subprocess_option_inherit_environment == + (options & subprocess_option_inherit_environment)) { + used_environment = environ; + } else { + used_environment = empty_environment; + } + + if (0 != posix_spawn_file_actions_init(&actions)) { + return -1; + } + + // Close the stdin write end + if (0 != posix_spawn_file_actions_addclose(&actions, stdinfd[1])) { + posix_spawn_file_actions_destroy(&actions); + return -1; + } + + // Map the read end to stdin + if (0 != + posix_spawn_file_actions_adddup2(&actions, stdinfd[0], STDIN_FILENO)) { + posix_spawn_file_actions_destroy(&actions); + return -1; + } + + // Close the stdout read end + if (0 != posix_spawn_file_actions_addclose(&actions, stdoutfd[0])) { + posix_spawn_file_actions_destroy(&actions); + return -1; + } + + // Map the write end to stdout + if (0 != + posix_spawn_file_actions_adddup2(&actions, stdoutfd[1], STDOUT_FILENO)) { + posix_spawn_file_actions_destroy(&actions); + return -1; + } + + if (subprocess_option_combined_stdout_stderr == + (options & subprocess_option_combined_stdout_stderr)) { + if (0 != posix_spawn_file_actions_adddup2(&actions, STDOUT_FILENO, + STDERR_FILENO)) { + posix_spawn_file_actions_destroy(&actions); + return -1; + } + } else { + // Close the stderr read end + if (0 != posix_spawn_file_actions_addclose(&actions, stderrfd[0])) { + posix_spawn_file_actions_destroy(&actions); + return -1; + } + // Map the write end to stdout + if (0 != posix_spawn_file_actions_adddup2(&actions, stderrfd[1], + STDERR_FILENO)) { + posix_spawn_file_actions_destroy(&actions); + return -1; + } + } + +#ifdef __clang__ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wcast-qual" +#pragma clang diagnostic ignored "-Wold-style-cast" +#endif + if (subprocess_option_search_user_path == + (options & subprocess_option_search_user_path)) { + if (0 != posix_spawnp(&child, commandLine[0], &actions, SUBPROCESS_NULL, + (char *const *)commandLine, used_environment)) { + posix_spawn_file_actions_destroy(&actions); + return -1; + } + } else { + if (0 != posix_spawn(&child, commandLine[0], &actions, SUBPROCESS_NULL, + (char *const *)commandLine, used_environment)) { + posix_spawn_file_actions_destroy(&actions); + return -1; + } + } +#ifdef __clang__ +#pragma clang diagnostic pop +#endif + + // Close the stdin read end + close(stdinfd[0]); + // Store the stdin write end + out_process->stdin_file = fdopen(stdinfd[1], "wb"); + + // Close the stdout write end + close(stdoutfd[1]); + // Store the stdout read end + out_process->stdout_file = fdopen(stdoutfd[0], "rb"); + + if (subprocess_option_combined_stdout_stderr == + (options & subprocess_option_combined_stdout_stderr)) { + out_process->stderr_file = out_process->stdout_file; + } else { + // Close the stderr write end + close(stderrfd[1]); + // Store the stderr read end + out_process->stderr_file = fdopen(stderrfd[0], "rb"); + } + + // Store the child's pid + out_process->child = child; + + out_process->alive = 1; + + posix_spawn_file_actions_destroy(&actions); + return 0; +#endif +} + +FILE *subprocess_stdin(const struct subprocess_s *const process) { + return process->stdin_file; +} + +FILE *subprocess_stdout(const struct subprocess_s *const process) { + return process->stdout_file; +} + +FILE *subprocess_stderr(const struct subprocess_s *const process) { + if (process->stdout_file != process->stderr_file) { + return process->stderr_file; + } else { + return SUBPROCESS_NULL; + } +} + +int subprocess_join(struct subprocess_s *const process, + int *const out_return_code) { +#if defined(_WIN32) + const unsigned long infinite = 0xFFFFFFFF; + + if (process->stdin_file) { + fclose(process->stdin_file); + process->stdin_file = SUBPROCESS_NULL; + } + + if (process->hStdInput) { + CloseHandle(process->hStdInput); + process->hStdInput = SUBPROCESS_NULL; + } + + WaitForSingleObject(process->hProcess, infinite); + + if (out_return_code) { + if (!GetExitCodeProcess( + process->hProcess, + SUBPROCESS_PTR_CAST(unsigned long *, out_return_code))) { + return -1; + } + } + + process->alive = 0; + + return 0; +#else + int status; + + if (process->stdin_file) { + fclose(process->stdin_file); + process->stdin_file = SUBPROCESS_NULL; + } + + if (process->child) { + if (process->child != waitpid(process->child, &status, 0)) { + return -1; + } + + process->child = 0; + + if (WIFEXITED(status)) { + process->return_status = WEXITSTATUS(status); + } else { + process->return_status = EXIT_FAILURE; + } + + process->alive = 0; + } + + if (out_return_code) { + *out_return_code = process->return_status; + } + + return 0; +#endif +} + +int subprocess_destroy(struct subprocess_s *const process) { + if (process->stdin_file) { + fclose(process->stdin_file); + process->stdin_file = SUBPROCESS_NULL; + } + + if (process->stdout_file) { + fclose(process->stdout_file); + + if (process->stdout_file != process->stderr_file) { + fclose(process->stderr_file); + } + + process->stdout_file = SUBPROCESS_NULL; + process->stderr_file = SUBPROCESS_NULL; + } + +#if defined(_WIN32) + if (process->hProcess) { + CloseHandle(process->hProcess); + process->hProcess = SUBPROCESS_NULL; + + if (process->hStdInput) { + CloseHandle(process->hStdInput); + } + + if (process->hEventOutput) { + CloseHandle(process->hEventOutput); + } + + if (process->hEventError) { + CloseHandle(process->hEventError); + } + } +#endif + + return 0; +} + +int subprocess_terminate(struct subprocess_s *const process) { +#if defined(_WIN32) + unsigned int killed_process_exit_code; + int success_terminate; + int windows_call_result; + + killed_process_exit_code = 99; + windows_call_result = + TerminateProcess(process->hProcess, killed_process_exit_code); + success_terminate = (windows_call_result == 0) ? 1 : 0; + return success_terminate; +#else + int result; + result = kill(process->child, 9); + return result; +#endif +} + +unsigned subprocess_read_stdout(struct subprocess_s *const process, + char *const buffer, unsigned size) { +#if defined(_WIN32) + void *handle; + unsigned long bytes_read = 0; + struct subprocess_overlapped_s overlapped = {0, 0, {{0, 0}}, SUBPROCESS_NULL}; + overlapped.hEvent = process->hEventOutput; + + handle = SUBPROCESS_PTR_CAST(void *, + _get_osfhandle(_fileno(process->stdout_file))); + + if (!ReadFile(handle, buffer, size, &bytes_read, + SUBPROCESS_PTR_CAST(LPOVERLAPPED, &overlapped))) { + const unsigned long errorIoPending = 997; + unsigned long error = GetLastError(); + + // Means we've got an async read! + if (error == errorIoPending) { + if (!GetOverlappedResult(handle, + SUBPROCESS_PTR_CAST(LPOVERLAPPED, &overlapped), + &bytes_read, 1)) { + const unsigned long errorIoIncomplete = 996; + const unsigned long errorHandleEOF = 38; + error = GetLastError(); + + if ((error != errorIoIncomplete) && (error != errorHandleEOF)) { + return 0; + } + } + } + } + + return SUBPROCESS_CAST(unsigned, bytes_read); +#else + const int fd = fileno(process->stdout_file); + const ssize_t bytes_read = read(fd, buffer, size); + + if (bytes_read < 0) { + return 0; + } + + return SUBPROCESS_CAST(unsigned, bytes_read); +#endif +} + +unsigned subprocess_read_stderr(struct subprocess_s *const process, + char *const buffer, unsigned size) { +#if defined(_WIN32) + void *handle; + unsigned long bytes_read = 0; + struct subprocess_overlapped_s overlapped = {0, 0, {{0, 0}}, SUBPROCESS_NULL}; + overlapped.hEvent = process->hEventError; + + handle = SUBPROCESS_PTR_CAST(void *, + _get_osfhandle(_fileno(process->stderr_file))); + + if (!ReadFile(handle, buffer, size, &bytes_read, + SUBPROCESS_PTR_CAST(LPOVERLAPPED, &overlapped))) { + const unsigned long errorIoPending = 997; + unsigned long error = GetLastError(); + + // Means we've got an async read! + if (error == errorIoPending) { + if (!GetOverlappedResult(handle, + SUBPROCESS_PTR_CAST(LPOVERLAPPED, &overlapped), + &bytes_read, 1)) { + const unsigned long errorIoIncomplete = 996; + const unsigned long errorHandleEOF = 38; + error = GetLastError(); + + if ((error != errorIoIncomplete) && (error != errorHandleEOF)) { + return 0; + } + } + } + } + + return SUBPROCESS_CAST(unsigned, bytes_read); +#else + const int fd = fileno(process->stderr_file); + const ssize_t bytes_read = read(fd, buffer, size); + + if (bytes_read < 0) { + return 0; + } + + return SUBPROCESS_CAST(unsigned, bytes_read); +#endif +} + +int subprocess_alive(struct subprocess_s *const process) { + int is_alive = SUBPROCESS_CAST(int, process->alive); + + if (!is_alive) { + return 0; + } +#if defined(_WIN32) + { + const unsigned long zero = 0x0; + const unsigned long wait_object_0 = 0x00000000L; + + is_alive = wait_object_0 != WaitForSingleObject(process->hProcess, zero); + } +#else + { + int status; + is_alive = 0 == waitpid(process->child, &status, WNOHANG); + + // If the process was successfully waited on we need to cleanup now. + if (!is_alive) { + if (WIFEXITED(status)) { + process->return_status = WEXITSTATUS(status); + } else { + process->return_status = EXIT_FAILURE; + } + + // Since we've already successfully waited on the process, we need to wipe + // the child now. + process->child = 0; + + if (subprocess_join(process, SUBPROCESS_NULL)) { + return -1; + } + } + } +#endif + + if (!is_alive) { + process->alive = 0; + } + + return is_alive; +} + +#if defined(__cplusplus) +} // extern "C" +#endif + +#endif /* SHEREDOM_SUBPROCESS_H_INCLUDED */ diff --git a/third_party/utest/test/test.c b/third_party/utest/test/test.c @@ -0,0 +1,268 @@ +/* + This is free and unencumbered software released into the public domain. + + Anyone is free to copy, modify, publish, use, compile, sell, or + distribute this software, either in source code form or as a compiled + binary, for any purpose, commercial or non-commercial, and by any + means. + + In jurisdictions that recognize copyright laws, the author or authors + of this software dedicate any and all copyright interest in the + software to the public domain. We make this dedication for the benefit + of the public at large and to the detriment of our heirs and + successors. We intend this dedication to be an overt act of + relinquishment in perpetuity of all present and future rights to this + software under copyright law. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR + OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + OTHER DEALINGS IN THE SOFTWARE. + + For more information, please refer to <http://unlicense.org/> +*/ + +#include "utest.h" + +#ifdef _MSC_VER +/* disable 'conditional expression is constant' - our examples below use this! + */ +#pragma warning(disable : 4127) +#endif + +UTEST(c, ASSERT_TRUE) { ASSERT_TRUE(1); } + +UTEST(c, ASSERT_FALSE) { ASSERT_FALSE(0); } + +UTEST(c, ASSERT_EQ) { ASSERT_EQ(1, 1); } + +UTEST(c, ASSERT_NE) { ASSERT_NE(1, 2); } + +UTEST(c, ASSERT_LT) { ASSERT_LT(1, 2); } + +UTEST(c, ASSERT_LE) { + ASSERT_LE(1, 1); + ASSERT_LE(1, 2); +} + +UTEST(c, ASSERT_GT) { ASSERT_GT(2, 1); } + +UTEST(c, ASSERT_GE) { + ASSERT_GE(1, 1); + ASSERT_GE(2, 1); +} + +UTEST(c, ASSERT_STREQ) { ASSERT_STREQ("foo", "foo"); } + +UTEST(c, ASSERT_STRNE) { ASSERT_STRNE("foo", "bar"); } + +UTEST(c, ASSERT_STRNEQ) { ASSERT_STRNEQ("foo", "foobar", strlen("foo")); } + +UTEST(c, ASSERT_STRNNE) { ASSERT_STRNNE("foo", "barfoo", strlen("foo")); } + +UTEST(c, EXPECT_TRUE) { EXPECT_TRUE(1); } + +UTEST(c, EXPECT_FALSE) { EXPECT_FALSE(0); } + +UTEST(c, EXPECT_EQ) { EXPECT_EQ(1, 1); } + +UTEST(c, EXPECT_NE) { EXPECT_NE(1, 2); } + +UTEST(c, EXPECT_LT) { EXPECT_LT(1, 2); } + +UTEST(c, EXPECT_LE) { + EXPECT_LE(1, 1); + EXPECT_LE(1, 2); +} + +UTEST(c, EXPECT_GT) { EXPECT_GT(2, 1); } + +UTEST(c, EXPECT_GE) { + EXPECT_GE(1, 1); + EXPECT_GE(2, 1); +} + +UTEST(c, EXPECT_STREQ) { EXPECT_STREQ("foo", "foo"); } + +UTEST(c, EXPECT_STRNE) { EXPECT_STRNE("foo", "bar"); } + +UTEST(c, EXPECT_STRNEQ) { EXPECT_STRNEQ("foo", "foobar", strlen("foo")); } + +UTEST(c, EXPECT_STRNNE) { EXPECT_STRNNE("foo", "barfoo", strlen("foo")); } + +UTEST(c, no_double_eval) { + int i = 0; + ASSERT_EQ(i++, 0); + ASSERT_EQ(i, 1); +} + +struct MyTestF { + int foo; +}; + +UTEST_F_SETUP(MyTestF) { + ASSERT_EQ(0, utest_fixture->foo); + utest_fixture->foo = 42; +} + +UTEST_F_TEARDOWN(MyTestF) { ASSERT_EQ(13, utest_fixture->foo); } + +UTEST_F(MyTestF, c) { + ASSERT_EQ(42, utest_fixture->foo); + utest_fixture->foo = 13; +} + +UTEST_F(MyTestF, c2) { + ASSERT_EQ(42, utest_fixture->foo); + utest_fixture->foo = 13; +} + +struct MyTestI { + size_t foo; + size_t bar; +}; + +UTEST_I_SETUP(MyTestI) { + ASSERT_EQ(0u, utest_fixture->foo); + ASSERT_EQ(0u, utest_fixture->bar); + utest_fixture->foo = 42; + utest_fixture->bar = utest_index; +} + +UTEST_I_TEARDOWN(MyTestI) { + ASSERT_EQ(13u, utest_fixture->foo); + ASSERT_EQ(utest_index, utest_fixture->bar); +} + +UTEST_I(MyTestI, c, 2) { + ASSERT_GT(2u, utest_fixture->bar); + utest_fixture->foo = 13; +} + +UTEST_I(MyTestI, c2, 128) { + ASSERT_GT(128u, utest_fixture->bar); + utest_fixture->foo = 13; +} + +UTEST(c, Float) { + float a = 1; + float b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(c, Double) { + double a = 1; + double b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(c, LongDouble) { + long double a = 1; + long double b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(c, Char) { + char a = 1; + char b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(c, SignedChar) { + signed char a = 1; + signed char b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(c, UChar) { + unsigned char a = 1; + unsigned char b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(c, Short) { + short a = 1; + short b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(c, UShort) { + unsigned short a = 1; + unsigned short b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(c, Int) { + int a = 1; + int b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(c, UInt) { + unsigned int a = 1; + unsigned int b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(c, Long) { + long a = 1; + long b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(c, ULong) { + unsigned long a = 1; + unsigned long b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +#if defined(__clang__) +#if __has_warning("-Wunsafe-buffer-usage") +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wunsafe-buffer-usage" +#endif +#endif + +UTEST(c, Ptr) { + char foo = 42; + EXPECT_NE(&foo, &foo + 1); +} + +UTEST(c, VoidPtr) { + void *foo = 0; + EXPECT_NE(foo, (char *)foo + 1); +} + +static const int data[4] = {42, 13, 6, -53}; + +UTEST(c, Array) { EXPECT_NE(data, data + 1); } + +#if defined(__clang__) +#if __has_warning("-Wunsafe-buffer-usage") +#pragma clang diagnostic pop +#endif +#endif + +UTEST(c, Near) { + float a = 42.0f; + float b = 42.01f; + EXPECT_NEAR(a, b, 0.01f); + ASSERT_NEAR(a, b, 0.01f); +} + +UTEST(c, Todo) { UTEST_SKIP("Not yet implemented!"); } diff --git a/third_party/utest/test/test.cpp b/third_party/utest/test/test.cpp @@ -0,0 +1,303 @@ +// This is free and unencumbered software released into the public domain. +// +// Anyone is free to copy, modify, publish, use, compile, sell, or +// distribute this software, either in source code form or as a compiled +// binary, for any purpose, commercial or non-commercial, and by any +// means. +// +// In jurisdictions that recognize copyright laws, the author or authors +// of this software dedicate any and all copyright interest in the +// software to the public domain. We make this dedication for the benefit +// of the public at large and to the detriment of our heirs and +// successors. We intend this dedication to be an overt act of +// relinquishment in perpetuity of all present and future rights to this +// software under copyright law. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR +// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +// +// For more information, please refer to <http://unlicense.org/> + +#include "utest.h" + +#ifdef _MSC_VER +// disable 'conditional expression is constant' - our examples below use this! +#pragma warning(disable : 4127) +#endif + +UTEST(cpp, ASSERT_TRUE) { ASSERT_TRUE(1); } + +UTEST(cpp, ASSERT_FALSE) { ASSERT_FALSE(0); } + +UTEST(cpp, ASSERT_EQ) { ASSERT_EQ(1, 1); } + +UTEST(cpp, ASSERT_NE) { ASSERT_NE(1, 2); } + +UTEST(cpp, ASSERT_LT) { ASSERT_LT(1, 2); } + +UTEST(cpp, ASSERT_LE) { + ASSERT_LE(1, 1); + ASSERT_LE(1, 2); +} + +UTEST(cpp, ASSERT_GT) { ASSERT_GT(2, 1); } + +UTEST(cpp, ASSERT_GE) { + ASSERT_GE(1, 1); + ASSERT_GE(2, 1); +} + +UTEST(cpp, ASSERT_STREQ) { ASSERT_STREQ("foo", "foo"); } + +UTEST(cpp, ASSERT_STRNE) { ASSERT_STRNE("foo", "bar"); } + +UTEST(cpp, ASSERT_STRNEQ) { ASSERT_STRNEQ("foo", "foobar", strlen("foo")); } + +UTEST(cpp, ASSERT_STRNNE) { ASSERT_STRNNE("foo", "barfoo", strlen("foo")); } + +UTEST(cpp, EXPECT_TRUE) { EXPECT_TRUE(1); } + +UTEST(cpp, EXPECT_FALSE) { EXPECT_FALSE(0); } + +UTEST(cpp, EXPECT_EQ) { EXPECT_EQ(1, 1); } + +UTEST(cpp, EXPECT_NE) { EXPECT_NE(1, 2); } + +UTEST(cpp, EXPECT_LT) { EXPECT_LT(1, 2); } + +UTEST(cpp, EXPECT_LE) { + EXPECT_LE(1, 1); + EXPECT_LE(1, 2); +} + +UTEST(cpp, EXPECT_GT) { EXPECT_GT(2, 1); } + +UTEST(cpp, EXPECT_GE) { + EXPECT_GE(1, 1); + EXPECT_GE(2, 1); +} + +UTEST(cpp, EXPECT_STREQ) { EXPECT_STREQ("foo", "foo"); } + +UTEST(cpp, EXPECT_STRNE) { EXPECT_STRNE("foo", "bar"); } + +UTEST(cpp, EXPECT_STRNEQ) { EXPECT_STRNEQ("foo", "foobar", strlen("foo")); } + +UTEST(cpp, EXPECT_STRNNE) { EXPECT_STRNNE("foo", "barfoo", strlen("foo")); } + +UTEST(cpp, no_double_eval) { + int i = 0; + ASSERT_EQ(i++, 0); + ASSERT_EQ(i, 1); +} + +struct MyTestF { + int foo; +}; + +UTEST_F_SETUP(MyTestF) { + ASSERT_EQ(0, utest_fixture->foo); + utest_fixture->foo = 42; +} + +UTEST_F_TEARDOWN(MyTestF) { ASSERT_EQ(13, utest_fixture->foo); } + +UTEST_F(MyTestF, cpp_1) { + ASSERT_EQ(42, utest_fixture->foo); + utest_fixture->foo = 13; +} + +UTEST_F(MyTestF, cpp_2) { + ASSERT_EQ(42, utest_fixture->foo); + utest_fixture->foo = 13; +} + +struct MyTestI { + size_t foo; + size_t bar; +}; + +UTEST_I_SETUP(MyTestI) { + ASSERT_EQ(0u, utest_fixture->foo); + ASSERT_EQ(0u, utest_fixture->bar); + utest_fixture->foo = 42; + utest_fixture->bar = utest_index; +} + +UTEST_I_TEARDOWN(MyTestI) { + ASSERT_EQ(13u, utest_fixture->foo); + ASSERT_EQ(utest_index, utest_fixture->bar); +} + +UTEST_I(MyTestI, cpp_1, 2) { + ASSERT_GT(2u, utest_fixture->bar); + utest_fixture->foo = 13; +} + +UTEST_I(MyTestI, cpp_2, 128) { + ASSERT_GT(128u, utest_fixture->bar); + utest_fixture->foo = 13; +} + +UTEST(cpp, Float) { + float a = 1; + float b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp, Double) { + double a = 1; + double b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp, LongDouble) { + long double a = 1; + long double b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp, Char) { + char a = 1; + char b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp, SignedChar) { + signed char a = 1; + signed char b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp, UChar) { + unsigned char a = 1; + unsigned char b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp, Short) { + short a = 1; + short b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp, UShort) { + unsigned short a = 1; + unsigned short b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp, Int) { + int a = 1; + int b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp, UInt) { + unsigned int a = 1; + unsigned int b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp, Long) { + long a = 1; + long b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp, ULong) { + unsigned long a = 1; + unsigned long b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +#if defined(__clang__) +#if __has_warning("-Wunsafe-buffer-usage") +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wunsafe-buffer-usage" +#endif +#endif + +UTEST(cpp, Ptr) { + char foo = 42; + EXPECT_NE(&foo, &foo + 1); +} + +static const int data[4] = {42, 13, 6, -53}; + +UTEST(cpp, Array) { EXPECT_NE(data, data + 1); } + +#if defined(__clang__) +#if __has_warning("-Wunsafe-buffer-usage") +#pragma clang diagnostic pop +#endif +#endif + +UTEST(cpp, Near) { + float a = 42.0f; + float b = 42.01f; + EXPECT_NEAR(a, b, 0.01f); + ASSERT_NEAR(a, b, 0.01f); +} + +// GCC stdlib has a sanitizer bug in exceptions! +#if defined(__has_feature) +#if __has_feature(memory_sanitizer) +#define MEMORY_SANITIZER +#endif +#endif + +#if defined(MEMORY_SANITIZER) +__attribute__((no_sanitize("memory"))) +#endif +static int +foo(int bar) { + if (bar == 1) + throw std::range_error("bad bar"); + return bar + 1; +} + +UTEST(cpp, Exception) { + EXPECT_EXCEPTION(foo(1), std::range_error); + ASSERT_EXCEPTION(foo(1), std::range_error); +} + +#if !defined(MEMORY_SANITIZER) +UTEST(cpp, ExceptionWithMessage) { + EXPECT_EXCEPTION_WITH_MESSAGE(foo(1), std::range_error, "bad bar"); + ASSERT_EXCEPTION_WITH_MESSAGE(foo(1), std::range_error, "bad bar"); +} +#endif + +UTEST(cpp, Todo) { UTEST_SKIP("Not yet implemented!"); } + +enum SomeEnum { SomeEnumFoo, SomeEnumBar }; + +UTEST(cpp, Enum) { + EXPECT_NE(SomeEnumFoo, SomeEnumBar); + ASSERT_EQ(SomeEnumFoo, SomeEnumFoo); +} + +UTEST(cpp, Modulo) { + int c = 42; + EXPECT_NE(c % 16, 0); + EXPECT_NE(0, c % 16); + ASSERT_EQ(10, c % 16); +} diff --git a/third_party/utest/test/test11.c b/third_party/utest/test/test11.c @@ -0,0 +1,260 @@ +/* + This is free and unencumbered software released into the public domain. + + Anyone is free to copy, modify, publish, use, compile, sell, or + distribute this software, either in source code form or as a compiled + binary, for any purpose, commercial or non-commercial, and by any + means. + + In jurisdictions that recognize copyright laws, the author or authors + of this software dedicate any and all copyright interest in the + software to the public domain. We make this dedication for the benefit + of the public at large and to the detriment of our heirs and + successors. We intend this dedication to be an overt act of + relinquishment in perpetuity of all present and future rights to this + software under copyright law. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR + OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + OTHER DEALINGS IN THE SOFTWARE. + + For more information, please refer to <http://unlicense.org/> +*/ + +#include "utest.h" + +#ifdef _MSC_VER +/* disable 'conditional expression is constant' - our examples below use this! + */ +#pragma warning(disable : 4127) +#endif + +UTEST(c11, ASSERT_TRUE) { ASSERT_TRUE(1); } + +UTEST(c11, ASSERT_FALSE) { ASSERT_FALSE(0); } + +UTEST(c11, ASSERT_EQ) { ASSERT_EQ(1, 1); } + +UTEST(c11, ASSERT_NE) { ASSERT_NE(1, 2); } + +UTEST(c11, ASSERT_LT) { ASSERT_LT(1, 2); } + +UTEST(c11, ASSERT_LE) { + ASSERT_LE(1, 1); + ASSERT_LE(1, 2); +} + +UTEST(c11, ASSERT_GT) { ASSERT_GT(2, 1); } + +UTEST(c11, ASSERT_GE) { + ASSERT_GE(1, 1); + ASSERT_GE(2, 1); +} + +UTEST(c11, ASSERT_STREQ) { ASSERT_STREQ("foo", "foo"); } + +UTEST(c11, ASSERT_STRNE) { ASSERT_STRNE("foo", "bar"); } + +UTEST(c11, EXPECT_TRUE) { EXPECT_TRUE(1); } + +UTEST(c11, EXPECT_FALSE) { EXPECT_FALSE(0); } + +UTEST(c11, EXPECT_EQ) { EXPECT_EQ(1, 1); } + +UTEST(c11, EXPECT_NE) { EXPECT_NE(1, 2); } + +UTEST(c11, EXPECT_LT) { EXPECT_LT(1, 2); } + +UTEST(c11, EXPECT_LE) { + EXPECT_LE(1, 1); + EXPECT_LE(1, 2); +} + +UTEST(c11, EXPECT_GT) { EXPECT_GT(2, 1); } + +UTEST(c11, EXPECT_GE) { + EXPECT_GE(1, 1); + EXPECT_GE(2, 1); +} + +UTEST(c11, EXPECT_STREQ) { EXPECT_STREQ("foo", "foo"); } + +UTEST(c11, EXPECT_STRNE) { EXPECT_STRNE("foo", "bar"); } + +UTEST(c11, no_double_eval) { + int i = 0; + ASSERT_EQ(i++, 0); + ASSERT_EQ(i, 1); +} + +struct MyTestF { + int foo; +}; + +UTEST_F_SETUP(MyTestF) { + ASSERT_EQ(0, utest_fixture->foo); + utest_fixture->foo = 42; +} + +UTEST_F_TEARDOWN(MyTestF) { ASSERT_EQ(13, utest_fixture->foo); } + +UTEST_F(MyTestF, c11) { + ASSERT_EQ(42, utest_fixture->foo); + utest_fixture->foo = 13; +} + +UTEST_F(MyTestF, c11_2) { + ASSERT_EQ(42, utest_fixture->foo); + utest_fixture->foo = 13; +} + +struct MyTestI { + size_t foo; + size_t bar; +}; + +UTEST_I_SETUP(MyTestI) { + ASSERT_EQ(0u, utest_fixture->foo); + ASSERT_EQ(0u, utest_fixture->bar); + utest_fixture->foo = 42; + utest_fixture->bar = utest_index; +} + +UTEST_I_TEARDOWN(MyTestI) { + ASSERT_EQ(13u, utest_fixture->foo); + ASSERT_EQ(utest_index, utest_fixture->bar); +} + +UTEST_I(MyTestI, c11, 2) { + ASSERT_GT(2u, utest_fixture->bar); + utest_fixture->foo = 13; +} + +UTEST_I(MyTestI, c11_2, 128) { + ASSERT_GT(128u, utest_fixture->bar); + utest_fixture->foo = 13; +} + +UTEST(c11, Float) { + float a = 1; + float b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(c11, Double) { + double a = 1; + double b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(c11, LongDouble) { + long double a = 1; + long double b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(c11, Char) { + char a = 1; + char b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(c11, SignedChar) { + signed char a = 1; + signed char b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(c11, UChar) { + unsigned char a = 1; + unsigned char b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(c11, Short) { + short a = 1; + short b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(c11, UShort) { + unsigned short a = 1; + unsigned short b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(c11, Int) { + int a = 1; + int b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(c11, UInt) { + unsigned int a = 1; + unsigned int b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(c11, Long) { + long a = 1; + long b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(c11, ULong) { + unsigned long a = 1; + unsigned long b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +#if defined(__clang__) +#if __has_warning("-Wunsafe-buffer-usage") +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wunsafe-buffer-usage" +#endif +#endif + +UTEST(c11, Ptr) { + char foo = 42; + EXPECT_NE(&foo, &foo + 1); +} + +UTEST(c11, VoidPtr) { + void *foo = 0; + EXPECT_NE(foo, (char *)foo + 1); +} + +static const int data[4] = {42, 13, 6, -53}; + +UTEST(c11, Array) { EXPECT_NE(data, data + 1); } + +#if defined(__clang__) +#if __has_warning("-Wunsafe-buffer-usage") +#pragma clang diagnostic pop +#endif +#endif + +UTEST(c11, Near) { + float a = 42.0f; + float b = 42.01f; + EXPECT_NEAR(a, b, 0.01f); + ASSERT_NEAR(a, b, 0.01f); +} + +UTEST(c11, Todo) { UTEST_SKIP("Not yet implemented!"); } diff --git a/third_party/utest/test/test11.cpp b/third_party/utest/test/test11.cpp @@ -0,0 +1,352 @@ +// This is free and unencumbered software released into the public domain. +// +// Anyone is free to copy, modify, publish, use, compile, sell, or +// distribute this software, either in source code form or as a compiled +// binary, for any purpose, commercial or non-commercial, and by any +// means. +// +// In jurisdictions that recognize copyright laws, the author or authors +// of this software dedicate any and all copyright interest in the +// software to the public domain. We make this dedication for the benefit +// of the public at large and to the detriment of our heirs and +// successors. We intend this dedication to be an overt act of +// relinquishment in perpetuity of all present and future rights to this +// software under copyright law. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR +// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +// +// For more information, please refer to <http://unlicense.org/> + +#include "utest.h" + +#ifdef _MSC_VER +// disable 'conditional expression is constant' - our examples below use this! +#pragma warning(disable : 4127) +#endif + +UTEST(cpp11, ASSERT_TRUE) { ASSERT_TRUE(1); } + +UTEST(cpp11, ASSERT_FALSE) { ASSERT_FALSE(0); } + +UTEST(cpp11, ASSERT_EQ) { ASSERT_EQ(1, 1); } + +UTEST(cpp11, ASSERT_NE) { ASSERT_NE(1, 2); } + +UTEST(cpp11, ASSERT_LT) { ASSERT_LT(1, 2); } + +UTEST(cpp11, ASSERT_LE) { + ASSERT_LE(1, 1); + ASSERT_LE(1, 2); +} + +UTEST(cpp11, ASSERT_GT) { ASSERT_GT(2, 1); } + +UTEST(cpp11, ASSERT_GE) { + ASSERT_GE(1, 1); + ASSERT_GE(2, 1); +} + +UTEST(cpp11, ASSERT_STREQ) { ASSERT_STREQ("foo", "foo"); } + +UTEST(cpp11, ASSERT_STRNE) { ASSERT_STRNE("foo", "bar"); } + +UTEST(cpp11, ASSERT_STRNEQ) { ASSERT_STRNEQ("foo", "foobar", strlen("foo")); } + +UTEST(cpp11, ASSERT_STRNNE) { ASSERT_STRNNE("foo", "barfoo", strlen("foo")); } + +UTEST(cpp11, EXPECT_TRUE) { EXPECT_TRUE(1); } + +UTEST(cpp11, EXPECT_FALSE) { EXPECT_FALSE(0); } + +UTEST(cpp11, EXPECT_EQ) { EXPECT_EQ(1, 1); } + +UTEST(cpp11, EXPECT_NE) { EXPECT_NE(1, 2); } + +UTEST(cpp11, EXPECT_LT) { EXPECT_LT(1, 2); } + +UTEST(cpp11, EXPECT_LE) { + EXPECT_LE(1, 1); + EXPECT_LE(1, 2); +} + +UTEST(cpp11, EXPECT_GT) { EXPECT_GT(2, 1); } + +UTEST(cpp11, EXPECT_GE) { + EXPECT_GE(1, 1); + EXPECT_GE(2, 1); +} + +UTEST(cpp11, EXPECT_STREQ) { EXPECT_STREQ("foo", "foo"); } + +UTEST(cpp11, EXPECT_STRNE) { EXPECT_STRNE("foo", "bar"); } + +UTEST(cpp11, EXPECT_STRNEQ) { EXPECT_STRNEQ("foo", "foobar", strlen("foo")); } + +UTEST(cpp11, EXPECT_STRNNE) { EXPECT_STRNNE("foo", "barfoo", strlen("foo")); } + +UTEST(cpp11, no_double_eval) { + int i = 0; + ASSERT_EQ(i++, 0); + ASSERT_EQ(i, 1); +} + +UTEST(cpp11, bool_eval) { + bool i = false; + ASSERT_EQ(i, false); + i = true; + ASSERT_EQ(i, true); +} + +struct MyTestF { + int foo; +}; + +UTEST_F_SETUP(MyTestF) { + ASSERT_EQ(0, utest_fixture->foo); + utest_fixture->foo = 42; +} + +UTEST_F_TEARDOWN(MyTestF) { ASSERT_EQ(13, utest_fixture->foo); } + +UTEST_F(MyTestF, cpp11_1) { + ASSERT_EQ(42, utest_fixture->foo); + utest_fixture->foo = 13; +} + +UTEST_F(MyTestF, cpp11_2) { + ASSERT_EQ(42, utest_fixture->foo); + utest_fixture->foo = 13; +} + +struct MyTestI { + size_t foo; + size_t bar; +}; + +UTEST_I_SETUP(MyTestI) { + ASSERT_EQ(0u, utest_fixture->foo); + ASSERT_EQ(0u, utest_fixture->bar); + utest_fixture->foo = 42; + utest_fixture->bar = utest_index; +} + +UTEST_I_TEARDOWN(MyTestI) { + ASSERT_EQ(13u, utest_fixture->foo); + ASSERT_EQ(utest_index, utest_fixture->bar); +} + +UTEST_I(MyTestI, cpp11_1, 2) { + ASSERT_GT(2u, utest_fixture->bar); + utest_fixture->foo = 13; +} + +UTEST_I(MyTestI, cpp11_2, 128) { + ASSERT_GT(128u, utest_fixture->bar); + utest_fixture->foo = 13; +} + +UTEST(cpp11, Float) { + float a = 1; + float b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp11, Double) { + double a = 1; + double b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp11, LongDouble) { + long double a = 1; + long double b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp11, Char) { + char a = 1; + char b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp11, SignedChar) { + signed char a = 1; + signed char b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp11, UChar) { + unsigned char a = 1; + unsigned char b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp11, Short) { + short a = 1; + short b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp11, UShort) { + unsigned short a = 1; + unsigned short b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp11, Int) { + int a = 1; + int b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp11, UInt) { + unsigned int a = 1; + unsigned int b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp11, Long) { + long a = 1; + long b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp11, ULong) { + unsigned long a = 1; + unsigned long b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +#ifdef __clang__ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wc++98-compat-pedantic" +#endif + +UTEST(cpp11, LongLong) { + long long a = 1; + long long b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp11, ULongLong) { + unsigned long long a = 1; + unsigned long long b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +#ifdef __clang__ +#pragma clang diagnostic pop +#endif + +#if defined(__clang__) +#if __has_warning("-Wunsafe-buffer-usage") +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wunsafe-buffer-usage" +#endif +#endif + +UTEST(cpp11, Ptr) { + char foo = 42; + EXPECT_NE(&foo, &foo + 1); +} + +UTEST(cpp11, VoidPtr) { + void *foo = reinterpret_cast<void *>(0); + EXPECT_NE(foo, static_cast<char *>(foo) + 1); +} + +static const int data[4] = {42, 13, 6, -53}; + +UTEST(cpp11, Array) { EXPECT_NE(data, data + 1); } + +#if defined(__clang__) +#if __has_warning("-Wunsafe-buffer-usage") +#pragma clang diagnostic pop +#endif +#endif + +UTEST(cpp11, Near) { + float a = 42.0f; + float b = 42.01f; + EXPECT_NEAR(a, b, 0.01f); + ASSERT_NEAR(a, b, 0.01f); +} + +// GCC stdlib has a sanitizer bug in exceptions! +#if defined(__has_feature) +#if __has_feature(memory_sanitizer) +#define MEMORY_SANITIZER +#endif +#endif + +static int foo(int bar) { + if (bar == 1) + throw std::range_error("bad bar"); + return bar + 1; +} + +UTEST(cpp11, Exception) { + EXPECT_EXCEPTION(foo(1), std::range_error); + ASSERT_EXCEPTION(foo(1), std::range_error); +} + +#if !defined(MEMORY_SANITIZER) +UTEST(cpp11, ExceptionWithMessage) { + EXPECT_EXCEPTION_WITH_MESSAGE(foo(1), std::range_error, "bad bar"); + ASSERT_EXCEPTION_WITH_MESSAGE(foo(1), std::range_error, "bad bar"); +} +#endif + +UTEST(cpp11, Todo) { UTEST_SKIP("Not yet implemented!"); } + +enum SomeEnum { SomeEnumFoo, SomeEnumBar }; + +UTEST(cpp11, Enum) { + EXPECT_NE(SomeEnumFoo, SomeEnumBar); + ASSERT_EQ(SomeEnumFoo, SomeEnumFoo); +} + +#if !defined(_MSC_VER) || (_MSC_VER >= 1900) + +#ifdef __clang__ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wc++98-compat-pedantic" +#endif + +enum class SomeEnumClass { Foo, Bar }; + +UTEST(cpp11, EnumClass) { + EXPECT_NE(SomeEnumClass::Foo, SomeEnumClass::Bar); + ASSERT_EQ(SomeEnumClass::Foo, SomeEnumClass::Foo); +} + +#ifdef __clang__ +#pragma clang diagnostic pop +#endif + +UTEST(cpp11, Null) { + EXPECT_EQ(nullptr, nullptr); + ASSERT_EQ(nullptr, nullptr); +} + +#endif diff --git a/third_party/utest/test/test14.cpp b/third_party/utest/test/test14.cpp @@ -0,0 +1,352 @@ +// This is free and unencumbered software released into the public domain. +// +// Anyone is free to copy, modify, publish, use, compile, sell, or +// distribute this software, either in source code form or as a compiled +// binary, for any purpose, commercial or non-commercial, and by any +// means. +// +// In jurisdictions that recognize copyright laws, the author or authors +// of this software dedicate any and all copyright interest in the +// software to the public domain. We make this dedication for the benefit +// of the public at large and to the detriment of our heirs and +// successors. We intend this dedication to be an overt act of +// relinquishment in perpetuity of all present and future rights to this +// software under copyright law. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR +// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +// +// For more information, please refer to <http://unlicense.org/> + +#include "utest.h" + +#ifdef _MSC_VER +// disable 'conditional expression is constant' - our examples below use this! +#pragma warning(disable : 4127) +#endif + +UTEST(cpp14, ASSERT_TRUE) { ASSERT_TRUE(1); } + +UTEST(cpp14, ASSERT_FALSE) { ASSERT_FALSE(0); } + +UTEST(cpp14, ASSERT_EQ) { ASSERT_EQ(1, 1); } + +UTEST(cpp14, ASSERT_NE) { ASSERT_NE(1, 2); } + +UTEST(cpp14, ASSERT_LT) { ASSERT_LT(1, 2); } + +UTEST(cpp14, ASSERT_LE) { + ASSERT_LE(1, 1); + ASSERT_LE(1, 2); +} + +UTEST(cpp14, ASSERT_GT) { ASSERT_GT(2, 1); } + +UTEST(cpp14, ASSERT_GE) { + ASSERT_GE(1, 1); + ASSERT_GE(2, 1); +} + +UTEST(cpp14, ASSERT_STREQ) { ASSERT_STREQ("foo", "foo"); } + +UTEST(cpp14, ASSERT_STRNE) { ASSERT_STRNE("foo", "bar"); } + +UTEST(cpp14, ASSERT_STRNEQ) { ASSERT_STRNEQ("foo", "foobar", strlen("foo")); } + +UTEST(cpp14, ASSERT_STRNNE) { ASSERT_STRNNE("foo", "barfoo", strlen("foo")); } + +UTEST(cpp14, EXPECT_TRUE) { EXPECT_TRUE(1); } + +UTEST(cpp14, EXPECT_FALSE) { EXPECT_FALSE(0); } + +UTEST(cpp14, EXPECT_EQ) { EXPECT_EQ(1, 1); } + +UTEST(cpp14, EXPECT_NE) { EXPECT_NE(1, 2); } + +UTEST(cpp14, EXPECT_LT) { EXPECT_LT(1, 2); } + +UTEST(cpp14, EXPECT_LE) { + EXPECT_LE(1, 1); + EXPECT_LE(1, 2); +} + +UTEST(cpp14, EXPECT_GT) { EXPECT_GT(2, 1); } + +UTEST(cpp14, EXPECT_GE) { + EXPECT_GE(1, 1); + EXPECT_GE(2, 1); +} + +UTEST(cpp14, EXPECT_STREQ) { EXPECT_STREQ("foo", "foo"); } + +UTEST(cpp14, EXPECT_STRNE) { EXPECT_STRNE("foo", "bar"); } + +UTEST(cpp14, EXPECT_STRNEQ) { EXPECT_STRNEQ("foo", "foobar", strlen("foo")); } + +UTEST(cpp14, EXPECT_STRNNE) { EXPECT_STRNNE("foo", "barfoo", strlen("foo")); } + +UTEST(cpp14, no_double_eval) { + int i = 0; + ASSERT_EQ(i++, 0); + ASSERT_EQ(i, 1); +} + +UTEST(cpp14, bool_eval) { + bool i = false; + ASSERT_EQ(i, false); + i = true; + ASSERT_EQ(i, true); +} + +struct MyTestF { + int foo; +}; + +UTEST_F_SETUP(MyTestF) { + ASSERT_EQ(0, utest_fixture->foo); + utest_fixture->foo = 42; +} + +UTEST_F_TEARDOWN(MyTestF) { ASSERT_EQ(13, utest_fixture->foo); } + +UTEST_F(MyTestF, cpp14_1) { + ASSERT_EQ(42, utest_fixture->foo); + utest_fixture->foo = 13; +} + +UTEST_F(MyTestF, cpp14_2) { + ASSERT_EQ(42, utest_fixture->foo); + utest_fixture->foo = 13; +} + +struct MyTestI { + size_t foo; + size_t bar; +}; + +UTEST_I_SETUP(MyTestI) { + ASSERT_EQ(0u, utest_fixture->foo); + ASSERT_EQ(0u, utest_fixture->bar); + utest_fixture->foo = 42; + utest_fixture->bar = utest_index; +} + +UTEST_I_TEARDOWN(MyTestI) { + ASSERT_EQ(13u, utest_fixture->foo); + ASSERT_EQ(utest_index, utest_fixture->bar); +} + +UTEST_I(MyTestI, cpp14_1, 2) { + ASSERT_GT(2u, utest_fixture->bar); + utest_fixture->foo = 13; +} + +UTEST_I(MyTestI, cpp14_2, 128) { + ASSERT_GT(128u, utest_fixture->bar); + utest_fixture->foo = 13; +} + +UTEST(cpp14, Float) { + float a = 1; + float b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp14, Double) { + double a = 1; + double b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp14, LongDouble) { + long double a = 1; + long double b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp14, Char) { + char a = 1; + char b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp14, SignedChar) { + signed char a = 1; + signed char b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp14, UChar) { + unsigned char a = 1; + unsigned char b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp14, Short) { + short a = 1; + short b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp14, UShort) { + unsigned short a = 1; + unsigned short b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp14, Int) { + int a = 1; + int b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp14, UInt) { + unsigned int a = 1; + unsigned int b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp14, Long) { + long a = 1; + long b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp14, ULong) { + unsigned long a = 1; + unsigned long b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +#ifdef __clang__ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wc++98-compat-pedantic" +#endif + +UTEST(cpp14, LongLong) { + long long a = 1; + long long b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp14, ULongLong) { + unsigned long long a = 1; + unsigned long long b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +#ifdef __clang__ +#pragma clang diagnostic pop +#endif + +#if defined(__clang__) +#if __has_warning("-Wunsafe-buffer-usage") +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wunsafe-buffer-usage" +#endif +#endif + +UTEST(cpp14, Ptr) { + char foo = 42; + EXPECT_NE(&foo, &foo + 1); +} + +UTEST(cpp14, VoidPtr) { + void *foo = reinterpret_cast<void *>(0); + EXPECT_NE(foo, static_cast<char *>(foo) + 1); +} + +static const int data[4] = {42, 13, 6, -53}; + +UTEST(cpp14, Array) { EXPECT_NE(data, data + 1); } + +#if defined(__clang__) +#if __has_warning("-Wunsafe-buffer-usage") +#pragma clang diagnostic pop +#endif +#endif + +UTEST(cpp14, Near) { + float a = 42.0f; + float b = 42.01f; + EXPECT_NEAR(a, b, 0.01f); + ASSERT_NEAR(a, b, 0.01f); +} + +// GCC stdlib has a sanitizer bug in exceptions! +#if defined(__has_feature) +#if __has_feature(memory_sanitizer) +#define MEMORY_SANITIZER +#endif +#endif + +static int foo(int bar) { + if (bar == 1) + throw std::range_error("bad bar"); + return bar + 1; +} + +UTEST(cpp14, Exception) { + EXPECT_EXCEPTION(foo(1), std::range_error); + ASSERT_EXCEPTION(foo(1), std::range_error); +} + +#if !defined(MEMORY_SANITIZER) +UTEST(cpp14, ExceptionWithMessage) { + EXPECT_EXCEPTION_WITH_MESSAGE(foo(1), std::range_error, "bad bar"); + ASSERT_EXCEPTION_WITH_MESSAGE(foo(1), std::range_error, "bad bar"); +} +#endif + +UTEST(cpp14, Todo) { UTEST_SKIP("Not yet implemented!"); } + +enum SomeEnum { SomeEnumFoo, SomeEnumBar }; + +UTEST(cpp14, Enum) { + EXPECT_NE(SomeEnumFoo, SomeEnumBar); + ASSERT_EQ(SomeEnumFoo, SomeEnumFoo); +} + +#if !defined(_MSC_VER) || (_MSC_VER >= 1900) + +#ifdef __clang__ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wc++98-compat-pedantic" +#endif + +enum class SomeEnumClass { Foo, Bar }; + +UTEST(cpp14, EnumClass) { + EXPECT_NE(SomeEnumClass::Foo, SomeEnumClass::Bar); + ASSERT_EQ(SomeEnumClass::Foo, SomeEnumClass::Foo); +} + +#ifdef __clang__ +#pragma clang diagnostic pop +#endif + +UTEST(cpp14, Null) { + EXPECT_EQ(nullptr, nullptr); + ASSERT_EQ(nullptr, nullptr); +} + +#endif diff --git a/third_party/utest/test/test17.cpp b/third_party/utest/test/test17.cpp @@ -0,0 +1,352 @@ +// This is free and unencumbered software released into the public domain. +// +// Anyone is free to copy, modify, publish, use, compile, sell, or +// distribute this software, either in source code form or as a compiled +// binary, for any purpose, commercial or non-commercial, and by any +// means. +// +// In jurisdictions that recognize copyright laws, the author or authors +// of this software dedicate any and all copyright interest in the +// software to the public domain. We make this dedication for the benefit +// of the public at large and to the detriment of our heirs and +// successors. We intend this dedication to be an overt act of +// relinquishment in perpetuity of all present and future rights to this +// software under copyright law. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR +// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +// +// For more information, please refer to <http://unlicense.org/> + +#include "utest.h" + +#ifdef _MSC_VER +// disable 'conditional expression is constant' - our examples below use this! +#pragma warning(disable : 4127) +#endif + +UTEST(cpp17, ASSERT_TRUE) { ASSERT_TRUE(1); } + +UTEST(cpp17, ASSERT_FALSE) { ASSERT_FALSE(0); } + +UTEST(cpp17, ASSERT_EQ) { ASSERT_EQ(1, 1); } + +UTEST(cpp17, ASSERT_NE) { ASSERT_NE(1, 2); } + +UTEST(cpp17, ASSERT_LT) { ASSERT_LT(1, 2); } + +UTEST(cpp17, ASSERT_LE) { + ASSERT_LE(1, 1); + ASSERT_LE(1, 2); +} + +UTEST(cpp17, ASSERT_GT) { ASSERT_GT(2, 1); } + +UTEST(cpp17, ASSERT_GE) { + ASSERT_GE(1, 1); + ASSERT_GE(2, 1); +} + +UTEST(cpp17, ASSERT_STREQ) { ASSERT_STREQ("foo", "foo"); } + +UTEST(cpp17, ASSERT_STRNE) { ASSERT_STRNE("foo", "bar"); } + +UTEST(cpp17, ASSERT_STRNEQ) { ASSERT_STRNEQ("foo", "foobar", strlen("foo")); } + +UTEST(cpp17, ASSERT_STRNNE) { ASSERT_STRNNE("foo", "barfoo", strlen("foo")); } + +UTEST(cpp17, EXPECT_TRUE) { EXPECT_TRUE(1); } + +UTEST(cpp17, EXPECT_FALSE) { EXPECT_FALSE(0); } + +UTEST(cpp17, EXPECT_EQ) { EXPECT_EQ(1, 1); } + +UTEST(cpp17, EXPECT_NE) { EXPECT_NE(1, 2); } + +UTEST(cpp17, EXPECT_LT) { EXPECT_LT(1, 2); } + +UTEST(cpp17, EXPECT_LE) { + EXPECT_LE(1, 1); + EXPECT_LE(1, 2); +} + +UTEST(cpp17, EXPECT_GT) { EXPECT_GT(2, 1); } + +UTEST(cpp17, EXPECT_GE) { + EXPECT_GE(1, 1); + EXPECT_GE(2, 1); +} + +UTEST(cpp17, EXPECT_STREQ) { EXPECT_STREQ("foo", "foo"); } + +UTEST(cpp17, EXPECT_STRNE) { EXPECT_STRNE("foo", "bar"); } + +UTEST(cpp17, EXPECT_STRNEQ) { EXPECT_STRNEQ("foo", "foobar", strlen("foo")); } + +UTEST(cpp17, EXPECT_STRNNE) { EXPECT_STRNNE("foo", "barfoo", strlen("foo")); } + +UTEST(cpp17, no_double_eval) { + int i = 0; + ASSERT_EQ(i++, 0); + ASSERT_EQ(i, 1); +} + +UTEST(cpp17, bool_eval) { + bool i = false; + ASSERT_EQ(i, false); + i = true; + ASSERT_EQ(i, true); +} + +struct MyTestF { + int foo; +}; + +UTEST_F_SETUP(MyTestF) { + ASSERT_EQ(0, utest_fixture->foo); + utest_fixture->foo = 42; +} + +UTEST_F_TEARDOWN(MyTestF) { ASSERT_EQ(13, utest_fixture->foo); } + +UTEST_F(MyTestF, cpp17_1) { + ASSERT_EQ(42, utest_fixture->foo); + utest_fixture->foo = 13; +} + +UTEST_F(MyTestF, cpp17_2) { + ASSERT_EQ(42, utest_fixture->foo); + utest_fixture->foo = 13; +} + +struct MyTestI { + size_t foo; + size_t bar; +}; + +UTEST_I_SETUP(MyTestI) { + ASSERT_EQ(0u, utest_fixture->foo); + ASSERT_EQ(0u, utest_fixture->bar); + utest_fixture->foo = 42; + utest_fixture->bar = utest_index; +} + +UTEST_I_TEARDOWN(MyTestI) { + ASSERT_EQ(13u, utest_fixture->foo); + ASSERT_EQ(utest_index, utest_fixture->bar); +} + +UTEST_I(MyTestI, cpp17_1, 2) { + ASSERT_GT(2u, utest_fixture->bar); + utest_fixture->foo = 13; +} + +UTEST_I(MyTestI, cpp17_2, 128) { + ASSERT_GT(128u, utest_fixture->bar); + utest_fixture->foo = 13; +} + +UTEST(cpp17, Float) { + float a = 1; + float b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp17, Double) { + double a = 1; + double b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp17, LongDouble) { + long double a = 1; + long double b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp17, Char) { + char a = 1; + char b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp17, SignedChar) { + signed char a = 1; + signed char b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp17, UChar) { + unsigned char a = 1; + unsigned char b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp17, Short) { + short a = 1; + short b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp17, UShort) { + unsigned short a = 1; + unsigned short b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp17, Int) { + int a = 1; + int b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp17, UInt) { + unsigned int a = 1; + unsigned int b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp17, Long) { + long a = 1; + long b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp17, ULong) { + unsigned long a = 1; + unsigned long b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +#ifdef __clang__ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wc++98-compat-pedantic" +#endif + +UTEST(cpp17, LongLong) { + long long a = 1; + long long b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp17, ULongLong) { + unsigned long long a = 1; + unsigned long long b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +#ifdef __clang__ +#pragma clang diagnostic pop +#endif + +#if defined(__clang__) +#if __has_warning("-Wunsafe-buffer-usage") +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wunsafe-buffer-usage" +#endif +#endif + +UTEST(cpp17, Ptr) { + char foo = 42; + EXPECT_NE(&foo, &foo + 1); +} + +UTEST(cpp17, VoidPtr) { + void *foo = reinterpret_cast<void *>(0); + EXPECT_NE(foo, static_cast<char *>(foo) + 1); +} + +static const int data[4] = {42, 13, 6, -53}; + +UTEST(cpp17, Array) { EXPECT_NE(data, data + 1); } + +#if defined(__clang__) +#if __has_warning("-Wunsafe-buffer-usage") +#pragma clang diagnostic pop +#endif +#endif + +UTEST(cpp17, Near) { + float a = 42.0f; + float b = 42.01f; + EXPECT_NEAR(a, b, 0.01f); + ASSERT_NEAR(a, b, 0.01f); +} + +// GCC stdlib has a sanitizer bug in exceptions! +#if defined(__has_feature) +#if __has_feature(memory_sanitizer) +#define MEMORY_SANITIZER +#endif +#endif + +static int foo(int bar) { + if (bar == 1) + throw std::range_error("bad bar"); + return bar + 1; +} + +UTEST(cpp17, Exception) { + EXPECT_EXCEPTION(foo(1), std::range_error); + ASSERT_EXCEPTION(foo(1), std::range_error); +} + +#if !defined(MEMORY_SANITIZER) +UTEST(cpp17, ExceptionWithMessage) { + EXPECT_EXCEPTION_WITH_MESSAGE(foo(1), std::range_error, "bad bar"); + ASSERT_EXCEPTION_WITH_MESSAGE(foo(1), std::range_error, "bad bar"); +} +#endif + +UTEST(cpp17, Todo) { UTEST_SKIP("Not yet implemented!"); } + +enum SomeEnum { SomeEnumFoo, SomeEnumBar }; + +UTEST(cpp17, Enum) { + EXPECT_NE(SomeEnumFoo, SomeEnumBar); + ASSERT_EQ(SomeEnumFoo, SomeEnumFoo); +} + +#if !defined(_MSC_VER) || (_MSC_VER >= 1900) + +#ifdef __clang__ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wc++98-compat-pedantic" +#endif + +enum class SomeEnumClass { Foo, Bar }; + +UTEST(cpp17, EnumClass) { + EXPECT_NE(SomeEnumClass::Foo, SomeEnumClass::Bar); + ASSERT_EQ(SomeEnumClass::Foo, SomeEnumClass::Foo); +} + +#ifdef __clang__ +#pragma clang diagnostic pop +#endif + +UTEST(cpp17, Null) { + EXPECT_EQ(nullptr, nullptr); + ASSERT_EQ(nullptr, nullptr); +} + +#endif diff --git a/third_party/utest/test/test99.c b/third_party/utest/test/test99.c @@ -0,0 +1,260 @@ +/* + This is free and unencumbered software released into the public domain. + + Anyone is free to copy, modify, publish, use, compile, sell, or + distribute this software, either in source code form or as a compiled + binary, for any purpose, commercial or non-commercial, and by any + means. + + In jurisdictions that recognize copyright laws, the author or authors + of this software dedicate any and all copyright interest in the + software to the public domain. We make this dedication for the benefit + of the public at large and to the detriment of our heirs and + successors. We intend this dedication to be an overt act of + relinquishment in perpetuity of all present and future rights to this + software under copyright law. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR + OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + OTHER DEALINGS IN THE SOFTWARE. + + For more information, please refer to <http://unlicense.org/> +*/ + +#include "utest.h" + +#ifdef _MSC_VER +/* disable 'conditional expression is constant' - our examples below use this! + */ +#pragma warning(disable : 4127) +#endif + +UTEST(c99, ASSERT_TRUE) { ASSERT_TRUE(1); } + +UTEST(c99, ASSERT_FALSE) { ASSERT_FALSE(0); } + +UTEST(c99, ASSERT_EQ) { ASSERT_EQ(1, 1); } + +UTEST(c99, ASSERT_NE) { ASSERT_NE(1, 2); } + +UTEST(c99, ASSERT_LT) { ASSERT_LT(1, 2); } + +UTEST(c99, ASSERT_LE) { + ASSERT_LE(1, 1); + ASSERT_LE(1, 2); +} + +UTEST(c99, ASSERT_GT) { ASSERT_GT(2, 1); } + +UTEST(c99, ASSERT_GE) { + ASSERT_GE(1, 1); + ASSERT_GE(2, 1); +} + +UTEST(c99, ASSERT_STREQ) { ASSERT_STREQ("foo", "foo"); } + +UTEST(c99, ASSERT_STRNE) { ASSERT_STRNE("foo", "bar"); } + +UTEST(c99, EXPECT_TRUE) { EXPECT_TRUE(1); } + +UTEST(c99, EXPECT_FALSE) { EXPECT_FALSE(0); } + +UTEST(c99, EXPECT_EQ) { EXPECT_EQ(1, 1); } + +UTEST(c99, EXPECT_NE) { EXPECT_NE(1, 2); } + +UTEST(c99, EXPECT_LT) { EXPECT_LT(1, 2); } + +UTEST(c99, EXPECT_LE) { + EXPECT_LE(1, 1); + EXPECT_LE(1, 2); +} + +UTEST(c99, EXPECT_GT) { EXPECT_GT(2, 1); } + +UTEST(c99, EXPECT_GE) { + EXPECT_GE(1, 1); + EXPECT_GE(2, 1); +} + +UTEST(c99, EXPECT_STREQ) { EXPECT_STREQ("foo", "foo"); } + +UTEST(c99, EXPECT_STRNE) { EXPECT_STRNE("foo", "bar"); } + +UTEST(c99, no_double_eval) { + int i = 0; + ASSERT_EQ(i++, 0); + ASSERT_EQ(i, 1); +} + +struct MyTestF { + int foo; +}; + +UTEST_F_SETUP(MyTestF) { + ASSERT_EQ(0, utest_fixture->foo); + utest_fixture->foo = 42; +} + +UTEST_F_TEARDOWN(MyTestF) { ASSERT_EQ(13, utest_fixture->foo); } + +UTEST_F(MyTestF, c99) { + ASSERT_EQ(42, utest_fixture->foo); + utest_fixture->foo = 13; +} + +UTEST_F(MyTestF, c99_2) { + ASSERT_EQ(42, utest_fixture->foo); + utest_fixture->foo = 13; +} + +struct MyTestI { + size_t foo; + size_t bar; +}; + +UTEST_I_SETUP(MyTestI) { + ASSERT_EQ(0u, utest_fixture->foo); + ASSERT_EQ(0u, utest_fixture->bar); + utest_fixture->foo = 42; + utest_fixture->bar = utest_index; +} + +UTEST_I_TEARDOWN(MyTestI) { + ASSERT_EQ(13u, utest_fixture->foo); + ASSERT_EQ(utest_index, utest_fixture->bar); +} + +UTEST_I(MyTestI, c99, 2) { + ASSERT_GT(2u, utest_fixture->bar); + utest_fixture->foo = 13; +} + +UTEST_I(MyTestI, c99_2, 128) { + ASSERT_GT(128u, utest_fixture->bar); + utest_fixture->foo = 13; +} + +UTEST(c99, Float) { + float a = 1; + float b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(c99, Double) { + double a = 1; + double b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(c99, LongDouble) { + long double a = 1; + long double b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(c99, Char) { + char a = 1; + char b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(c99, SignedChar) { + signed char a = 1; + signed char b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(c99, UChar) { + unsigned char a = 1; + unsigned char b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(c99, Short) { + short a = 1; + short b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(c99, UShort) { + unsigned short a = 1; + unsigned short b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(c99, Int) { + int a = 1; + int b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(c99, UInt) { + unsigned int a = 1; + unsigned int b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(c99, Long) { + long a = 1; + long b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(c99, ULong) { + unsigned long a = 1; + unsigned long b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +#if defined(__clang__) +#if __has_warning("-Wunsafe-buffer-usage") +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wunsafe-buffer-usage" +#endif +#endif + +UTEST(c99, Ptr) { + char foo = 42; + EXPECT_NE(&foo, &foo + 1); +} + +UTEST(c99, VoidPtr) { + void *foo = 0; + EXPECT_NE(foo, (char *)foo + 1); +} + +static const int data[4] = {42, 13, 6, -53}; + +UTEST(c99, Array) { EXPECT_NE(data, data + 1); } + +#if defined(__clang__) +#if __has_warning("-Wunsafe-buffer-usage") +#pragma clang diagnostic pop +#endif +#endif + +UTEST(c99, Near) { + float a = 42.0f; + float b = 42.01f; + EXPECT_NEAR(a, b, 0.01f); + ASSERT_NEAR(a, b, 0.01f); +} + +UTEST(c99, Todo) { UTEST_SKIP("Not yet implemented!"); } diff --git a/third_party/utest/test/test_no_exceptions.cpp b/third_party/utest/test/test_no_exceptions.cpp @@ -0,0 +1,261 @@ +// This is free and unencumbered software released into the public domain. +// +// Anyone is free to copy, modify, publish, use, compile, sell, or +// distribute this software, either in source code form or as a compiled +// binary, for any purpose, commercial or non-commercial, and by any +// means. +// +// In jurisdictions that recognize copyright laws, the author or authors +// of this software dedicate any and all copyright interest in the +// software to the public domain. We make this dedication for the benefit +// of the public at large and to the detriment of our heirs and +// successors. We intend this dedication to be an overt act of +// relinquishment in perpetuity of all present and future rights to this +// software under copyright law. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR +// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +// +// For more information, please refer to <http://unlicense.org/> + +#include "utest.h" + +#ifdef _MSC_VER +// disable 'conditional expression is constant' - our examples below use this! +#pragma warning(disable : 4127) +#endif + +UTEST(cpp_no_exceptions, ASSERT_TRUE) { ASSERT_TRUE(1); } + +UTEST(cpp_no_exceptions, ASSERT_FALSE) { ASSERT_FALSE(0); } + +UTEST(cpp_no_exceptions, ASSERT_EQ) { ASSERT_EQ(1, 1); } + +UTEST(cpp_no_exceptions, ASSERT_NE) { ASSERT_NE(1, 2); } + +UTEST(cpp_no_exceptions, ASSERT_LT) { ASSERT_LT(1, 2); } + +UTEST(cpp_no_exceptions, ASSERT_LE) { + ASSERT_LE(1, 1); + ASSERT_LE(1, 2); +} + +UTEST(cpp_no_exceptions, ASSERT_GT) { ASSERT_GT(2, 1); } + +UTEST(cpp_no_exceptions, ASSERT_GE) { + ASSERT_GE(1, 1); + ASSERT_GE(2, 1); +} + +UTEST(cpp_no_exceptions, ASSERT_STREQ) { ASSERT_STREQ("foo", "foo"); } + +UTEST(cpp_no_exceptions, ASSERT_STRNE) { ASSERT_STRNE("foo", "bar"); } + +UTEST(cpp_no_exceptions, ASSERT_STRNEQ) { + ASSERT_STRNEQ("foo", "foobar", strlen("foo")); +} + +UTEST(cpp_no_exceptions, ASSERT_STRNNE) { + ASSERT_STRNNE("foo", "barfoo", strlen("foo")); +} + +UTEST(cpp_no_exceptions, EXPECT_TRUE) { EXPECT_TRUE(1); } + +UTEST(cpp_no_exceptions, EXPECT_FALSE) { EXPECT_FALSE(0); } + +UTEST(cpp_no_exceptions, EXPECT_EQ) { EXPECT_EQ(1, 1); } + +UTEST(cpp_no_exceptions, EXPECT_NE) { EXPECT_NE(1, 2); } + +UTEST(cpp_no_exceptions, EXPECT_LT) { EXPECT_LT(1, 2); } + +UTEST(cpp_no_exceptions, EXPECT_LE) { + EXPECT_LE(1, 1); + EXPECT_LE(1, 2); +} + +UTEST(cpp_no_exceptions, EXPECT_GT) { EXPECT_GT(2, 1); } + +UTEST(cpp_no_exceptions, EXPECT_GE) { + EXPECT_GE(1, 1); + EXPECT_GE(2, 1); +} + +UTEST(cpp_no_exceptions, EXPECT_STREQ) { EXPECT_STREQ("foo", "foo"); } + +UTEST(cpp_no_exceptions, EXPECT_STRNE) { EXPECT_STRNE("foo", "bar"); } + +UTEST(cpp_no_exceptions, EXPECT_STRNEQ) { + EXPECT_STRNEQ("foo", "foobar", strlen("foo")); +} + +UTEST(cpp_no_exceptions, EXPECT_STRNNE) { + EXPECT_STRNNE("foo", "barfoo", strlen("foo")); +} + +UTEST(cpp_no_exceptions, no_double_eval) { + int i = 0; + ASSERT_EQ(i++, 0); + ASSERT_EQ(i, 1); +} + +struct MyTestF { + int foo; +}; + +UTEST_F_SETUP(MyTestF) { + ASSERT_EQ(0, utest_fixture->foo); + utest_fixture->foo = 42; +} + +UTEST_F_TEARDOWN(MyTestF) { ASSERT_EQ(13, utest_fixture->foo); } + +UTEST_F(MyTestF, cpp_no_exceptions_1) { + ASSERT_EQ(42, utest_fixture->foo); + utest_fixture->foo = 13; +} + +UTEST_F(MyTestF, cpp_no_exceptions_2) { + ASSERT_EQ(42, utest_fixture->foo); + utest_fixture->foo = 13; +} + +struct MyTestI { + size_t foo; + size_t bar; +}; + +UTEST_I_SETUP(MyTestI) { + ASSERT_EQ(0u, utest_fixture->foo); + ASSERT_EQ(0u, utest_fixture->bar); + utest_fixture->foo = 42; + utest_fixture->bar = utest_index; +} + +UTEST_I_TEARDOWN(MyTestI) { + ASSERT_EQ(13u, utest_fixture->foo); + ASSERT_EQ(utest_index, utest_fixture->bar); +} + +UTEST_I(MyTestI, cpp_no_exceptions_1, 2) { + ASSERT_GT(2u, utest_fixture->bar); + utest_fixture->foo = 13; +} + +UTEST_I(MyTestI, cpp_no_exceptions_2, 128) { + ASSERT_GT(128u, utest_fixture->bar); + utest_fixture->foo = 13; +} + +UTEST(cpp_no_exceptions, Float) { + float a = 1; + float b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp_no_exceptions, Double) { + double a = 1; + double b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp_no_exceptions, LongDouble) { + long double a = 1; + long double b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp_no_exceptions, Char) { + signed char a = 1; + signed char b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp_no_exceptions, UChar) { + unsigned char a = 1; + unsigned char b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp_no_exceptions, Short) { + short a = 1; + short b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp_no_exceptions, UShort) { + unsigned short a = 1; + unsigned short b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp_no_exceptions, Int) { + int a = 1; + int b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp_no_exceptions, UInt) { + unsigned int a = 1; + unsigned int b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp_no_exceptions, Long) { + long a = 1; + long b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp_no_exceptions, ULong) { + unsigned long a = 1; + unsigned long b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +#if defined(__clang__) +#if __has_warning("-Wunsafe-buffer-usage") +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wunsafe-buffer-usage" +#endif +#endif + +UTEST(cpp_no_exceptions, Ptr) { + char foo = 42; + EXPECT_NE(&foo, &foo + 1); +} + +static const int data[4] = {42, 13, 6, -53}; + +UTEST(cpp_no_exceptions, Array) { EXPECT_NE(data, data + 1); } + +#if defined(__clang__) +#if __has_warning("-Wunsafe-buffer-usage") +#pragma clang diagnostic pop +#endif +#endif + +UTEST(cpp_no_exceptions, Near) { + float a = 42.0f; + float b = 42.01f; + EXPECT_NEAR(a, b, 0.01f); + ASSERT_NEAR(a, b, 0.01f); +} + +UTEST(cpp_no_exceptions, Todo) { UTEST_SKIP("Not yet implemented!"); } diff --git a/third_party/utest/test/type_printers.c b/third_party/utest/test/type_printers.c @@ -0,0 +1,5 @@ +// TODO: Broken under MINGW for some reason. +#if !(defined(__MINGW32__) || defined(__MINGW64__)) +#include "utest.h" +#include "type_printers.inc" +#endif diff --git a/third_party/utest/test/type_printers.cpp b/third_party/utest/test/type_printers.cpp @@ -0,0 +1,2 @@ +#include "utest.h" +#include "type_printers.inc" diff --git a/third_party/utest/test/type_printers.inc b/third_party/utest/test/type_printers.inc @@ -0,0 +1,52 @@ +#include <limits.h> + +#if __STDC_VERSION__ >= 201112L || __cplusplus >= 201103L + +#ifdef __cplusplus +UTEST(type_printers, cpp) { +#elif defined(UTEST_OVERLOADABLE) +UTEST(type_printers, overloadable) { +#elif __STDC_VERSION__ +UTEST(type_printers, stdc) { +#else +#error +#endif + + FILE *old = utest_state.output; + FILE *out = tmpfile(); + ASSERT_TRUE(!!out); + utest_state.output = out; + + int i = INT_MIN; + long l = LONG_MIN; + long long ll = LLONG_MIN; + unsigned u = UINT_MAX; + unsigned long ul = ULONG_MAX; + unsigned long long ull = ULLONG_MAX; + float f = 0.f; + double d = 0.; + long double ld = 0.l; + utest_type_printer(i); + utest_type_printer(l); + utest_type_printer(ll); + utest_type_printer(u); + utest_type_printer(ul); + utest_type_printer(ull); + utest_type_printer(f); + utest_type_printer(d); + utest_type_printer(ld); + + char expected[1024] = {0}; + size_t expected_len = + UTEST_SNPRINTF(expected, sizeof expected - 1, "%d%ld%lld%u%lu%llu%f%f%Lf", + i, l, ll, u, ul, ull, f, d, ld); + fflush(out); + rewind(out); + char buf[1024] = {'\0'}; + const size_t n = fread(buf, 1, sizeof buf, out); + fclose(out); + utest_state.output = old; + ASSERT_EQ(n, expected_len); + ASSERT_STREQ(buf, expected); +} +#endif diff --git a/third_party/utest/utest.h b/third_party/utest/utest.h @@ -0,0 +1,1719 @@ +/* + The latest version of this library is available on GitHub; + https://github.com/sheredom/utest.h +*/ + +/* + This is free and unencumbered software released into the public domain. + + Anyone is free to copy, modify, publish, use, compile, sell, or + distribute this software, either in source code form or as a compiled + binary, for any purpose, commercial or non-commercial, and by any + means. + + In jurisdictions that recognize copyright laws, the author or authors + of this software dedicate any and all copyright interest in the + software to the public domain. We make this dedication for the benefit + of the public at large and to the detriment of our heirs and + successors. We intend this dedication to be an overt act of + relinquishment in perpetuity of all present and future rights to this + software under copyright law. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR + OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + OTHER DEALINGS IN THE SOFTWARE. + + For more information, please refer to <http://unlicense.org/> +*/ + +#ifndef SHEREDOM_UTEST_H_INCLUDED +#define SHEREDOM_UTEST_H_INCLUDED + +#ifdef _MSC_VER +/* + Disable warning about not inlining 'inline' functions. +*/ +#pragma warning(disable : 4710) + +/* + Disable warning about inlining functions that are not marked 'inline'. +*/ +#pragma warning(disable : 4711) + +/* + Disable warning for alignment padding added +*/ +#pragma warning(disable : 4820) + +#if _MSC_VER > 1900 +/* + Disable warning about preprocessor macros not being defined in MSVC headers. +*/ +#pragma warning(disable : 4668) + +/* + Disable warning about no function prototype given in MSVC headers. +*/ +#pragma warning(disable : 4255) + +/* + Disable warning about pointer or reference to potentially throwing function. +*/ +#pragma warning(disable : 5039) + +/* + Disable warning about macro expansion producing 'defined' has undefined + behavior. +*/ +#pragma warning(disable : 5105) +#endif + +#if _MSC_VER > 1930 +/* + Disable warning about 'const' variable is not used. +*/ +#pragma warning(disable : 5264) +#endif + +#pragma warning(push, 1) +#endif + +#if defined(_MSC_VER) && (_MSC_VER < 1920) +typedef __int64 utest_int64_t; +typedef unsigned __int64 utest_uint64_t; +typedef unsigned __int32 utest_uint32_t; +#else +#include <stdint.h> +typedef int64_t utest_int64_t; +typedef uint64_t utest_uint64_t; +typedef uint32_t utest_uint32_t; +#endif + +#include <stddef.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <errno.h> + +#if defined(__cplusplus) +#if defined(_MSC_VER) && !defined(_CPPUNWIND) +/* We're on MSVC and the compiler is compiling without exception support! */ +#elif !defined(_MSC_VER) && !defined(__EXCEPTIONS) +/* We're on a GCC/Clang compiler that doesn't have exception support! */ +#else +#define UTEST_HAS_EXCEPTIONS 1 +#endif +#endif + +#if defined(UTEST_HAS_EXCEPTIONS) +#include <stdexcept> +#endif + +#if defined(_MSC_VER) +#pragma warning(pop) +#endif + +#if defined(__cplusplus) +#define UTEST_C_FUNC extern "C" +#else +#define UTEST_C_FUNC +#endif + +#define UTEST_TEST_PASSED (0) +#define UTEST_TEST_FAILURE (1) +#define UTEST_TEST_SKIPPED (2) + +#if defined(__TINYC__) +#define UTEST_ATTRIBUTE(a) __attribute((a)) +#else +#define UTEST_ATTRIBUTE(a) __attribute__((a)) +#endif + +#if defined(_MSC_VER) || defined(__MINGW64__) || defined(__MINGW32__) + +#if defined(__MINGW64__) || defined(__MINGW32__) +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wpragmas" +#pragma GCC diagnostic ignored "-Wunknown-pragmas" +#endif + +#if defined(_WINDOWS_) || defined(_WINDOWS_H) +typedef LARGE_INTEGER utest_large_integer; +#else +// use old QueryPerformanceCounter definitions (not sure is this needed in some +// edge cases or not) on Win7 with VS2015 these extern declaration cause "second +// C linkage of overloaded function not allowed" error +typedef union { + struct { + unsigned long LowPart; + long HighPart; + } DUMMYSTRUCTNAME; + struct { + unsigned long LowPart; + long HighPart; + } u; + utest_int64_t QuadPart; +} utest_large_integer; + +UTEST_C_FUNC __declspec(dllimport) int __stdcall QueryPerformanceCounter( + utest_large_integer *); +UTEST_C_FUNC __declspec(dllimport) int __stdcall QueryPerformanceFrequency( + utest_large_integer *); + +#if defined(__MINGW64__) || defined(__MINGW32__) +#pragma GCC diagnostic pop +#endif +#endif + +#elif defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__) || \ + defined(__NetBSD__) || defined(__DragonFly__) || defined(__sun__) || \ + defined(__HAIKU__) +/* + slightly obscure include here - we need to include glibc's features.h, but + we don't want to just include a header that might not be defined for other + c libraries like musl. Instead we include limits.h, which we know on all + glibc distributions includes features.h +*/ +#include <limits.h> + +#if defined(__GLIBC__) && defined(__GLIBC_MINOR__) +#include <time.h> + +#if ((2 < __GLIBC__) || ((2 == __GLIBC__) && (17 <= __GLIBC_MINOR__))) +/* glibc is version 2.17 or above, so we can just use clock_gettime */ +#define UTEST_USE_CLOCKGETTIME +#else +#include <sys/syscall.h> +#include <unistd.h> +#endif +#else // Other libc implementations +#include <time.h> +#define UTEST_USE_CLOCKGETTIME +#endif + +#elif defined(__APPLE__) +#include <time.h> +#endif + +#if defined(_MSC_VER) && (_MSC_VER < 1920) +#define UTEST_PRId64 "I64d" +#define UTEST_PRIu64 "I64u" +#else +#include <inttypes.h> + +#define UTEST_PRId64 PRId64 +#define UTEST_PRIu64 PRIu64 +#endif + +#if defined(__cplusplus) +#define UTEST_INLINE inline + +#if defined(__clang__) +#define UTEST_INITIALIZER_BEGIN_DISABLE_WARNINGS \ + _Pragma("clang diagnostic push") \ + _Pragma("clang diagnostic ignored \"-Wglobal-constructors\"") + +#define UTEST_INITIALIZER_END_DISABLE_WARNINGS _Pragma("clang diagnostic pop") +#else +#define UTEST_INITIALIZER_BEGIN_DISABLE_WARNINGS +#define UTEST_INITIALIZER_END_DISABLE_WARNINGS +#endif + +#define UTEST_INITIALIZER(f) \ + struct f##_cpp_struct { \ + f##_cpp_struct(); \ + }; \ + UTEST_INITIALIZER_BEGIN_DISABLE_WARNINGS static f##_cpp_struct \ + f##_cpp_global UTEST_INITIALIZER_END_DISABLE_WARNINGS; \ + f##_cpp_struct::f##_cpp_struct() +#elif defined(_MSC_VER) +#define UTEST_INLINE __forceinline + +#if defined(_WIN64) +#define UTEST_SYMBOL_PREFIX +#else +#define UTEST_SYMBOL_PREFIX "_" +#endif + +#if defined(__clang__) +#define UTEST_INITIALIZER_BEGIN_DISABLE_WARNINGS \ + _Pragma("clang diagnostic push") \ + _Pragma("clang diagnostic ignored \"-Wmissing-variable-declarations\"") + +#define UTEST_INITIALIZER_END_DISABLE_WARNINGS _Pragma("clang diagnostic pop") +#else +#define UTEST_INITIALIZER_BEGIN_DISABLE_WARNINGS +#define UTEST_INITIALIZER_END_DISABLE_WARNINGS +#endif + +#pragma section(".CRT$XCU", read) +#define UTEST_INITIALIZER(f) \ + static void __cdecl f(void); \ + UTEST_INITIALIZER_BEGIN_DISABLE_WARNINGS \ + __pragma(comment(linker, "/include:" UTEST_SYMBOL_PREFIX #f "_")) \ + UTEST_C_FUNC \ + __declspec(allocate(".CRT$XCU")) void(__cdecl * f##_)(void) = f; \ + UTEST_INITIALIZER_END_DISABLE_WARNINGS \ + static void __cdecl f(void) +#else +#if defined(__linux__) +#if defined(__clang__) +#if __has_warning("-Wreserved-id-macro") +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wreserved-id-macro" +#endif +#endif + +#define __STDC_FORMAT_MACROS 1 + +#if defined(__clang__) +#if __has_warning("-Wreserved-id-macro") +#pragma clang diagnostic pop +#endif +#endif +#endif + +#define UTEST_INLINE inline + +#define UTEST_INITIALIZER(f) \ + static void f(void) UTEST_ATTRIBUTE(constructor); \ + static void f(void) +#endif + +#if defined(__cplusplus) +#define UTEST_CAST(type, x) static_cast<type>(x) +#define UTEST_PTR_CAST(type, x) reinterpret_cast<type>(x) +#define UTEST_EXTERN extern "C" +#define UTEST_NULL NULL +#else +#define UTEST_CAST(type, x) ((type)(x)) +#define UTEST_PTR_CAST(type, x) ((type)(x)) +#define UTEST_EXTERN extern +#define UTEST_NULL 0 +#endif + +#ifdef _MSC_VER +/* + io.h contains definitions for some structures with natural padding. This is + uninteresting, but for some reason MSVC's behaviour is to warn about + including this system header. That *is* interesting +*/ +#pragma warning(disable : 4820) +#pragma warning(push, 1) +#include <io.h> +#pragma warning(pop) +#define UTEST_COLOUR_OUTPUT() (_isatty(_fileno(stdout))) +#else +#if defined(__EMSCRIPTEN__) +#include <emscripten/html5.h> +#define UTEST_COLOUR_OUTPUT() false +#else +#include <unistd.h> +#define UTEST_COLOUR_OUTPUT() (isatty(STDOUT_FILENO)) +#endif +#endif + +static UTEST_INLINE void *utest_realloc(void *const pointer, size_t new_size) { + void *const new_pointer = realloc(pointer, new_size); + + if (UTEST_NULL == new_pointer) { + free(pointer); + } + + return new_pointer; +} + +// Prevent 64-bit integer overflow when computing a timestamp by using a trick +// from Sokol: +// https://github.com/floooh/sokol/blob/189843bf4f86969ca4cc4b6d94e793a37c5128a7/sokol_time.h#L204 +static UTEST_INLINE utest_int64_t utest_mul_div(const utest_int64_t value, + const utest_int64_t numer, + const utest_int64_t denom) { + const utest_int64_t q = value / denom; + const utest_int64_t r = value % denom; + return q * numer + r * numer / denom; +} + +static UTEST_INLINE utest_int64_t utest_ns(void) { +#if defined(_MSC_VER) || defined(__MINGW64__) || defined(__MINGW32__) + utest_large_integer counter; + utest_large_integer frequency; + QueryPerformanceCounter(&counter); + QueryPerformanceFrequency(&frequency); + return utest_mul_div(counter.QuadPart, 1000000000, frequency.QuadPart); +#elif defined(__linux__) && defined(__STRICT_ANSI__) + return utest_mul_div(clock(), 1000000000, CLOCKS_PER_SEC); +#elif defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__) || \ + defined(__NetBSD__) || defined(__DragonFly__) || defined(__sun__) || \ + defined(__HAIKU__) + struct timespec ts; +#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L) && \ + !defined(__HAIKU__) + timespec_get(&ts, TIME_UTC); +#else + const clockid_t cid = CLOCK_REALTIME; +#if defined(UTEST_USE_CLOCKGETTIME) + clock_gettime(cid, &ts); +#else + syscall(SYS_clock_gettime, cid, &ts); +#endif +#endif + return UTEST_CAST(utest_int64_t, ts.tv_sec) * 1000 * 1000 * 1000 + ts.tv_nsec; +#elif __APPLE__ + return UTEST_CAST(utest_int64_t, clock_gettime_nsec_np(CLOCK_UPTIME_RAW)); +#elif __EMSCRIPTEN__ + return emscripten_performance_now() * 1000000.0; +#else +#error Unsupported platform! +#endif +} + +typedef void (*utest_testcase_t)(int *, size_t); + +struct utest_test_state_s { + utest_testcase_t func; + size_t index; + char *name; +}; + +struct utest_state_s { + struct utest_test_state_s *tests; + size_t tests_length; + FILE *output; +}; + +/* extern to the global state utest needs to execute */ +UTEST_EXTERN struct utest_state_s utest_state; + +#if defined(_MSC_VER) +#define UTEST_WEAK __forceinline +#elif defined(__MINGW32__) || defined(__MINGW64__) +#define UTEST_WEAK static UTEST_ATTRIBUTE(used) +#elif defined(__clang__) || defined(__GNUC__) || defined(__TINYC__) +#define UTEST_WEAK UTEST_ATTRIBUTE(weak) +#else +#error Non clang, non gcc, non MSVC, non tcc compiler found! +#endif + +#if defined(_MSC_VER) +#define UTEST_UNUSED +#else +#define UTEST_UNUSED UTEST_ATTRIBUTE(unused) +#endif + +#ifdef __clang__ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wvariadic-macros" +#pragma clang diagnostic ignored "-Wc++98-compat-pedantic" +#endif +#define UTEST_PRINTF(...) \ + if (utest_state.output) { \ + fprintf(utest_state.output, __VA_ARGS__); \ + } \ + printf(__VA_ARGS__) +#ifdef __clang__ +#pragma clang diagnostic pop +#endif + +#ifdef __clang__ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wvariadic-macros" +#pragma clang diagnostic ignored "-Wc++98-compat-pedantic" +#endif + +#ifdef _MSC_VER +#define UTEST_SNPRINTF(BUFFER, N, ...) _snprintf_s(BUFFER, N, N, __VA_ARGS__) +#else +#define UTEST_SNPRINTF(...) snprintf(__VA_ARGS__) +#endif + +#ifdef __clang__ +#pragma clang diagnostic pop +#endif + +#if defined(__cplusplus) +/* if we are using c++ we can use overloaded methods (its in the language) */ +#define UTEST_OVERLOADABLE +#elif defined(__clang__) +/* otherwise, if we are using clang with c - use the overloadable attribute */ +#define UTEST_OVERLOADABLE UTEST_ATTRIBUTE(overloadable) +#endif + +#if defined(__cplusplus) && (__cplusplus >= 201103L) + +#ifdef __clang__ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wc++98-compat-pedantic" +#endif + +#include <type_traits> + +template <typename T, bool is_enum = std::is_enum<T>::value> +struct utest_type_deducer final { + static void _(const T t); +}; + +template <> struct utest_type_deducer<char, false> { + static void _(const char c) { + if (std::is_signed<decltype(c)>::value) { + UTEST_PRINTF("%d", static_cast<int>(c)); + } else { + UTEST_PRINTF("%u", static_cast<unsigned int>(c)); + } + } +}; +template <> struct utest_type_deducer<signed char, false> { + static void _(const signed char c) { + UTEST_PRINTF("%d", static_cast<int>(c)); + } +}; + +template <> struct utest_type_deducer<unsigned char, false> { + static void _(const unsigned char c) { + UTEST_PRINTF("%u", static_cast<unsigned int>(c)); + } +}; + +template <> struct utest_type_deducer<short, false> { + static void _(const short s) { UTEST_PRINTF("%d", static_cast<int>(s)); } +}; + +template <> struct utest_type_deducer<unsigned short, false> { + static void _(const unsigned short s) { + UTEST_PRINTF("%u", static_cast<unsigned>(s)); + } +}; + +template <> struct utest_type_deducer<float, false> { + static void _(const float f) { UTEST_PRINTF("%f", static_cast<double>(f)); } +}; + +template <> struct utest_type_deducer<double, false> { + static void _(const double d) { UTEST_PRINTF("%f", d); } +}; + +template <> struct utest_type_deducer<long double, false> { + static void _(const long double d) { +#if defined(__MINGW32__) || defined(__MINGW64__) + /* MINGW is weird - doesn't like LF at all?! */ + UTEST_PRINTF("%f", (double)d); +#else + UTEST_PRINTF("%Lf", d); +#endif + } +}; + +template <> struct utest_type_deducer<int, false> { + static void _(const int i) { UTEST_PRINTF("%d", i); } +}; + +template <> struct utest_type_deducer<unsigned int, false> { + static void _(const unsigned int i) { UTEST_PRINTF("%u", i); } +}; + +template <> struct utest_type_deducer<long, false> { + static void _(const long i) { UTEST_PRINTF("%ld", i); } +}; + +template <> struct utest_type_deducer<unsigned long, false> { + static void _(const unsigned long i) { UTEST_PRINTF("%lu", i); } +}; + +template <> struct utest_type_deducer<long long, false> { + static void _(const long long i) { UTEST_PRINTF("%lld", i); } +}; + +template <> struct utest_type_deducer<unsigned long long, false> { + static void _(const unsigned long long i) { UTEST_PRINTF("%llu", i); } +}; + +template <> struct utest_type_deducer<bool, false> { + static void _(const bool i) { UTEST_PRINTF(i ? "true" : "false"); } +}; + +template <typename T> struct utest_type_deducer<const T *, false> { + static void _(const T *t) { + UTEST_PRINTF("%p", static_cast<void *>(const_cast<T *>(t))); + } +}; + +template <typename T> struct utest_type_deducer<T *, false> { + static void _(T *t) { UTEST_PRINTF("%p", static_cast<void *>(t)); } +}; + +template <typename T> struct utest_type_deducer<T, true> { + static void _(const T t) { + UTEST_PRINTF("%llu", static_cast<unsigned long long>(t)); + } +}; + +template <> struct utest_type_deducer<std::nullptr_t, false> { + static void _(std::nullptr_t t) { + UTEST_PRINTF("%p", static_cast<void *>(t)); + } +}; + +template <typename T> +UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(const T t) { + utest_type_deducer<T>::_(t); +} + +#ifdef __clang__ +#pragma clang diagnostic pop +#endif + +#elif defined(UTEST_OVERLOADABLE) + +UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(signed char c); +UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(signed char c) { + UTEST_PRINTF("%d", UTEST_CAST(int, c)); +} + +UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(unsigned char c); +UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(unsigned char c) { + UTEST_PRINTF("%u", UTEST_CAST(unsigned int, c)); +} + +UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(float f); +UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(float f) { + UTEST_PRINTF("%f", UTEST_CAST(double, f)); +} + +UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(double d); +UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(double d) { + UTEST_PRINTF("%f", d); +} + +UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(long double d); +UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(long double d) { +#if defined(__MINGW32__) || defined(__MINGW64__) + /* MINGW is weird - doesn't like LF at all?! */ + UTEST_PRINTF("%f", (double)d); +#else + UTEST_PRINTF("%Lf", d); +#endif +} + +UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(int i); +UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(int i) { + UTEST_PRINTF("%d", i); +} + +UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(unsigned int i); +UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(unsigned int i) { + UTEST_PRINTF("%u", i); +} + +UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(long int i); +UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(long int i) { + UTEST_PRINTF("%ld", i); +} + +UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(long unsigned int i); +UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(long unsigned int i) { + UTEST_PRINTF("%lu", i); +} + +UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(const void *p); +UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(const void *p) { + UTEST_PRINTF("%p", p); +} + +/* + long long is a c++11 extension +*/ +#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) || \ + defined(__cplusplus) && (__cplusplus >= 201103L) || \ + (defined(__MINGW32__) || defined(__MINGW64__)) + +#ifdef __clang__ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wc++98-compat-pedantic" +#endif + +UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(long long int i); +UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(long long int i) { + UTEST_PRINTF("%lld", i); +} + +UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(long long unsigned int i); +UTEST_WEAK UTEST_OVERLOADABLE void +utest_type_printer(long long unsigned int i) { + UTEST_PRINTF("%llu", i); +} + +#ifdef __clang__ +#pragma clang diagnostic pop +#endif + +#endif +#elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L) && \ + !(defined(__MINGW32__) || defined(__MINGW64__)) || \ + defined(__TINYC__) +#define utest_type_printer(val) \ + UTEST_PRINTF( \ + _Generic((val), \ + signed char: "%d", \ + unsigned char: "%u", \ + short: "%d", \ + unsigned short: "%u", \ + int: "%d", \ + long: "%ld", \ + long long: "%lld", \ + unsigned: "%u", \ + unsigned long: "%lu", \ + unsigned long long: "%llu", \ + float: "%f", \ + double: "%f", \ + long double: "%Lf", \ + default: _Generic((val - val), ptrdiff_t: "%p", default: "undef")), \ + (val)) +#else +/* + we don't have the ability to print the values we got, so we create a macro + to tell our users we can't do anything fancy +*/ +#define utest_type_printer(...) UTEST_PRINTF("undef") +#endif + +#if defined(_MSC_VER) +#define UTEST_SURPRESS_WARNING_BEGIN \ + __pragma(warning(push)) __pragma(warning(disable : 4127)) \ + __pragma(warning(disable : 4571)) __pragma(warning(disable : 4130)) +#define UTEST_SURPRESS_WARNING_END __pragma(warning(pop)) +#else +#define UTEST_SURPRESS_WARNING_BEGIN +#define UTEST_SURPRESS_WARNING_END +#endif + +#if defined(__cplusplus) && (__cplusplus >= 201103L) +#define UTEST_AUTO(x) auto +#elif !defined(__cplusplus) + +#if defined(__clang__) +/* clang-format off */ +/* had to disable clang-format here because it malforms the pragmas */ +#define UTEST_AUTO(x) \ + _Pragma("clang diagnostic push") \ + _Pragma("clang diagnostic ignored \"-Wgnu-auto-type\"") __auto_type \ + _Pragma("clang diagnostic pop") +/* clang-format on */ +#else +#define UTEST_AUTO(x) __typeof__(x + 0) +#endif + +#else +#define UTEST_AUTO(x) typeof(x + 0) +#endif + +#if defined(__clang__) +#define UTEST_STRNCMP(x, y, size) \ + _Pragma("clang diagnostic push") \ + _Pragma("clang diagnostic ignored \"-Wdisabled-macro-expansion\"") \ + strncmp(x, y, size) _Pragma("clang diagnostic pop") +#else +#define UTEST_STRNCMP(x, y, size) strncmp(x, y, size) +#endif + +#if defined(_MSC_VER) +#define UTEST_STRNCPY(x, y, size) strcpy_s(x, size, y) +#elif !defined(__clang__) && defined(__GNUC__) +static UTEST_INLINE char * +utest_strncpy_gcc(char *const dst, const char *const src, const size_t size) { +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wstringop-overflow" + return strncpy(dst, src, size); +#pragma GCC diagnostic pop +} + +#define UTEST_STRNCPY(x, y, size) utest_strncpy_gcc(x, y, size) +#else +#define UTEST_STRNCPY(x, y, size) strncpy(x, y, size) +#endif + +#define UTEST_SKIP(msg) \ + do { \ + UTEST_PRINTF(" Skipped : '%s'\n", (msg)); \ + *utest_result = UTEST_TEST_SKIPPED; \ + return; \ + } while (0) + +#if defined(__clang__) +#define UTEST_COND(x, y, cond, msg, is_assert) \ + UTEST_SURPRESS_WARNING_BEGIN do { \ + _Pragma("clang diagnostic push") \ + _Pragma("clang diagnostic ignored \"-Wlanguage-extension-token\"") \ + _Pragma("clang diagnostic ignored \"-Wc++98-compat-pedantic\"") \ + _Pragma("clang diagnostic ignored \"-Wfloat-equal\"") \ + UTEST_AUTO(x) xEval = (x); \ + UTEST_AUTO(y) yEval = (y); \ + if (!((xEval)cond(yEval))) { \ + const char *const xAsString = #x; \ + const char *const yAsString = #y; \ + _Pragma("clang diagnostic pop") \ + UTEST_PRINTF("%s:%i: Failure\n", __FILE__, __LINE__); \ + UTEST_PRINTF(" Expected : ("); \ + UTEST_PRINTF("%s) " #cond " (%s", xAsString, yAsString); \ + UTEST_PRINTF(")\n"); \ + UTEST_PRINTF(" Actual : "); \ + utest_type_printer(xEval); \ + UTEST_PRINTF(" vs "); \ + utest_type_printer(yEval); \ + UTEST_PRINTF("\n"); \ + if (strlen(msg) > 0) { \ + UTEST_PRINTF(" Message : %s\n", msg); \ + } \ + *utest_result = UTEST_TEST_FAILURE; \ + if (is_assert) { \ + return; \ + } \ + } \ + } \ + while (0) \ + UTEST_SURPRESS_WARNING_END +#elif defined(__GNUC__) || defined(__TINYC__) +#define UTEST_COND(x, y, cond, msg, is_assert) \ + UTEST_SURPRESS_WARNING_BEGIN do { \ + UTEST_AUTO(x) xEval = (x); \ + UTEST_AUTO(y) yEval = (y); \ + if (!((xEval)cond(yEval))) { \ + const char *const xAsString = #x; \ + const char *const yAsString = #y; \ + UTEST_PRINTF("%s:%i: Failure\n", __FILE__, __LINE__); \ + UTEST_PRINTF(" Expected : ("); \ + UTEST_PRINTF("%s) " #cond " (%s", xAsString, yAsString); \ + UTEST_PRINTF(")\n"); \ + UTEST_PRINTF(" Actual : "); \ + utest_type_printer(xEval); \ + UTEST_PRINTF(" vs "); \ + utest_type_printer(yEval); \ + UTEST_PRINTF("\n"); \ + if (strlen(msg) > 0) { \ + UTEST_PRINTF(" Message : %s\n", msg); \ + } \ + *utest_result = UTEST_TEST_FAILURE; \ + if (is_assert) { \ + return; \ + } \ + } \ + } \ + while (0) \ + UTEST_SURPRESS_WARNING_END +#else +#define UTEST_COND(x, y, cond, msg, is_assert) \ + UTEST_SURPRESS_WARNING_BEGIN do { \ + if (!((x)cond(y))) { \ + UTEST_PRINTF("%s:%i: Failure (Expected " #cond " Actual)", __FILE__, \ + __LINE__); \ + if (strlen(msg) > 0) { \ + UTEST_PRINTF(" Message : %s", msg); \ + } \ + UTEST_PRINTF("\n"); \ + *utest_result = UTEST_TEST_FAILURE; \ + if (is_assert) { \ + return; \ + } \ + } \ + } \ + while (0) \ + UTEST_SURPRESS_WARNING_END +#endif + +#define EXPECT_EQ(x, y) UTEST_COND(x, y, ==, "", 0) +#define EXPECT_EQ_MSG(x, y, msg) UTEST_COND(x, y, ==, msg, 0) +#define ASSERT_EQ(x, y) UTEST_COND(x, y, ==, "", 1) +#define ASSERT_EQ_MSG(x, y, msg) UTEST_COND(x, y, ==, msg, 1) + +#define EXPECT_NE(x, y) UTEST_COND(x, y, !=, "", 0) +#define EXPECT_NE_MSG(x, y, msg) UTEST_COND(x, y, !=, msg, 0) +#define ASSERT_NE(x, y) UTEST_COND(x, y, !=, "", 1) +#define ASSERT_NE_MSG(x, y, msg) UTEST_COND(x, y, !=, msg, 1) + +#define EXPECT_LT(x, y) UTEST_COND(x, y, <, "", 0) +#define EXPECT_LT_MSG(x, y, msg) UTEST_COND(x, y, <, msg, 0) +#define ASSERT_LT(x, y) UTEST_COND(x, y, <, "", 1) +#define ASSERT_LT_MSG(x, y, msg) UTEST_COND(x, y, <, msg, 1) + +#define EXPECT_LE(x, y) UTEST_COND(x, y, <=, "", 0) +#define EXPECT_LE_MSG(x, y, msg) UTEST_COND(x, y, <=, msg, 0) +#define ASSERT_LE(x, y) UTEST_COND(x, y, <=, "", 1) +#define ASSERT_LE_MSG(x, y, msg) UTEST_COND(x, y, <=, msg, 1) + +#define EXPECT_GT(x, y) UTEST_COND(x, y, >, "", 0) +#define EXPECT_GT_MSG(x, y, msg) UTEST_COND(x, y, >, msg, 0) +#define ASSERT_GT(x, y) UTEST_COND(x, y, >, "", 1) +#define ASSERT_GT_MSG(x, y, msg) UTEST_COND(x, y, >, msg, 1) + +#define EXPECT_GE(x, y) UTEST_COND(x, y, >=, "", 0) +#define EXPECT_GE_MSG(x, y, msg) UTEST_COND(x, y, >=, msg, 0) +#define ASSERT_GE(x, y) UTEST_COND(x, y, >=, "", 1) +#define ASSERT_GE_MSG(x, y, msg) UTEST_COND(x, y, >=, msg, 1) + +#define UTEST_TRUE(x, msg, is_assert) \ + UTEST_SURPRESS_WARNING_BEGIN do { \ + const int xEval = !!(x); \ + if (!(xEval)) { \ + UTEST_PRINTF("%s:%i: Failure\n", __FILE__, __LINE__); \ + UTEST_PRINTF(" Expected : true\n"); \ + UTEST_PRINTF(" Actual : %s\n", (xEval) ? "true" : "false"); \ + if (strlen(msg) > 0) { \ + UTEST_PRINTF(" Message : %s\n", msg); \ + } \ + *utest_result = UTEST_TEST_FAILURE; \ + if (is_assert) { \ + return; \ + } \ + } \ + } \ + while (0) \ + UTEST_SURPRESS_WARNING_END + +#define EXPECT_TRUE(x) UTEST_TRUE(x, "", 0) +#define EXPECT_TRUE_MSG(x, msg) UTEST_TRUE(x, msg, 0) +#define ASSERT_TRUE(x) UTEST_TRUE(x, "", 1) +#define ASSERT_TRUE_MSG(x, msg) UTEST_TRUE(x, msg, 1) + +#define UTEST_FALSE(x, msg, is_assert) \ + UTEST_SURPRESS_WARNING_BEGIN do { \ + const int xEval = !!(x); \ + if (xEval) { \ + UTEST_PRINTF("%s:%i: Failure\n", __FILE__, __LINE__); \ + UTEST_PRINTF(" Expected : false\n"); \ + UTEST_PRINTF(" Actual : %s\n", (xEval) ? "true" : "false"); \ + if (strlen(msg) > 0) { \ + UTEST_PRINTF(" Message : %s\n", msg); \ + } \ + *utest_result = UTEST_TEST_FAILURE; \ + if (is_assert) { \ + return; \ + } \ + } \ + } \ + while (0) \ + UTEST_SURPRESS_WARNING_END + +#define EXPECT_FALSE(x) UTEST_FALSE(x, "", 0) +#define EXPECT_FALSE_MSG(x, msg) UTEST_FALSE(x, msg, 0) +#define ASSERT_FALSE(x) UTEST_FALSE(x, "", 1) +#define ASSERT_FALSE_MSG(x, msg) UTEST_FALSE(x, msg, 1) + +#define UTEST_STREQ(x, y, msg, is_assert) \ + UTEST_SURPRESS_WARNING_BEGIN do { \ + const char *xEval = (x); \ + const char *yEval = (y); \ + if (UTEST_NULL == xEval || UTEST_NULL == yEval || \ + 0 != strcmp(xEval, yEval)) { \ + UTEST_PRINTF("%s:%i: Failure\n", __FILE__, __LINE__); \ + UTEST_PRINTF(" Expected : \"%s\"\n", xEval); \ + UTEST_PRINTF(" Actual : \"%s\"\n", yEval); \ + if (strlen(msg) > 0) { \ + UTEST_PRINTF(" Message : %s\n", msg); \ + } \ + *utest_result = UTEST_TEST_FAILURE; \ + if (is_assert) { \ + return; \ + } \ + } \ + } \ + while (0) \ + UTEST_SURPRESS_WARNING_END + +#define EXPECT_STREQ(x, y) UTEST_STREQ(x, y, "", 0) +#define EXPECT_STREQ_MSG(x, y, msg) UTEST_STREQ(x, y, msg, 0) +#define ASSERT_STREQ(x, y) UTEST_STREQ(x, y, "", 1) +#define ASSERT_STREQ_MSG(x, y, msg) UTEST_STREQ(x, y, msg, 1) + +#define UTEST_STRNE(x, y, msg, is_assert) \ + UTEST_SURPRESS_WARNING_BEGIN do { \ + const char *xEval = (x); \ + const char *yEval = (y); \ + if (UTEST_NULL == xEval || UTEST_NULL == yEval || \ + 0 == strcmp(xEval, yEval)) { \ + UTEST_PRINTF("%s:%i: Failure\n", __FILE__, __LINE__); \ + UTEST_PRINTF(" Expected : \"%s\"\n", xEval); \ + UTEST_PRINTF(" Actual : \"%s\"\n", yEval); \ + if (strlen(msg) > 0) { \ + UTEST_PRINTF(" Message : %s\n", msg); \ + } \ + *utest_result = UTEST_TEST_FAILURE; \ + if (is_assert) { \ + return; \ + } \ + } \ + } \ + while (0) \ + UTEST_SURPRESS_WARNING_END + +#define EXPECT_STRNE(x, y) UTEST_STRNE(x, y, "", 0) +#define EXPECT_STRNE_MSG(x, y, msg) UTEST_STRNE(x, y, msg, 0) +#define ASSERT_STRNE(x, y) UTEST_STRNE(x, y, "", 1) +#define ASSERT_STRNE_MSG(x, y, msg) UTEST_STRNE(x, y, msg, 1) + +#define UTEST_STRNEQ(x, y, n, msg, is_assert) \ + UTEST_SURPRESS_WARNING_BEGIN do { \ + const char *xEval = (x); \ + const char *yEval = (y); \ + const size_t nEval = UTEST_CAST(size_t, n); \ + if (UTEST_NULL == xEval || UTEST_NULL == yEval || \ + 0 != UTEST_STRNCMP(xEval, yEval, nEval)) { \ + UTEST_PRINTF("%s:%i: Failure\n", __FILE__, __LINE__); \ + UTEST_PRINTF(" Expected : \"%.*s\"\n", UTEST_CAST(int, nEval), xEval); \ + UTEST_PRINTF(" Actual : \"%.*s\"\n", UTEST_CAST(int, nEval), yEval); \ + if (strlen(msg) > 0) { \ + UTEST_PRINTF(" Message : %s\n", msg); \ + } \ + *utest_result = UTEST_TEST_FAILURE; \ + if (is_assert) { \ + return; \ + } \ + } \ + } \ + while (0) \ + UTEST_SURPRESS_WARNING_END + +#define EXPECT_STRNEQ(x, y, n) UTEST_STRNEQ(x, y, n, "", 0) +#define EXPECT_STRNEQ_MSG(x, y, n, msg) UTEST_STRNEQ(x, y, n, msg, 0) +#define ASSERT_STRNEQ(x, y, n) UTEST_STRNEQ(x, y, n, "", 1) +#define ASSERT_STRNEQ_MSG(x, y, n, msg) UTEST_STRNEQ(x, y, n, msg, 1) + +#define UTEST_STRNNE(x, y, n, msg, is_assert) \ + UTEST_SURPRESS_WARNING_BEGIN do { \ + const char *xEval = (x); \ + const char *yEval = (y); \ + const size_t nEval = UTEST_CAST(size_t, n); \ + if (UTEST_NULL == xEval || UTEST_NULL == yEval || \ + 0 == UTEST_STRNCMP(xEval, yEval, nEval)) { \ + UTEST_PRINTF("%s:%i: Failure\n", __FILE__, __LINE__); \ + UTEST_PRINTF(" Expected : \"%.*s\"\n", UTEST_CAST(int, nEval), xEval); \ + UTEST_PRINTF(" Actual : \"%.*s\"\n", UTEST_CAST(int, nEval), yEval); \ + if (strlen(msg) > 0) { \ + UTEST_PRINTF(" Message : %s\n", msg); \ + } \ + *utest_result = UTEST_TEST_FAILURE; \ + if (is_assert) { \ + return; \ + } \ + } \ + } \ + while (0) \ + UTEST_SURPRESS_WARNING_END + +#define EXPECT_STRNNE(x, y, n) UTEST_STRNNE(x, y, n, "", 0) +#define EXPECT_STRNNE_MSG(x, y, n, msg) UTEST_STRNNE(x, y, n, msg, 0) +#define ASSERT_STRNNE(x, y, n) UTEST_STRNNE(x, y, n, "", 1) +#define ASSERT_STRNNE_MSG(x, y, n, msg) UTEST_STRNNE(x, y, n, msg, 1) + +#define UTEST_NEAR(x, y, epsilon, msg, is_assert) \ + UTEST_SURPRESS_WARNING_BEGIN do { \ + const double diff = \ + utest_fabs(UTEST_CAST(double, x) - UTEST_CAST(double, y)); \ + if (diff > UTEST_CAST(double, epsilon) || utest_isnan(diff)) { \ + UTEST_PRINTF("%s:%i: Failure\n", __FILE__, __LINE__); \ + UTEST_PRINTF(" Expected : %f\n", UTEST_CAST(double, x)); \ + UTEST_PRINTF(" Actual : %f\n", UTEST_CAST(double, y)); \ + if (strlen(msg) > 0) { \ + UTEST_PRINTF(" Message : %s\n", msg); \ + } \ + *utest_result = UTEST_TEST_FAILURE; \ + if (is_assert) { \ + return; \ + } \ + } \ + } \ + while (0) \ + UTEST_SURPRESS_WARNING_END + +#define EXPECT_NEAR(x, y, epsilon) UTEST_NEAR(x, y, epsilon, "", 0) +#define EXPECT_NEAR_MSG(x, y, epsilon, msg) UTEST_NEAR(x, y, epsilon, msg, 0) +#define ASSERT_NEAR(x, y, epsilon) UTEST_NEAR(x, y, epsilon, "", 1) +#define ASSERT_NEAR_MSG(x, y, epsilon, msg) UTEST_NEAR(x, y, epsilon, msg, 1) + +#if defined(UTEST_HAS_EXCEPTIONS) +#define UTEST_EXCEPTION(x, exception_type, msg, is_assert) \ + UTEST_SURPRESS_WARNING_BEGIN do { \ + int exception_caught = 0; \ + try { \ + x; \ + } catch (const exception_type &) { \ + exception_caught = 1; \ + } catch (...) { \ + exception_caught = 2; \ + } \ + if (1 != exception_caught) { \ + UTEST_PRINTF("%s:%i: Failure\n", __FILE__, __LINE__); \ + UTEST_PRINTF(" Expected : %s exception\n", #exception_type); \ + UTEST_PRINTF(" Actual : %s\n", (2 == exception_caught) \ + ? "Unexpected exception" \ + : "No exception"); \ + if (strlen(msg) > 0) { \ + UTEST_PRINTF(" Message : %s\n", msg); \ + } \ + *utest_result = UTEST_TEST_FAILURE; \ + if (is_assert) { \ + return; \ + } \ + } \ + } \ + while (0) \ + UTEST_SURPRESS_WARNING_END + +#define EXPECT_EXCEPTION(x, exception_type) \ + UTEST_EXCEPTION(x, exception_type, "", 0) +#define EXPECT_EXCEPTION_MSG(x, exception_type, msg) \ + UTEST_EXCEPTION(x, exception_type, msg, 0) +#define ASSERT_EXCEPTION(x, exception_type) \ + UTEST_EXCEPTION(x, exception_type, "", 1) +#define ASSERT_EXCEPTION_MSG(x, exception_type, msg) \ + UTEST_EXCEPTION(x, exception_type, msg, 1) + +#define UTEST_EXCEPTION_WITH_MESSAGE(x, exception_type, exception_message, \ + msg, is_assert) \ + UTEST_SURPRESS_WARNING_BEGIN do { \ + int exception_caught = 0; \ + char *message_caught = UTEST_NULL; \ + try { \ + x; \ + } catch (const exception_type &e) { \ + const char *const what = e.what(); \ + exception_caught = 1; \ + if (0 != \ + UTEST_STRNCMP(what, exception_message, strlen(exception_message))) { \ + const size_t message_size = strlen(what) + 1; \ + message_caught = UTEST_PTR_CAST(char *, malloc(message_size)); \ + UTEST_STRNCPY(message_caught, what, message_size); \ + } \ + } catch (...) { \ + exception_caught = 2; \ + } \ + if (1 != exception_caught) { \ + UTEST_PRINTF("%s:%i: Failure\n", __FILE__, __LINE__); \ + UTEST_PRINTF(" Expected : %s exception\n", #exception_type); \ + UTEST_PRINTF(" Actual : %s\n", (2 == exception_caught) \ + ? "Unexpected exception" \ + : "No exception"); \ + if (strlen(msg) > 0) { \ + UTEST_PRINTF(" Message : %s\n", msg); \ + } \ + *utest_result = UTEST_TEST_FAILURE; \ + if (is_assert) { \ + return; \ + } \ + } else if (UTEST_NULL != message_caught) { \ + UTEST_PRINTF("%s:%i: Failure\n", __FILE__, __LINE__); \ + UTEST_PRINTF(" Expected : %s exception with message %s\n", \ + #exception_type, exception_message); \ + UTEST_PRINTF(" Actual message : %s\n", message_caught); \ + if (strlen(msg) > 0) { \ + UTEST_PRINTF(" Message : %s\n", msg); \ + } \ + *utest_result = UTEST_TEST_FAILURE; \ + free(message_caught); \ + if (is_assert) { \ + return; \ + } \ + } \ + } \ + while (0) \ + UTEST_SURPRESS_WARNING_END + +#define EXPECT_EXCEPTION_WITH_MESSAGE(x, exception_type, exception_message) \ + UTEST_EXCEPTION_WITH_MESSAGE(x, exception_type, exception_message, "", 0) +#define EXPECT_EXCEPTION_WITH_MESSAGE_MSG(x, exception_type, \ + exception_message, msg) \ + UTEST_EXCEPTION_WITH_MESSAGE(x, exception_type, exception_message, msg, 0) +#define ASSERT_EXCEPTION_WITH_MESSAGE(x, exception_type, exception_message) \ + UTEST_EXCEPTION_WITH_MESSAGE(x, exception_type, exception_message, "", 1) +#define ASSERT_EXCEPTION_WITH_MESSAGE_MSG(x, exception_type, \ + exception_message, msg) \ + UTEST_EXCEPTION_WITH_MESSAGE(x, exception_type, exception_message, msg, 1) +#endif + +#if defined(__clang__) +#if __has_warning("-Wunsafe-buffer-usage") +#define UTEST_SURPRESS_WARNINGS_BEGIN \ + _Pragma("clang diagnostic push") \ + _Pragma("clang diagnostic ignored \"-Wunsafe-buffer-usage\"") +#define UTEST_SURPRESS_WARNINGS_END _Pragma("clang diagnostic pop") +#else +#define UTEST_SURPRESS_WARNINGS_BEGIN +#define UTEST_SURPRESS_WARNINGS_END +#endif +#elif defined(__GNUC__) && __GNUC__ >= 8 && defined(__cplusplus) +#define UTEST_SURPRESS_WARNINGS_BEGIN \ + _Pragma("GCC diagnostic push") \ + _Pragma("GCC diagnostic ignored \"-Wclass-memaccess\"") +#define UTEST_SURPRESS_WARNINGS_END _Pragma("GCC diagnostic pop") +#else +#define UTEST_SURPRESS_WARNINGS_BEGIN +#define UTEST_SURPRESS_WARNINGS_END +#endif + +#define UTEST(SET, NAME) \ + UTEST_SURPRESS_WARNINGS_BEGIN \ + UTEST_EXTERN struct utest_state_s utest_state; \ + static void utest_run_##SET##_##NAME(int *utest_result); \ + static void utest_##SET##_##NAME(int *utest_result, size_t utest_index) { \ + (void)utest_index; \ + utest_run_##SET##_##NAME(utest_result); \ + } \ + UTEST_INITIALIZER(utest_register_##SET##_##NAME) { \ + const size_t index = utest_state.tests_length++; \ + const char name_part[] = #SET "." #NAME; \ + const size_t name_size = strlen(name_part) + 1; \ + char *name = UTEST_PTR_CAST(char *, malloc(name_size)); \ + utest_state.tests = UTEST_PTR_CAST( \ + struct utest_test_state_s *, \ + utest_realloc(UTEST_PTR_CAST(void *, utest_state.tests), \ + sizeof(struct utest_test_state_s) * \ + utest_state.tests_length)); \ + if (utest_state.tests && name) { \ + utest_state.tests[index].func = &utest_##SET##_##NAME; \ + utest_state.tests[index].name = name; \ + utest_state.tests[index].index = 0; \ + UTEST_SNPRINTF(name, name_size, "%s", name_part); \ + } else { \ + if (utest_state.tests) { \ + free(utest_state.tests); \ + utest_state.tests = NULL; \ + } \ + if (name) { \ + free(name); \ + } \ + } \ + } \ + UTEST_SURPRESS_WARNINGS_END \ + void utest_run_##SET##_##NAME(int *utest_result) + +#define UTEST_F_SETUP(FIXTURE) \ + static void utest_f_setup_##FIXTURE(int *utest_result, \ + struct FIXTURE *utest_fixture) + +#define UTEST_F_TEARDOWN(FIXTURE) \ + static void utest_f_teardown_##FIXTURE(int *utest_result, \ + struct FIXTURE *utest_fixture) + +#define UTEST_F(FIXTURE, NAME) \ + UTEST_SURPRESS_WARNINGS_BEGIN \ + UTEST_EXTERN struct utest_state_s utest_state; \ + static void utest_f_setup_##FIXTURE(int *, struct FIXTURE *); \ + static void utest_f_teardown_##FIXTURE(int *, struct FIXTURE *); \ + static void utest_run_##FIXTURE##_##NAME(int *, struct FIXTURE *); \ + static void utest_f_##FIXTURE##_##NAME(int *utest_result, \ + size_t utest_index) { \ + struct FIXTURE fixture; \ + (void)utest_index; \ + memset(&fixture, 0, sizeof(fixture)); \ + utest_f_setup_##FIXTURE(utest_result, &fixture); \ + if (UTEST_TEST_PASSED != *utest_result) { \ + return; \ + } \ + utest_run_##FIXTURE##_##NAME(utest_result, &fixture); \ + utest_f_teardown_##FIXTURE(utest_result, &fixture); \ + } \ + UTEST_INITIALIZER(utest_register_##FIXTURE##_##NAME) { \ + const size_t index = utest_state.tests_length++; \ + const char name_part[] = #FIXTURE "." #NAME; \ + const size_t name_size = strlen(name_part) + 1; \ + char *name = UTEST_PTR_CAST(char *, malloc(name_size)); \ + utest_state.tests = UTEST_PTR_CAST( \ + struct utest_test_state_s *, \ + utest_realloc(UTEST_PTR_CAST(void *, utest_state.tests), \ + sizeof(struct utest_test_state_s) * \ + utest_state.tests_length)); \ + if (utest_state.tests && name) { \ + utest_state.tests[index].func = &utest_f_##FIXTURE##_##NAME; \ + utest_state.tests[index].name = name; \ + utest_state.tests[index].index = 0; \ + UTEST_SNPRINTF(name, name_size, "%s", name_part); \ + } else { \ + if (utest_state.tests) { \ + free(utest_state.tests); \ + utest_state.tests = NULL; \ + } \ + if (name) { \ + free(name); \ + } \ + } \ + } \ + UTEST_SURPRESS_WARNINGS_END \ + void utest_run_##FIXTURE##_##NAME(int *utest_result, \ + struct FIXTURE *utest_fixture) + +#define UTEST_I_SETUP(FIXTURE) \ + static void utest_i_setup_##FIXTURE( \ + int *utest_result, struct FIXTURE *utest_fixture, size_t utest_index) + +#define UTEST_I_TEARDOWN(FIXTURE) \ + static void utest_i_teardown_##FIXTURE( \ + int *utest_result, struct FIXTURE *utest_fixture, size_t utest_index) + +#define UTEST_I(FIXTURE, NAME, INDEX) \ + UTEST_SURPRESS_WARNINGS_BEGIN \ + UTEST_EXTERN struct utest_state_s utest_state; \ + static void utest_run_##FIXTURE##_##NAME##_##INDEX(int *, struct FIXTURE *); \ + static void utest_i_##FIXTURE##_##NAME##_##INDEX(int *utest_result, \ + size_t index) { \ + struct FIXTURE fixture; \ + memset(&fixture, 0, sizeof(fixture)); \ + utest_i_setup_##FIXTURE(utest_result, &fixture, index); \ + if (UTEST_TEST_PASSED != *utest_result) { \ + return; \ + } \ + utest_run_##FIXTURE##_##NAME##_##INDEX(utest_result, &fixture); \ + utest_i_teardown_##FIXTURE(utest_result, &fixture, index); \ + } \ + UTEST_INITIALIZER(utest_register_##FIXTURE##_##NAME##_##INDEX) { \ + size_t i; \ + utest_uint64_t iUp; \ + for (i = 0; i < (INDEX); i++) { \ + const size_t index = utest_state.tests_length++; \ + const char name_part[] = #FIXTURE "." #NAME; \ + const size_t name_size = strlen(name_part) + 32; \ + char *name = UTEST_PTR_CAST(char *, malloc(name_size)); \ + utest_state.tests = UTEST_PTR_CAST( \ + struct utest_test_state_s *, \ + utest_realloc(UTEST_PTR_CAST(void *, utest_state.tests), \ + sizeof(struct utest_test_state_s) * \ + utest_state.tests_length)); \ + if (utest_state.tests && name) { \ + utest_state.tests[index].func = &utest_i_##FIXTURE##_##NAME##_##INDEX; \ + utest_state.tests[index].index = i; \ + utest_state.tests[index].name = name; \ + iUp = UTEST_CAST(utest_uint64_t, i); \ + UTEST_SNPRINTF(name, name_size, "%s/%" UTEST_PRIu64, name_part, iUp); \ + } else { \ + if (utest_state.tests) { \ + free(utest_state.tests); \ + utest_state.tests = NULL; \ + } \ + if (name) { \ + free(name); \ + } \ + } \ + } \ + } \ + UTEST_SURPRESS_WARNINGS_END \ + void utest_run_##FIXTURE##_##NAME##_##INDEX(int *utest_result, \ + struct FIXTURE *utest_fixture) + +#ifdef __clang__ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wc++98-compat-pedantic" +#endif + +UTEST_WEAK +double utest_fabs(double d); +UTEST_WEAK +double utest_fabs(double d) { + union { + double d; + utest_uint64_t u; + } both; + both.d = d; + both.u &= 0x7fffffffffffffffu; + return both.d; +} + +UTEST_WEAK +int utest_isnan(double d); +UTEST_WEAK +int utest_isnan(double d) { + union { + double d; + utest_uint64_t u; + } both; + both.d = d; + both.u &= 0x7fffffffffffffffu; + return both.u > 0x7ff0000000000000u; +} + +#ifdef __clang__ +#pragma clang diagnostic pop +#endif + +#if defined(__clang__) +#if __has_warning("-Wunsafe-buffer-usage") +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wunsafe-buffer-usage" +#endif +#endif + +UTEST_WEAK +int utest_should_filter_test(const char *filter, const char *testcase); +UTEST_WEAK int utest_should_filter_test(const char *filter, + const char *testcase) { + if (filter) { + const char *filter_cur = filter; + const char *testcase_cur = testcase; + const char *filter_wildcard = UTEST_NULL; + + while (('\0' != *filter_cur) && ('\0' != *testcase_cur)) { + if ('*' == *filter_cur) { + /* store the position of the wildcard */ + filter_wildcard = filter_cur; + + /* skip the wildcard character */ + filter_cur++; + + while (('\0' != *filter_cur) && ('\0' != *testcase_cur)) { + if ('*' == *filter_cur) { + /* + we found another wildcard (filter is something like *foo*) so we + exit the current loop, and return to the parent loop to handle + the wildcard case + */ + break; + } else if (*filter_cur != *testcase_cur) { + /* otherwise our filter didn't match, so reset it */ + filter_cur = filter_wildcard; + } + + /* move testcase along */ + testcase_cur++; + + /* move filter along */ + filter_cur++; + } + + if (('\0' == *filter_cur) && ('\0' == *testcase_cur)) { + return 0; + } + + /* if the testcase has been exhausted, we don't have a match! */ + if ('\0' == *testcase_cur) { + return 1; + } + } else { + if (*testcase_cur != *filter_cur) { + /* test case doesn't match filter */ + return 1; + } else { + /* move our filter and testcase forward */ + testcase_cur++; + filter_cur++; + } + } + } + + if (('\0' != *filter_cur) || + (('\0' != *testcase_cur) && + ((filter == filter_cur) || ('*' != filter_cur[-1])))) { + /* we have a mismatch! */ + return 1; + } + } + + return 0; +} + +static UTEST_INLINE FILE *utest_fopen(const char *filename, const char *mode) { +#ifdef _MSC_VER + FILE *file; + if (0 == fopen_s(&file, filename, mode)) { + return file; + } else { + return UTEST_NULL; + } +#else + return fopen(filename, mode); +#endif +} + +static UTEST_INLINE int utest_main(int argc, const char *const argv[]); +int utest_main(int argc, const char *const argv[]) { + utest_uint64_t failed = 0; + utest_uint64_t skipped = 0; + size_t index = 0; + size_t *failed_testcases = UTEST_NULL; + size_t failed_testcases_length = 0; + size_t *skipped_testcases = UTEST_NULL; + size_t skipped_testcases_length = 0; + const char *filter = UTEST_NULL; + utest_uint64_t ran_tests = 0; + int enable_mixed_units = 0; + int random_order = 0; + utest_uint32_t seed = 0; + + enum colours { RESET, GREEN, RED, YELLOW }; + + const int use_colours = UTEST_COLOUR_OUTPUT(); + const char *colours[] = {"\033[0m", "\033[32m", "\033[31m", "\033[33m"}; + + if (!use_colours) { + for (index = 0; index < sizeof colours / sizeof colours[0]; index++) { + colours[index] = ""; + } + } + /* loop through all arguments looking for our options */ + for (index = 1; index < UTEST_CAST(size_t, argc); index++) { + /* Informational switches */ + const char help_str[] = "--help"; + const char list_str[] = "--list-tests"; + /* Test config switches */ + const char filter_str[] = "--filter="; + const char output_str[] = "--output="; + const char enable_mixed_units_str[] = "--enable-mixed-units"; + const char random_order_str[] = "--random-order"; + const char random_order_with_seed_str[] = "--random-order="; + + if (0 == UTEST_STRNCMP(argv[index], help_str, strlen(help_str))) { + printf("utest.h - the single file unit testing solution for C/C++!\n" + "Command line Options:\n" + " --help Show this message and exit.\n" + " --filter=<filter> Filter the test cases to run (EG. " + "MyTest*.a would run MyTestCase.a but not MyTestCase.b).\n" + " --list-tests List testnames, one per line. Output " + "names can be passed to --filter.\n"); + printf(" --output=<output> Output an xunit XML file to the file " + "specified in <output>.\n" + " --enable-mixed-units Enable the per-test output to contain " + "mixed units (s/ms/us/ns).\n" + " --random-order[=<seed>] Randomize the order that the tests are " + "ran in. If the optional <seed> argument is not provided, then a " + "random starting seed is used.\n"); + goto cleanup; + } else if (0 == + UTEST_STRNCMP(argv[index], filter_str, strlen(filter_str))) { + /* user wants to filter what test cases run! */ + filter = argv[index] + strlen(filter_str); + } else if (0 == + UTEST_STRNCMP(argv[index], output_str, strlen(output_str))) { + utest_state.output = utest_fopen(argv[index] + strlen(output_str), "w+"); + } else if (0 == UTEST_STRNCMP(argv[index], list_str, strlen(list_str))) { + for (index = 0; index < utest_state.tests_length; index++) { + UTEST_PRINTF("%s\n", utest_state.tests[index].name); + } + /* when printing the test list, don't actually run the tests */ + return 0; + } else if (0 == UTEST_STRNCMP(argv[index], enable_mixed_units_str, + strlen(enable_mixed_units_str))) { + enable_mixed_units = 1; + } else if (0 == UTEST_STRNCMP(argv[index], random_order_with_seed_str, + strlen(random_order_with_seed_str))) { + seed = + UTEST_CAST(utest_uint32_t, + strtoul(argv[index] + strlen(random_order_with_seed_str), + UTEST_NULL, 10)); + random_order = 1; + } else if (0 == UTEST_STRNCMP(argv[index], random_order_str, + strlen(random_order_str))) { + const utest_int64_t ns = utest_ns(); + + // Some really poor pseudo-random using the current time. I do this + // because I really want to avoid using C's rand() because that'd mean our + // random would be affected by any srand() usage by the user (which I + // don't want). + seed = UTEST_CAST(utest_uint32_t, ns >> 32) * 31 + + UTEST_CAST(utest_uint32_t, ns & 0xffffffff); + random_order = 1; + } + } + + if (random_order) { + // Use Fisher-Yates with the Durstenfield's version to randomly re-order the + // tests. + for (index = utest_state.tests_length; index > 1; index--) { + // For the random order we'll use PCG. + const utest_uint32_t state = seed; + const utest_uint32_t word = + ((state >> ((state >> 28u) + 4u)) ^ state) * 277803737u; + const utest_uint32_t next = + ((word >> 22u) ^ word) % UTEST_CAST(utest_uint32_t, index); + + // Swap the randomly chosen element into the last location. + const struct utest_test_state_s copy = utest_state.tests[index - 1]; + utest_state.tests[index - 1] = utest_state.tests[next]; + utest_state.tests[next] = copy; + + // Move the seed onwards. + seed = seed * 747796405u + 2891336453u; + } + } + + for (index = 0; index < utest_state.tests_length; index++) { + if (utest_should_filter_test(filter, utest_state.tests[index].name)) { + continue; + } + + ran_tests++; + } + + printf("%s[==========]%s Running %" UTEST_PRIu64 " test cases.\n", + colours[GREEN], colours[RESET], UTEST_CAST(utest_uint64_t, ran_tests)); + + if (utest_state.output) { + fprintf(utest_state.output, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); + fprintf(utest_state.output, + "<testsuites tests=\"%" UTEST_PRIu64 "\" name=\"All\">\n", + UTEST_CAST(utest_uint64_t, ran_tests)); + fprintf(utest_state.output, + "<testsuite name=\"Tests\" tests=\"%" UTEST_PRIu64 "\">\n", + UTEST_CAST(utest_uint64_t, ran_tests)); + } + + for (index = 0; index < utest_state.tests_length; index++) { + int result = UTEST_TEST_PASSED; + utest_int64_t ns = 0; + + if (utest_should_filter_test(filter, utest_state.tests[index].name)) { + continue; + } + + printf("%s[ RUN ]%s %s\n", colours[GREEN], colours[RESET], + utest_state.tests[index].name); + + if (utest_state.output) { + fprintf(utest_state.output, "<testcase name=\"%s\">", + utest_state.tests[index].name); + } + + ns = utest_ns(); + errno = 0; +#if defined(UTEST_HAS_EXCEPTIONS) + UTEST_SURPRESS_WARNING_BEGIN + try { + utest_state.tests[index].func(&result, utest_state.tests[index].index); + } catch (const std::exception &err) { + printf(" Exception : %s\n", err.what()); + result = UTEST_TEST_FAILURE; + } catch (...) { + printf(" Exception : Unknown\n"); + result = UTEST_TEST_FAILURE; + } + UTEST_SURPRESS_WARNING_END +#else + utest_state.tests[index].func(&result, utest_state.tests[index].index); +#endif + ns = utest_ns() - ns; + + if (utest_state.output) { + fprintf(utest_state.output, "</testcase>\n"); + } + + // Record the failing test. + if (UTEST_TEST_FAILURE == result) { + const size_t failed_testcase_index = failed_testcases_length++; + failed_testcases = UTEST_PTR_CAST( + size_t *, utest_realloc(UTEST_PTR_CAST(void *, failed_testcases), + sizeof(size_t) * failed_testcases_length)); + if (UTEST_NULL != failed_testcases) { + failed_testcases[failed_testcase_index] = index; + } + failed++; + } else if (UTEST_TEST_SKIPPED == result) { + const size_t skipped_testcase_index = skipped_testcases_length++; + skipped_testcases = UTEST_PTR_CAST( + size_t *, utest_realloc(UTEST_PTR_CAST(void *, skipped_testcases), + sizeof(size_t) * skipped_testcases_length)); + if (UTEST_NULL != skipped_testcases) { + skipped_testcases[skipped_testcase_index] = index; + } + skipped++; + } + + { + const char *const units[] = {"ns", "us", "ms", "s", UTEST_NULL}; + unsigned int unit_index = 0; + utest_int64_t time = ns; + + if (enable_mixed_units) { + for (unit_index = 0; UTEST_NULL != units[unit_index]; unit_index++) { + if (10000 > time) { + break; + } + + time /= 1000; + } + } + + if (UTEST_TEST_FAILURE == result) { + printf("%s[ FAILED ]%s %s (%" UTEST_PRId64 "%s)\n", colours[RED], + colours[RESET], utest_state.tests[index].name, time, + units[unit_index]); + } else if (UTEST_TEST_SKIPPED == result) { + printf("%s[ SKIPPED ]%s %s (%" UTEST_PRId64 "%s)\n", colours[YELLOW], + colours[RESET], utest_state.tests[index].name, time, + units[unit_index]); + } else { + printf("%s[ OK ]%s %s (%" UTEST_PRId64 "%s)\n", colours[GREEN], + colours[RESET], utest_state.tests[index].name, time, + units[unit_index]); + } + } + } + + printf("%s[==========]%s %" UTEST_PRIu64 " test cases ran.\n", colours[GREEN], + colours[RESET], ran_tests); + printf("%s[ PASSED ]%s %" UTEST_PRIu64 " tests.\n", colours[GREEN], + colours[RESET], ran_tests - failed - skipped); + + if (0 != skipped) { + printf("%s[ SKIPPED ]%s %" UTEST_PRIu64 " tests, listed below:\n", + colours[YELLOW], colours[RESET], skipped); + for (index = 0; index < skipped_testcases_length; index++) { + printf("%s[ SKIPPED ]%s %s\n", colours[YELLOW], colours[RESET], + utest_state.tests[skipped_testcases[index]].name); + } + } + + if (0 != failed) { + printf("%s[ FAILED ]%s %" UTEST_PRIu64 " tests, listed below:\n", + colours[RED], colours[RESET], failed); + for (index = 0; index < failed_testcases_length; index++) { + printf("%s[ FAILED ]%s %s\n", colours[RED], colours[RESET], + utest_state.tests[failed_testcases[index]].name); + } + } + + if (utest_state.output) { + fprintf(utest_state.output, "</testsuite>\n</testsuites>\n"); + } + +cleanup: + for (index = 0; index < utest_state.tests_length; index++) { + free(UTEST_PTR_CAST(void *, utest_state.tests[index].name)); + } + + free(UTEST_PTR_CAST(void *, skipped_testcases)); + free(UTEST_PTR_CAST(void *, failed_testcases)); + free(UTEST_PTR_CAST(void *, utest_state.tests)); + + if (utest_state.output) { + fclose(utest_state.output); + } + + return UTEST_CAST(int, failed); +} + +#if defined(__clang__) +#if __has_warning("-Wunsafe-buffer-usage") +#pragma clang diagnostic pop +#endif +#endif + +/* + we need, in exactly one source file, define the global struct that will hold + the data we need to run utest. This macro allows the user to declare the + data without having to use the UTEST_MAIN macro, thus allowing them to write + their own main() function. +*/ +#define UTEST_STATE() struct utest_state_s utest_state = {0, 0, 0} + +/* + define a main() function to call into utest.h and start executing tests! A + user can optionally not use this macro, and instead define their own main() + function and manually call utest_main. The user must, in exactly one source + file, use the UTEST_STATE macro to declare a global struct variable that + utest requires. +*/ +#define UTEST_MAIN() \ + UTEST_STATE(); \ + int main(int argc, const char *const argv[]) { \ + return utest_main(argc, argv); \ + } + +#endif /* SHEREDOM_UTEST_H_INCLUDED */ diff --git a/utils_test.cc b/utils_test.cc @@ -1,29 +0,0 @@ -extern "C" { -#include "utils.h" -} - -#include <limits.h> -#include <string.h> - -#include "gtest/gtest.h" - -TEST(Utils, CanConcatenatePaths) { - char out[PATH_MAX]; - const char* returned = path_concat(out, sizeof(out), "p1", "p2"); - EXPECT_EQ(strcmp(out, "p1/p2"), 0); - EXPECT_EQ(strcmp(returned, "p1/p2"), 0); -} - -TEST(Utils, CanConcatenatePathsFirstEmpty) { - char out[PATH_MAX]; - const char* returned = path_concat(out, sizeof(out), "", "p2"); - EXPECT_EQ(strcmp(out, "p2"), 0); - EXPECT_EQ(strcmp(returned, "p2"), 0); -} - -TEST(Utils, CanConcatenatePathsSecondEmpty) { - char out[PATH_MAX]; - const char* returned = path_concat(out, sizeof(out), "p1", ""); - EXPECT_EQ(strcmp(out, "p1"), 0); - EXPECT_EQ(strcmp(returned, "p1"), 0); -} diff --git a/utils_tests.c b/utils_tests.c @@ -0,0 +1,27 @@ +#include "utils.h" + +#include <limits.h> +#include <string.h> + +#include "utest.h" + +UTEST(path_concat, CanConcatenatePaths) { + char out[PATH_MAX]; + const char* returned = path_concat(out, sizeof(out), "p1", "p2"); + EXPECT_STREQ("p1/p2", out); + EXPECT_STREQ("p1/p2", returned); +} + +UTEST(path_concat, CanConcatenatePathsFirstEmpty) { + char out[PATH_MAX]; + const char* returned = path_concat(out, sizeof(out), "", "p2"); + EXPECT_STREQ("p2", out); + EXPECT_STREQ("p2", returned); +} + +UTEST(path_concat, CanConcatenatePathsSecondEmpty) { + char out[PATH_MAX]; + const char* returned = path_concat(out, sizeof(out), "p1", ""); + EXPECT_STREQ("p1", out); + EXPECT_STREQ("p1", returned); +}