Merge pull request 'Add testing' (#20) from init-testing into main

Reviewed-on: #20
Closes: #18
Refs: #18, #17
main
rkcf 2023-02-28 16:36:39 +00:00
commit bf90991e9c
6 changed files with 150 additions and 64 deletions

40
CMakeLists.txt Normal file
View File

@ -0,0 +1,40 @@
cmake_minimum_required(VERSION 3.14)
project(wtpp)
set(CMAKE_CXX_STANDARD 17)
add_library (wave src/wave.cpp)
add_library (wavetable src/wavetable.cpp)
add_library (common src/common.cpp)
add_library (generators src/generators.cpp)
add_executable(wtpp src/wtpp.cpp)
target_link_libraries(wtpp wave wavetable common generators)
include(FetchContent)
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/95d8c7ac4389b38b197bc7521dffb62508653eb5.zip
)
FetchContent_MakeAvailable(googletest)
enable_testing()
add_executable(wtpp_test tests/wtpp_test.cpp)
add_executable(wave_test tests/wave_test.cpp)
add_executable(wavetable_test tests/wavetable_test.cpp)
add_executable(generators_test tests/generators_test.cpp)
target_link_libraries(wtpp_test GTest::gtest_main)
target_link_libraries(wave_test wave GTest::gtest_main)
target_link_libraries(wavetable_test wave wavetable GTest::gtest_main)
target_link_libraries(generators_test generators GTest::gtest_main)
include(GoogleTest)
gtest_discover_tests(wtpp_test)
gtest_discover_tests(wave_test)
gtest_discover_tests(wavetable_test)
gtest_discover_tests(generators_test)

View File

@ -1,17 +1,20 @@
PROJ = wtpp
CC = /usr/bin/clang++
GDB = /usr/bin/gdb
OLEVEL = -O2
CFLAGS = -Wall -std=c++17 $(OLEVEL) -o $(PROJ)
OBJECTS = src/wtpp.cpp src/wave.cpp src/wavetable.cpp src/generators.cpp src/common.cpp
build:
$(CC) $(CFLAGS) $(OBJECTS)
build-release:
cmake --build build
clean:
rm wtpp
rm build/wtpp
debug: build
test: build
cd build && ctest --output-on-failure
debug:
$(CC) -g $(CFLAGS) $(OBJECTS)
$(GDB) $(PROJ)
web-debug:
$(CC) -g $(CFLAGS) $(OBJECTS)
gdbgui $(PROJ)

View File

@ -1,60 +1,6 @@
#include <array>
#include <cstdint>
#include <type_traits>
#include <math.h>
#include "ini.h"
#include "common.h"
#include "wave.h"
#include "generators.h"
#include "wavetable.h"
#include <iostream>
int main()
{
clog("Generating wave cycle lookup tables");
Wave sin_wave, squ_wave, saw_wave, tri_wave, isaw_wave;
// Calculate fundamental wave values
sample_table_t sin_vals = gen_sin();
sample_table_t squ_vals = gen_squ();
sample_table_t saw_vals = gen_saw();
sample_table_t tri_vals = gen_tri();
sample_table_t isaw_vals = gen_isaw();
// Set wave lookup table values
sin_wave.setTable(sin_vals);
squ_wave.setTable(squ_vals);
saw_wave.setTable(saw_vals);
tri_wave.setTable(tri_vals);
isaw_wave.setTable(isaw_vals);
clog("Printing head of sine table");
sin_wave.print_head();
clog("Printing head of square table");
squ_wave.print_head();
clog("Printing head of saw table");
saw_wave.print_head();
clog("Printing head of triangle table");
tri_wave.print_head();
clog("Printing head of inverse saw table");
isaw_wave.print_head();
Wavetable wt_01{"Fundamental Waves"};
clog(wt_01.name());
// Add fundamental waves to wt_01
std::array<Wave, 5> fundies = {sin_wave, squ_wave, saw_wave, tri_wave, isaw_wave};
for (uint8_t i = 0; i < 5; i++){
wt_01.setWave(i, fundies[i]);
}
// Write sine wave table to file
sin_wave.export_csv("data/sine.csv");
// Write full Wavetable data to file
wt_01.export_csv("data");
return 0;
}

35
tests/generators_test.cpp Normal file
View File

@ -0,0 +1,35 @@
#include "../src/generators.h"
#include "gtest/gtest.h"
TEST(Generator, SineWave)
{
sample_table_t t = gen_sin();
// Starts at 0
EXPECT_EQ(t[0], 0);
}
TEST(Generator, SquareWave) {
sample_table_t t = gen_squ();
// Starts at 1
EXPECT_EQ(t[0], 1);
// Ends at -1
EXPECT_EQ(t[DEFAULT_N_SAMPLES - 1], -1);
}
TEST(Generator, SawWave) {
sample_table_t t = gen_saw();
// Starts at -1
EXPECT_EQ(t[0], -1);
}
TEST(Generator, TriangleWave) {
sample_table_t t = gen_tri();
// Starts at -1
EXPECT_EQ(t[0], -1);
}
TEST(Generator, InverseSawWave) {
sample_table_t t = gen_isaw();
// Starts at 1
EXPECT_EQ(t[0], 1);
}

31
tests/wave_test.cpp Normal file
View File

@ -0,0 +1,31 @@
#include "../src/wave.h"
#include "gtest/gtest.h"
TEST(Wave, DefaultConstructor)
{
Wave w1, w2;
// Check that ids are correct
EXPECT_EQ(w1.getId(), 0);
EXPECT_EQ(w2.getId(), 1);
// Sample table should be 0 initialized
EXPECT_EQ(w1.getVal(0), 0);
}
TEST(Wave, CopyConstructor) {
Wave w1;
Wave w2(w1);
// Check id is correct
EXPECT_EQ(w2.getId(), 0);
// Sample table should be 0 initialized
EXPECT_EQ(w2.getVal(0), 0);
}
TEST(Wave, CopyAssignment)
{
Wave w1;
Wave w2 = w1;
// Check id is correct
EXPECT_EQ(w2.getId(), 0);
// Sample table should be 0 initialized
EXPECT_EQ(w2.getVal(0), 0);
}

31
tests/wavetable_test.cpp Normal file
View File

@ -0,0 +1,31 @@
#include "../src/wavetable.h"
#include "gtest/gtest.h"
TEST(Wavetable, DefaultConstructor)
{
Wavetable w1("PPG"), w2("!PPG");
// Check ids are correct
EXPECT_EQ(w1.getId(), 0);
EXPECT_EQ(w2.getId(), 1);
// Check name is correct
// std::string name = w1.name();
EXPECT_EQ(w1.name(), "0: PPG");
}
TEST(Wavetable, CopyConstructor) {
Wavetable w1("PPG");
Wavetable w2(w1);
// Check id is correct
EXPECT_EQ(w2.getId(), 0);
// Sample name is correct
EXPECT_EQ(w2.name(), "0: PPG");
}
TEST(Wavetable, CopyAssignment) {
Wavetable w1("PPG");
Wavetable w2 = w1;
// Check id is correct
EXPECT_EQ(w2.getId(), 0);
// Check name is correct
EXPECT_EQ(w2.name(), "0: PPG");
}