Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Build SeBa
name: Build and test SeBa

on:
push:
Expand All @@ -19,3 +19,7 @@ jobs:
run: |
cd dstar
make
- name: Build and run unit tests
run: |
git submodule update --init --recursive
make test
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Makefile.inc
dstar/Makefile.inc
**/*.o
**/*.a
starev
dstar/Normalisation
dstar/SeBa
**/*.data
build/
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "third_party/googletest"]
path = third_party/googletest
url = https://github.com/google/googletest.git
52 changes: 51 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ DIRS = node std sstar dstar rdc

all: lib $(EXE)

test:
test-compiler:
echo $(CXX)
echo `which $(CXX)`

Expand All @@ -22,3 +22,53 @@ lib: Makefile.inc
clean: Makefile.inc
@for d in $(DIRS) ; do echo '\nmake' $@ in $$d; make -C $$d $@ ; done
/bin/rm -f *~ $(EXE) Makefile.inc

# --- Test support ---
TEST_CXXFLAGS = $(CXXFLAGS) -std=c++23
TESTDIR = tests
BUILDDIR = build
TESTBIN = $(BUILDDIR)/run_tests
TESTSRC = $(wildcard $(TESTDIR)/*.cpp)
TESTOBJ = $(patsubst $(TESTDIR)/%.cpp,$(BUILDDIR)/%.o,$(TESTSRC))

GTEST_DIR = third_party/googletest/googletest
GTEST_SRC = $(GTEST_DIR)/src/gtest-all.cc
GTEST_OBJ = $(BUILDDIR)/gtest-all.o
GTEST_INC = -I$(GTEST_DIR)/include -I$(GTEST_DIR)
GTEST_MAIN_SRC = $(GTEST_DIR)/src/gtest_main.cc
GTEST_MAIN_OBJ = $(BUILDDIR)/gtest_main.o

# Create build directory
$(BUILDDIR):
mkdir -p $(BUILDDIR)

# Compile Google Test source
$(GTEST_OBJ): $(GTEST_SRC) | $(BUILDDIR)
$(CXX) $(TEST_CXXFLAGS) $(GTEST_INC) -c $< -o $@

# Compile test sources
$(BUILDDIR)/%.o: $(TESTDIR)/%.cpp | $(BUILDDIR)
$(CXX) $(TEST_CXXFLAGS) $(GTEST_INC) -I./include -c $< -o $@

# Compile Google Test main
$(GTEST_MAIN_OBJ): $(GTEST_MAIN_SRC) | $(BUILDDIR)
$(CXX) $(TEST_CXXFLAGS) $(GTEST_INC) -c $< -o $@

# Link final test binary
$(TESTBIN): $(TESTOBJ) $(GTEST_OBJ) $(GTEST_MAIN_OBJ)
$(CXX) $(TEST_CXXFLAGS) $(GTEST_INC) -I./include $^ \
$(LDLIBS) -lpthread -o $@

# Run tests
test: $(TESTBIN)
@echo "Running unit tests..."
@./$(TESTBIN)

# Run specific tests matching a pattern
test_%: $(TESTBIN)
@echo "Running tests matching '$*'..."
@./$(TESTBIN) --gtest_filter=*${*}*

# Optional cleanup
clean-tests:
/bin/rm -rf $(BUILDDIR)
78 changes: 78 additions & 0 deletions tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# Tests

## Overview

Unit tests live in the `tests/` directory.
They use **GoogleTest (gtest)**, a lightweight C++ testing framework.
Tests are built and executed by running:

``` sh
make test
```


This compiles all test files (`tests/*.cpp`), links them against project libraries and gtest, and runs them automatically.
Passing tests will print `[ PASSED ]` lines in the terminal.

## Getting GoogleTest Source

This project vendors GoogleTest to make the test build self-contained.
Ensure the submodules are cloned so that the GoogleTest code is included:

```sh
git submodule update --init --recursive
```

This will populate `third_party/googletest/`, which contains the GoogleTest source built automatically when running `make test`.

## Cleaning Up

To remove test binaries:

``` sh
make clean-tests
```

## Writing a Test

Each test file is a small C++ program that includes both gtest and your project
headers.

You want to define test cases using the `TEST` macro provided by gtest, and
ensure that your functions behave as expected using assertions like `EXPECT_EQ`, `ASSERT_TRUE`, etc.

Example:

``` cpp
#include "util_math.h
#include <gtest/gtest.h>

TEST(Basic, Add) { EXPECT_EQ(add(2, 2), 4); }
```

## Running Tests

Run all tests:

``` sh
make test
```

Run a specific test suite or test case:

``` sh
make test_[pattern]
```

E.g., run all tests marked `UtilMath`, run:

``` sh
make test_UtilMath

```

Run a specific test case within a suite:

``` sh
make test_UtilMath.Twiddles
```
5 changes: 5 additions & 0 deletions tests/test_basic.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include <gtest/gtest.h>

int add(int a, int b) { return a + b; }

TEST(Basic, Add) { EXPECT_EQ(add(2, 2), 4); }
30 changes: 30 additions & 0 deletions tests/test_util_math.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include "util_math.h"
#include <gtest/gtest.h>

// basic numerical tolerance for comparisons
static constexpr real EPS = 1e-12;

TEST(UtilMath, Twiddles) {
EXPECT_TRUE(twiddles(1.0, 1.0 + 1e-13, EPS));
EXPECT_FALSE(twiddles(1.0, 1.1, EPS));
}

TEST(UtilMath, Sign) {
EXPECT_EQ(sign(3.5), 1);
EXPECT_EQ(sign(-2.1), -1);
EXPECT_EQ(sign(0.0), 0);
}

TEST(UtilMath, AdjustNumberToPower) {
// result should not exceed max_step_size
real result = adjust_number_to_power(5.5, 4.0);
EXPECT_LE(result, 4.0);
}

TEST(UtilMath, AsinhAcoshConsistency) {
real x = 1.5;
real s = sinh(x);
real c = cosh(x);
EXPECT_NEAR(asinh(s), x, 1e-12);
EXPECT_NEAR(acosh(c), x, 1e-12);
}
1 change: 1 addition & 0 deletions third_party/googletest
Submodule googletest added at b2b907