diff --git a/.github/workflows/unit_test.yaml b/.github/workflows/unit_test.yaml new file mode 100644 index 00000000..86e43099 --- /dev/null +++ b/.github/workflows/unit_test.yaml @@ -0,0 +1,55 @@ +on: + workflow_dispatch: + pull_request: + push: + +jobs: + run_unit_test: + name: Run unit tests on ${{ matrix.os }} + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + include: + - {os: ubuntu-latest, arch: x86_64} + - {os: ubuntu-24.04-arm, arch: aarch64} + - {os: windows-latest, arch: AMD64} + - {os: macos-14, arch: arm64} + - {os: macos-13, arch: x86_64,} + + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + submodules: recursive + + - name: Set up Python 3.11 + uses: actions/setup-python@v5 + with: + python-version: 3.11 + + - name: Build xgrammar from source + run: | + echo "set(XGRAMMAR_BUILD_CXX_TESTS ON)" >> cmake/config.cmake + python -m pip install --upgrade pip + pip install -e . + + - name: Run C++ tests + run: | + ctest --test-dir build + + - name: Run Python tests + env: + HF_TOKEN: ${{ secrets.HF_TOKEN }} + if: env.HF_TOKEN != '' + run: | + pip install pytest + pytest + + - name: Run Python tests without HF_TOKEN + env: + HF_TOKEN: ${{ secrets.HF_TOKEN }} + if: env.HF_TOKEN == '' + run: | + pip install pytest + pytest -m "not hf_token_required" diff --git a/cpp/support/thread_pool.h b/cpp/support/thread_pool.h index b48fc5ca..0146f571 100644 --- a/cpp/support/thread_pool.h +++ b/cpp/support/thread_pool.h @@ -12,6 +12,7 @@ #include #include #include +#include #include #include "logging.h" @@ -67,9 +68,8 @@ class ThreadPool { * \note Tasks are executed in FIFO order but may complete in any order. */ template - auto Submit(F&& f, Args&&... args) - -> std::shared_future::type> { - using return_type = typename std::result_of::type; + auto Submit(F&& f, Args&&... args) -> std::shared_future> { + using return_type = std::invoke_result_t; // Package the task with its arguments into a shared pointer auto task = std::make_shared>( diff --git a/tests/cpp/test_thread_safe_cache.cc b/tests/cpp/test_thread_safe_cache.cc index 8413358f..bfefda5b 100644 --- a/tests/cpp/test_thread_safe_cache.cc +++ b/tests/cpp/test_thread_safe_cache.cc @@ -34,7 +34,9 @@ using namespace std::chrono_literals; TEST(XGrammarParallelTest, CacheEfficiency) { auto cache = ThreadSafeCache{[](const std::string&) { std::this_thread::sleep_for(1s); // simulate a slow operation - return MockGrammar{.uuid = counter++, .padding = {}}; + MockGrammar g{}; + g.uuid = counter++; + return g; }}; auto futures = std::vector>{};