From 2fff5ec118dc8e704b8d477f7fddc3beb89de712 Mon Sep 17 00:00:00 2001 From: Christian Speckner Date: Thu, 11 Jul 2024 00:57:16 +0200 Subject: [PATCH] Pull in gtest for unit tests. --- .gitignore | 2 ++ Makefile | 53 +++++++++++++++++++++++++++++++- common.rules | 2 ++ configure | 23 ++++++++++++++ src/emucore/elf/EncodingTest.cxx | 20 ++++++++++++ src/emucore/elf/module.mk | 6 +++- 6 files changed, 104 insertions(+), 2 deletions(-) create mode 100644 src/emucore/elf/EncodingTest.cxx diff --git a/.gitignore b/.gitignore index 219830d89..32fc51160 100644 --- a/.gitignore +++ b/.gitignore @@ -10,7 +10,9 @@ src/**/*.tlog out out.pgo out.pgen +out.test stella +stella-test stella-pgo stella-pgo-generate *.diff diff --git a/Makefile b/Makefile index 4469c7584..a5654dfe2 100644 --- a/Makefile +++ b/Makefile @@ -47,17 +47,38 @@ else CFLAGS:= -O2 endif +ifndef CXXFLAGS_TEST + CXXFLAGS_TEST := $(CXXFLAGS) +endif + +ifndef CFLAGS_TEST + CFLAGS_TEST := $(CFLAGS) +endif + +ifndef LDFLAGS_TEST + LDFLAGS_TEST := $(LDFLAGS) +endif + CXXFLAGS+= -Wall -Wextra -Wno-unused-parameter CFLAGS+= -Wall -Wextra -Wno-unused-parameter +CXXFLAGS_TEST+= -Wall -Wextra -Wno-unused-parameter +CFLAGS_TEST+= -Wall -Wextra -Wno-unused-parameter + ifdef HAVE_GCC CXXFLAGS+= -Wno-multichar -Wunused -Woverloaded-virtual -Wnon-virtual-dtor -std=c++20 CFLAGS+= -Wno-multichar -Wunused + + CXXFLAGS_TEST+= -Wno-multichar -Wunused -Woverloaded-virtual -Wnon-virtual-dtor -std=c++20 + CFLAGS_TEST+= -Wno-multichar -Wunused endif ifdef HAVE_CLANG CXXFLAGS+= -Wno-multichar -Wunused -Woverloaded-virtual -Wnon-virtual-dtor -std=c++20 CFLAGS+= -Wno-multichar -Wunused + + CXXFLAGS_TEST+= -Wno-multichar -Wunused -Woverloaded-virtual -Wnon-virtual-dtor -std=c++20 + CFLAGS_TEST+= -Wno-multichar -Wunused endif ifdef CLANG_WARNINGS @@ -70,6 +91,9 @@ ifdef CLANG_WARNINGS CXXFLAGS+= $(EXTRA_WARN) CFLAGS+= $(EXTRA_WARN) + + CXXFLAGS_TEST+= $(EXTRA_WARN) + CFLAGS_TEST+= $(EXTRA_WARN) endif ifdef PROFILE @@ -108,12 +132,14 @@ ifdef STELLA_BUILD_ROOT else OBJECT_ROOT := out endif +OBJECT_ROOT_TEST := out.test OBJECT_ROOT_PROFILE_GENERERATE := out.pgen OBJECT_ROOT_PROFILE_USE := out.pgo EXECUTABLE := stella$(EXEEXT) EXECUTABLE_PROFILE_GENERATE := stella-pgo-generate$(EXEEXT) EXECUTABLE_PROFILE_USE := stella-pgo$(EXEEXT) +EXECUTABLE_TEST := stella-test$(EXEEXT) PROFILE_DIR = $(CURDIR)/test/roms/profile PROFILE_OUT = $(PROFILE_DIR)/out @@ -154,6 +180,9 @@ all: $(EXECUTABLE) pgo: $(EXECUTABLE_PROFILE_USE) +test: $(EXECUTABLE_TEST) + ./$(EXECUTABLE_TEST) + ###################################################################### # Various minor settings ###################################################################### @@ -195,6 +224,7 @@ DEPDIRS = $(addsuffix /$(DEPDIR),$(MODULE_DIRS)) DEPFILES = OBJ=$(addprefix $(OBJECT_ROOT)/,$(OBJS)) +OBJ_TEST=$(addprefix $(OBJECT_ROOT_TEST)/,$(OBJS_TEST)) OBJ_PROFILE_GENERATE=$(addprefix $(OBJECT_ROOT_PROFILE_GENERERATE)/,$(OBJS)) OBJ_PROFILE_USE=$(addprefix $(OBJECT_ROOT_PROFILE_USE)/,$(OBJS)) @@ -202,6 +232,9 @@ OBJ_PROFILE_USE=$(addprefix $(OBJECT_ROOT_PROFILE_USE)/,$(OBJS)) $(EXECUTABLE): $(OBJ) $(LD) $(LDFLAGS) $(PRE_OBJS_FLAGS) $+ $(POST_OBJS_FLAGS) $(LIBS) $(PROF) -o $@ +$(EXECUTABLE_TEST): $(OBJ_TEST) + $(LD) $(LDFLAGS_TEST) $(PRE_OBJS_FLAGS) $+ $(POST_OBJS_FLAGS) $(LIBS) -lgtest -lgtest_main -o $@ + $(EXECUTABLE_PROFILE_GENERATE): $(OBJ_PROFILE_GENERATE) $(LD) $(LDFLAGS_PROFILE_GENERATE) $(PRE_OBJS_FLAGS) $+ $(POST_OBJS_FLAGS) $(LIBS) $(PROF) -o $@ @@ -216,7 +249,7 @@ clean: -$(RM) -fr \ $(OBJECT_ROOT) $(OBJECT_ROOT_PROFILE_GENERERATE) $(OBJECT_ROOT_PROFILE_USE) \ $(EXECUTABLE) $(EXECUTABLE_PROFILE_GENERATE) $(EXECUTABLE_PROFILE_USE) \ - $(PROFILE_OUT) $(PROFILE_STAMP) + $(OBJECT_ROOT_TEST) $(PROFILE_OUT) $(PROFILE_STAMP) .PHONY: all clean dist distclean @@ -244,6 +277,16 @@ $(OBJECT_ROOT)/%.o: %.cxx $(merge_dep) $(OBJECT_ROOT)/%.o: %.c + $(create_dir) + $(CC) $(CXX_UPDATE_DEP_FLAG) $(CFLAGS_TEST) $(CPPFLAGS) -c $(<) -o $@ + $(merge_dep) + +$(OBJECT_ROOT_TEST)/%.o: %.cxx + $(create_dir) + $(CXX) $(CXX_UPDATE_DEP_FLAG) $(CXXFLAGS_TEST) $(CPPFLAGS) -c $(<) -o $@ + $(merge_dep) + +$(OBJECT_ROOT_TEST)/%.o: %.c $(create_dir) $(CC) $(CXX_UPDATE_DEP_FLAG) $(CFLAGS) $(CPPFLAGS) -c $(<) -o $@ $(merge_dep) @@ -281,6 +324,14 @@ $(OBJECT_ROOT)/%.o: %.c $(create_dir) $(CC) $(CXX_UPDATE_DEP_FLAG) $(CFLAGS) $(CPPFLAGS) -c $(<) -o $@ +$(OBJECT_ROOT_TEST)/%.o: %.cxx + $(create_dir) + $(CXX) $(CXX_UPDATE_DEP_FLAG) $(CXXFLAGS_TEST) $(CPPFLAGS) -c $(<) -o $@ + +$(OBJECT_ROOT_TEST)/%.o: %.c + $(create_dir) + $(CC) $(CXX_UPDATE_DEP_FLAG) $(CFLAGS_TEST) $(CPPFLAGS) -c $(<) -o $@ + $(OBJECT_ROOT_PROFILE_GENERERATE)/%.o: %.cxx $(create_dir) $(CXX) $(CXX_UPDATE_DEP_FLAG) $(CXXFLAGS_PROFILE_GENERATE) $(CPPFLAGS) -c $(<) -o $@ diff --git a/common.rules b/common.rules index be66f8b3a..2838c0e5d 100644 --- a/common.rules +++ b/common.rules @@ -4,7 +4,9 @@ # contains the module name, a trick we use so we can keep multiple different # module object lists, one for each module. MODULE_OBJS-$(MODULE) := $(MODULE_OBJS) +MODULE_TEST_OBJS-$(MODULE) := $(MODULE_TEST_OBJS) # If not building as a plugin, add the object files to the main OBJS list #OBJS += $(MODULE_LIB-$(MODULE)) OBJS += $(MODULE_OBJS) +OBJS_TEST := $(MODULE_TEST_OBJS) diff --git a/configure b/configure index e35ae9aca..8a10609d8 100755 --- a/configure +++ b/configure @@ -13,6 +13,16 @@ # use environment vars if set CXXFLAGS="$CXXFLAGS $CPPFLAGS" +if test -n "$CXXFLAGS_TEST"; then + CXXFLAGS_TEST="$CXXFLAGS_TEST $CPPFLAGS" +else + CXXFLAGS_TEST="$CXXFLAGS" +fi + +if test -z "$LDFLAGS_TEST"; then + LDFLAGS_TEST="$LDFLAGS" +fi + # default option behaviour yes/no _build_gui=yes _build_windowed=yes @@ -225,6 +235,7 @@ Optional Libraries: --with-sdl-prefix=DIR Prefix where the sdl2-config script is installed (optional) --with-libpng-prefix=DIR Prefix where libpng is installed (optional) --with-zlib-prefix=DIR Prefix where zlib is installed (optional) + --with-gtest-prefix=DIR Prefix where googletest is installed (optional) Some influential environment variables: LDFLAGS linker flags, e.g. -L if you have libraries in a @@ -233,6 +244,8 @@ Some influential environment variables: CXXFLAGS C++ compiler flags CPPFLAGS C++ preprocessor flags, e.g. -I if you have headers in a nonstandard directory + LDFLAGS_TEST linker flags for unit tests + CXXFLAGS_TEST C++ compiler flags for unit tests EOF exit 0 @@ -280,6 +293,11 @@ for ac_option in $@; do _prefix=`echo $ac_option | cut -d '=' -f 2` ZLIB_CFLAGS="-I$_prefix/include" ZLIB_LIBS="-L$_prefix/lib" + ;; + --with-gtest-prefix=*) + _prefix=`echo $ac_option | cut -d '=' -f 2` + CXXFLAGS_TEST="$CXXFLAGS_TEST -I$_prefix/include" + LDFLAGS_TEST="$LDFLAGS_TEST -L$_prefix/lib" ;; --host=*) _host=`echo $ac_option | cut -d '=' -f 2` @@ -841,6 +859,9 @@ case $_host_os in INCLUDES="$INCLUDES -I$SRC_OS/unix" if test "$have_clang" == yes; then CXXFLAGS="$CXXFLAGS -Wno-poison-system-directories" + if test -n "$CXXFLAGS_TEST"; then + CXXFLAGS_TEST="$CXXFLAGS_TEST -Wno-poison-system-directories" + fi fi _libsqlite3=no ;; @@ -954,6 +975,7 @@ cat > config.mak << EOF CC := $CC CXX := $CXX CXXFLAGS := $CXXFLAGS +CXXFLAGS_TEST := $CXXFLAGS_TEST LD := $LD LIBS += $LIBS RANLIB := $_ranlib @@ -991,6 +1013,7 @@ INCLUDES += $INCLUDES OBJS += $OBJS DEFINES += $DEFINES LDFLAGS += $LDFLAGS +LDFLAGS_TEST += $LDFLAGS_TEST $_config_mk_data EOF diff --git a/src/emucore/elf/EncodingTest.cxx b/src/emucore/elf/EncodingTest.cxx new file mode 100644 index 000000000..a24cec16e --- /dev/null +++ b/src/emucore/elf/EncodingTest.cxx @@ -0,0 +1,20 @@ +//============================================================================ +// +// SSSS tt lll lll +// SS SS tt ll ll +// SS tttttt eeee ll ll aaaa +// SSSS tt ee ee ll ll aa +// SS tt eeeeee ll ll aaaaa -- "An Atari 2600 VCS Emulator" +// SS SS tt ee ll ll aa aa +// SSSS ttt eeeee llll llll aaaaa +// +// Copyright (c) 1995-2024 by Bradford W. Mott, Stephen Anthony +// and the Stella Team +// +// See the file "License.txt" for information on usage and redistribution of +// this file, and for a DISCLAIMER OF ALL WARRANTIES. +//============================================================================ + +#include + +TEST(HelloWorld, Passes) {} diff --git a/src/emucore/elf/module.mk b/src/emucore/elf/module.mk index 671451272..4e4bb0637 100644 --- a/src/emucore/elf/module.mk +++ b/src/emucore/elf/module.mk @@ -5,8 +5,12 @@ MODULE_OBJS = \ src/emucore/elf/ElfLinker.o \ src/emucore/elf/ElfUtil.o +MODULE_TEST_OBJS = \ + src/emucore/elf/ElfUtil.o \ + src/emucore/elf/EncodingTest.o + MODULE_DIRS += \ - src/emucore/elf + src/emucore/elf/EncodingTest.o # Include common rules include $(srcdir)/common.rules