# Test suites and machine fixture matrix. Run from the repository root.
# JOBS controls workers in the integration driver.
JOBS         ?= 8
TEST_HARTS   := 1 2 8
SMP_TESTS    := $(addprefix smp-test-,$(TEST_HARTS))
CYCLES_TESTS := $(addprefix cycles-test-,$(TEST_HARTS))
TESTS        := \
	seed-test \
	std-test \
	fmt-test \
	host-test \
	bin-test \
	module-test \
	package-test \
	native-test \
	shared-test \
	sync-test \
	verify-test \
	kernel-test \
	kernel-boot-test \
	trap-test loader-test \
	dispatch-test \
	smp-test \
	termination-test \
	runtime-test \
	bootstrap-test \
	scheduling-test \
	mmio-test \
	cycles-test

test: $(TESTS)

# Hosted test tools share one compiler recipe.
# Runner binary: the lowering IL checker.
TEST_TOOL_SRC := test/runner.rad test/native/build.rad \
    test/shared/build.rad test/sync/build.rad test/loader/hostile.rad \
    test/verify/check.rad test/host/child.rad
TEST_TOOLS := $(TEST_TOOL_SRC:.rad=.rv64)

$(TEST_TOOLS): %.rv64: %.rad $(STD_LIB) $(STD_MANIFEST) $(RAD_BIN) test/Makefile
	@$(RADIANCE) -name tool $< -o $@

# The integration driver runs the checks that start other programs: the
# compiler, the test tools and the emulator. Each of its commands works in
# bin/test/NAME.
INTEGRATION      := test/integration.rv64
INTEGRATION_SRC  := $(sort $(wildcard test/integration/*.rad))
INTEGRATION_FIXTURES := $(wildcard test/integration/fixtures/*/*)
INTEGRATION_OPTS := -compiler $(RAD_BIN) -jobs $(JOBS)
# Run the driver command $(1) with the arguments $(2).
integration = $(HOSTED) $(INTEGRATION) $(1) $(INTEGRATION_OPTS) $(2)

$(INTEGRATION): $(INTEGRATION_FIXTURES) $(INTEGRATION_SRC) $(STD_LIB) $(STD_MANIFEST) \
    test/integration/integration.manifest $(RAD_BIN) test/Makefile
	@$(RADIANCE) test/integration -o $@

seed-test: $(INTEGRATION)
	@$(call integration,seed)

# std::process against the emulator.
host-test: $(INTEGRATION) test/host/child.rv64 $(BIN_DIR)/native.fixture.rv64
	@$(call integration,host)

package-test: $(INTEGRATION)
	@$(call integration,packages)

module-test: $(RAD_BIN) test/compiler/discovery.rv64 test/compiler/metadata.rv64
	@$(HOSTED) test/compiler/discovery.rv64
	@$(HOSTED) test/compiler/metadata.rv64

# Standard Library Tests

STD_LIB_TEST := lib/std.test.rv64

STD_TEST_FIXTURES := $(wildcard lib/std/lang/manifest/tests/fixtures/*.manifest \
    lib/std/lang/resolver/tests/fixtures/*/*.rad)

std-test: $(STD_LIB_TEST)
	@$(HOSTED) $(STD_LIB_TEST)

$(STD_LIB_TEST): $(STD_TEST_FIXTURES) $(STD_TEST_LIB) $(STD_MANIFEST) $(RAD_BIN) test/Makefile
	@$(RADIANCE) -test lib/std -o $@

clean-std-test:
	@rm -f lib/std.test.rv64 lib/std.test.rv64.debug \
	    lib/std.test.rv64.s lib/std.test.rv64.o

# The source formatter keeps the AST of every standard library and compiler
# source, and formats its own output to itself.
FMT_TEST_SRC := $(shell find lib compiler test/integration test/host -name '*.rad' | sort)

fmt-test: $(RAD_BIN) $(INTEGRATION)
	@$(call integration,fmt,$(FMT_TEST_SRC))

kernel-test: $(KERNEL_TEST) $(INTEGRATION) $(RAD_BIN)
	@$(HOSTED) $(KERNEL_TEST)
	@$(call integration,slots)

kernel-boot-test: $(BIN_DIR)/kernel.rv64 $(INTEGRATION)
	@$(call integration,boot)

# Native image execution through the configured machine-capable emulator.
native-test: $(BIN_DIR)/native.fixture.rv64
	@$(RAD_EMULATOR) -machine -max-steps=1000000 -run $<

$(BIN_DIR)/native.fixture.rv64: test/native/build.rv64 test/Makefile
	@$(HOSTED) $< $@

# Shared package execution
shared-test: test/shared/build.rv64 $(INTEGRATION) $(RAD_BIN)
	@$(call integration,shared)

# Kernel synchronization machine tests
sync-test: test/sync/build.rv64 $(INTEGRATION)
	@$(call integration,sync)

# Binary Tests

# Only tests with `//! returns:` are compiled to binaries and executed.
BIN_TEST_SRC   := $(shell grep -l '^//! returns:' $(wildcard test/tests/*.rad test/tests/*.ras \
    test/tests/*/test.rad))
BIN_TEST_BIN   := $(addsuffix .rv64,$(basename $(BIN_TEST_SRC)))

bin-test: test/runner.rv64 $(INTEGRATION) $(BIN_TEST_BIN)
	@$(call integration,bin)

# Compile each executable test to a binary. A test is one module, or a package
# directory `test/tests/NAME/` with `test.manifest` and the root `test.rad`
# when it has submodules or assembly. A test links the packages it imports.
# Fixtures that use std must rebuild when a library source changes.
# A package fixture also depends on its own module and assembly inputs.
.SECONDEXPANSION:
$(filter %/test.rv64,$(BIN_TEST_BIN)): test/tests/%/test.rv64: test/tests/%/test.manifest \
    $$(wildcard test/tests/$$*/*.rad test/tests/$$*/*.ras)

test/tests/%.rv64: test/tests/%.rad $(RAD_BIN) $(INTEGRATION) test/Makefile
	@$(call integration,compile,$< $@)

test/tests/%.rv64: test/tests/%.ras test/runner.rv64 test/Makefile
	@$(HOSTED) test/runner.rv64 assemble $< $@

clean-bin-test:
	@rm -f $(TEST_TOOLS) $(INTEGRATION) $(BIN_TEST_BIN) \
	    test/compiler/discovery.rv64 test/compiler/metadata.rv64 \
	    $(wildcard test/tests/*.rv64.debug test/tests/*.rv64.s) \
	    $(TEST_TOOLS:.rv64=.rv64.debug) $(TEST_TOOLS:.rv64=.rv64.s) \
	    $(TEST_TOOLS:.rv64=.rv64.o)

# Integer trap contexts and nested machine interrupts.
trap-test: test/sync/build.rv64 $(INTEGRATION)
	@$(call integration,trap)

# Runtime package compilation and execution through the production kernel.
# Generator of the hostile packages that the loader must reject.
loader-test: $(BIN_DIR)/kernel.build.rv64 test/loader/hostile.rv64 $(INTEGRATION) $(RAD_BIN)
	@$(call integration,loader)

# Machine fixtures build one kernel image each; every hart count then runs
# the same image as its own target.
FIXTURE_DEPS := $(RAD_BIN) $(BIN_DIR)/kernel.build.rv64 \
    $(INTEGRATION) test/kernel/fixture.rad \
    test/kernel.rad test/Makefile kernel/mmio.rad $(KERNEL_MODULES) $(KERNEL_ASM)
DISPATCH_IMAGES := $(BIN_DIR)/dispatch.rv64 $(BIN_DIR)/smp.rv64 \
    $(BIN_DIR)/termination.rv64 $(BIN_DIR)/mmio.rv64

# Dispatch fixtures are built from test/NAME by the integration driver.
$(DISPATCH_IMAGES): $(BIN_DIR)/%.rv64: test/%/spin.rad test/%/machine.ras \
    test/%/kernel/dispatchcheck.rad $(FIXTURE_DEPS)
	@$(call integration,dispatch-build,$* $@)

$(BIN_DIR)/termination.rv64: test/termination/cases.rad
$(BIN_DIR)/mmio.rv64: test/mmio/cases.rad
$(BIN_DIR)/smp.rv64: test/smp/kernel/invariants.rad test/smp/cases.rad

# Run the machine fixture image $(2) on $(3) harts with the extra emulator
# flags $(4), and report $(1) when it exits with status zero.
MACHINE_FLAGS := -machine -memory-size=262144 -max-steps=70000000000
fixture = $(RAD_EMULATOR) $(MACHINE_FLAGS) -harts=$(3) $(4) -run $(2) \
    && echo "$(1): $(3)-hart execution passed"

# Native preemption across user code, idle, and retained machine work.
dispatch-test: $(BIN_DIR)/dispatch.rv64
	@$(call fixture,dispatch,$<,1)

# Concurrent user contexts and metadata transactions on every online hart.
smp-test: $(SMP_TESTS)
$(SMP_TESTS): smp-test-%: $(BIN_DIR)/smp.rv64
	@$(call fixture,smp,$<,$*,-irq=7 -irq-at=1)

# User exit, abort, and CPU faults preserve the surviving parent's execution.
termination-test: $(BIN_DIR)/termination.rv64
	@$(call fixture,termination,$<,1)

# Scheduled compilation, cancellation, retry, and shared executable publication.
runtime-test: $(BIN_DIR)/kernel.build.rv64 $(INTEGRATION) $(RAD_BIN)
	@$(call integration,runtime)

# Root lifetime and resource authority across the boot-package handoff.
bootstrap-test: $(BIN_DIR)/kernel.build.rv64 $(INTEGRATION) $(RAD_BIN)
	@$(call integration,bootstrap)

# Fixture root and scheduler packages execute authority changes in user mode.
scheduling-test: $(BIN_DIR)/kernel.rv64 $(INTEGRATION) $(RAD_BIN)
	@$(call integration,scheduling)

# Checked user-mode register access, including rejection before device effects.
mmio-test: $(BIN_DIR)/mmio.rv64
	@$(call fixture,mmio,$<,1)

# Dependency, controller, and worker loading under every hart count.
$(BIN_DIR)/cycles.rv64: test/cycles/spin.rad \
    test/cycles/job.rad test/cycles/support.rad test/cycles/machine.ras \
    test/cycles/kernel/dispatchcheck.rad test/smp/kernel/invariants.rad \
    $(FIXTURE_DEPS)
	@$(call integration,cycles-build,$@)

cycles-test: $(CYCLES_TESTS)
$(CYCLES_TESTS): cycles-test-%: $(BIN_DIR)/cycles.rv64
	@$(call fixture,cycles,$<,$*)

# The verifier accepts or rejects the binary RIL of each fixture as
# test/verify/expected says.
verify-test: test/verify/check.rv64 $(INTEGRATION) $(RAD_BIN)
	@$(call integration,verify)

# Exercise the production test discovery module with bounded storage. The
# compiler modules have no package directory, so these builds use package flags.
test/compiler/discovery.rv64: test/compiler/discovery.rad compiler/radiance/testgen.rad \
    test/compiler/checks.manifest $(STD_LIB) $(RAD_BIN) test/Makefile
	@$(RADIANCE) $(STD) -pkg testgen -manifest seed/dependencies/std \
	    -mod compiler/radiance/testgen.rad \
	    -pkg checks -mod test/compiler/discovery.rad -entry checks -o $@

# Check canonical exported metadata through the production compiler module.
test/compiler/metadata.rv64: test/compiler/metadata.rad compiler/radiance/metadata.rad \
    test/compiler/metachecks.manifest $(STD_LIB) $(RAD_BIN) test/Makefile
	@$(RADIANCE) $(STD) -pkg metadata -manifest seed/dependencies/std \
	    -mod compiler/radiance/metadata.rad -pkg metachecks -mod test/compiler/metadata.rad \
	    -entry metachecks -o $@

.PHONY: test $(TESTS) $(SMP_TESTS) $(CYCLES_TESTS) clean-std-test clean-bin-test
