# Tests the hardware-limits mode of LispBM's conf-get, (conf-get 'name 2), in
# lispBM/lispif_vesc_extensions.c.
#
# The limits block and its lim_max/lim_min helpers are cut out of the real
# source rather than copied, so they cannot drift from what the firmware
# compiles. Built twice: with every HW_LIM_* a board can define, each a
# different number so that a limit read from the wrong macro shows, and with
# none, where the fallbacks apply.
#
# The extraction is checked: if a marker moves, the build fails rather than
# testing an empty file.

TOP = ../..
SRC = $(TOP)/lispBM/lispif_vesc_extensions.c

CFLAGS = -std=gnu99 -O1 -g -Wall -Wextra -DNO_STM32 -I. -I$(TOP)

all: test test-none

limits.inc: $(SRC) Makefile
	@awk '/^static inline float lim_m(ax|in)\(float min, float max\)/ { print } \
		/^\t\tif \(defaultcfg == 2\) \{$$/ { on = 1; \
			print "static void hw_limits(mc_configuration *mcconf) {"; next } \
		on && /^\t\t}$$/ { print "}"; exit } \
		on { print }' $(SRC) > $@
	@test "$$(grep -c '^static inline float lim_m' $@)" = 2 || { echo \
		"limits.inc lacks lim_max/lim_min: a marker in $(SRC) changed"; \
		rm -f $@; exit 1; }
	@tail -1 $@ | grep -q '^}$$' && grep -q 'HW_LIM_TEMP_FET' $@ || { echo \
		"limits.inc looks truncated: the defaultcfg == 2 block in $(SRC)"\
		" changed, so the extraction needs updating"; rm -f $@; exit 1; }

test: main.c limits.inc
	gcc $(CFLAGS) -DTEST_ALL_LIMITS -o $@ main.c

test-none: main.c limits.inc
	gcc $(CFLAGS) -o $@ main.c

run: test test-none
	@echo "--- every HW_LIM_* defined ---"
	./test
	@echo "--- none defined ---"
	./test-none

clean:
	rm -f test test-none limits.inc

.PHONY: all run clean
