#=====================================================================
# Tests for librts.  Builds 32-bit ELF binaries that link against the
# installed librts.a, using librts's own I/O (no libc) via _main.
#=====================================================================

PREFIX = $(HOME)/compiladores/root
INC    = $(PREFIX)/usr/include
LIB    = $(PREFIX)/usr/lib

CC       = cc
CFLAGS   = -Og -ggdb -Wall -Wextra -pedantic -nostdlib -fno-builtin \
           -m32 -msse2 -mfpmath=sse -std=c23 -I$(INC)
LDFLAGS  = -m elf_i386 -L$(LIB)
LDLIBS   = -lrts

TESTS = test_ternary test_balanced3 test_posit3 test_takum3

.PHONY: all run clean

all: $(TESTS)

# Each test: compile .c -> .o, link with librts (no libc) -> binary
$(TESTS): %: %.o
	ld $(LDFLAGS) -o $@ $< $(LDLIBS)

%.o: %.c test.h
	$(CC) $(CFLAGS) -c $< -o $@

run: all
	@ok=0; fail=0; \
	for t in $(TESTS); do \
	  echo "=========================================="; \
	  ./$$t; rc=$$?; \
	  if [ $$rc -eq 0 ]; then ok=$$((ok+1)); \
	  else fail=$$((fail+1)); echo "!! $$t FAILED (exit $$rc)"; fi; \
	done; \
	echo "=========================================="; \
	echo "suites: $$ok passed, $$fail failed"; \
	[ $$fail -eq 0 ]

clean:
	rm -f $(TESTS) *.o
