name: Concrete CPU - Tests

on:
  workflow_call:
  workflow_dispatch:

concurrency:
  group: concrete_cpu_test-${{ github.ref }}
  cancel-in-progress: ${{ github.ref != 'refs/heads/main' }}

env:
  CARGO_TERM_COLOR: always
jobs:
  tests-linux:
    runs-on: ubuntu-20.04
    env:
      RUSTFLAGS: -D warnings
    steps:
      - uses: actions/checkout@v3

      - name: Rust install
        uses: actions-rs/toolchain@v1
        with:
          toolchain: nightly
          override: true
          components: rustfmt, clippy

      - name: Download cargo cache
        uses: Swatinem/rust-cache@v2

      - name: Formatting
        run: |
          cd backends/concrete-cpu/implementation
          cargo fmt --check

      - name: Remove header to force regeneration
        run: |
          cd backends/concrete-cpu/implementation
          rm include/concrete-cpu.h

      - name: Build
        run: |
          cd backends/concrete-cpu/implementation
          cargo build --all-targets

      - name: Check regenerated header is unchanged
        run: |
          cd backends/concrete-cpu/implementation
          git diff --exit-code

      - name: Lint
        run: |
          cd backends/concrete-cpu/implementation
          cargo +stable clippy --all-targets

      - name: Tests
        run: |
          cd backends/concrete-cpu/implementation
          cargo test --no-fail-fast --all-targets --features=nightly

      - name: Zig install
        run: |
          sudo snap install zig --classic --beta

      - name: Zig test
        run: |
          cd backends/concrete-cpu/implementation/test
          make test

  tests-mac_x86:
    runs-on: macos-11
    env:
      RUSTFLAGS: -D warnings
    steps:
      - uses: actions/checkout@v3

      - name: Rust install
        uses: actions-rs/toolchain@v1
        with:
          toolchain: nightly
          override: true

      - name: Download cargo cache
        uses: Swatinem/rust-cache@v2

      - name: Tests
        run: |
          cd backends/concrete-cpu/implementation
          cargo test --no-fail-fast --all-targets

      - name: Zig install
        run: |
          brew install zig

      - name: Zig test
        run: |
          cd backends/concrete-cpu/implementation/test
          make test


  tests-mac-m1:
    runs-on: "aws-mac2-metal"
    env:
      RUSTFLAGS: -D warnings
    steps:
      - uses: actions/checkout@v3

      - name: Rust install
        uses: actions-rs/toolchain@v1
        with:
          toolchain: nightly
          override: true

      - name: Download cargo cache
        uses: Swatinem/rust-cache@v2

      - name: Tests
        run: |
          cd backends/concrete-cpu/implementation
          cargo test --no-fail-fast --all-targets

      - name: Zig install
        run: |
          brew install zig

      - name: Zig test
        run: |
          cd backends/concrete-cpu/implementation/test
          make test
