WIP on restoring unit tests on cppunit

This commit is contained in:
Michaël Lemaire 2013-11-10 17:03:07 +01:00 committed by Michael Lemaire
parent 5c2bc4b31e
commit 8051719f9f
27 changed files with 618 additions and 169 deletions

View file

@ -33,7 +33,7 @@ release:
+make BUILDMODE=release all +make BUILDMODE=release all
tests:build tests:build
LD_LIBRARY_PATH=$(LIBRARY_PATH) CK_DEFAULT_TIMEOUT=30 ${BUILDPATH}/testing/paysages-tests LD_LIBRARY_PATH=$(LIBRARY_PATH) ${BUILDPATH}/tests/paysages-tests
run_cli:build run_cli:build
LD_LIBRARY_PATH=$(LIBRARY_PATH) ${RUNNER} ${BUILDPATH}/controlling/paysages-cli LD_LIBRARY_PATH=$(LIBRARY_PATH) ${RUNNER} ${BUILDPATH}/controlling/paysages-cli

View file

@ -10,4 +10,4 @@ SUBDIRS = \
editing \ editing \
controlling controlling
#unix:SUBDIRS += testing tests unix:SUBDIRS += tests

View file

@ -5,14 +5,8 @@
#include "System.h" #include "System.h"
int tests_cpu_count; int tests_cpu_count;
extern void test_euclid_case(Suite* s);
extern void test_camera_case(Suite* s);
extern void test_clouds_case(Suite* s);
extern void test_render_case(Suite* s); extern void test_render_case(Suite* s);
extern void test_noise_case(Suite* s);
extern void test_terrain_painting_case(Suite* s); extern void test_terrain_painting_case(Suite* s);
extern void test_bruneton_case(Suite* s);
extern void test_pack_case(Suite* s);
extern void test_zone_case(Suite* s); extern void test_zone_case(Suite* s);
int main(int argc, char** argv) int main(int argc, char** argv)
@ -25,14 +19,8 @@ int main(int argc, char** argv)
tests_cpu_count = systemGetCoreCount(); tests_cpu_count = systemGetCoreCount();
/* TODO Find a way to automate this */ /* TODO Find a way to automate this */
test_euclid_case(s);
test_camera_case(s);
test_clouds_case(s);
test_render_case(s); test_render_case(s);
test_noise_case(s);
test_terrain_painting_case(s); test_terrain_painting_case(s);
test_bruneton_case(s);
test_pack_case(s);
test_zone_case(s); test_zone_case(s);
SRunner *sr = srunner_create(s); SRunner *sr = srunner_create(s);

View file

@ -1,78 +0,0 @@
#include "testing/common.h"
#include "rendering/noise.h"
START_TEST(test_noise_range)
{
NoiseGenerator* noise;
double minvalue, maxvalue;
noise = noiseCreateGenerator();
noiseAddLevelSimple(noise, 0.1, -1.0, 1.0);
noiseAddLevelSimple(noise, 0.2, -0.5, 0.2);
noiseAddLevelSimple(noise, 0.4, -0.3, 1.2);
noiseValidate(noise);
noiseGetRange(noise, &minvalue, &maxvalue);
ck_assert_double_eq(minvalue, -1.8);
ck_assert_double_eq(maxvalue, 2.4);
}
END_TEST
START_TEST(test_noise_normalize)
{
int x;
NoiseGenerator* noise;
noise = noiseCreateGenerator();
/* Symmetric case */
noiseAddLevelsSimple(noise, 10, 1.0, -4.0, 4.0, 0.5);
noiseValidate(noise);
noiseNormalizeAmplitude(noise, -1.0, 1.0, 0);
for (x = 0; x < 1000; x++)
{
double value = noiseGet1DTotal(noise, 0.01 * (double)x);
ck_assert_double_in_range(value, -1.0, 1.0);
}
/* Target center differs from current center */
noiseClearLevels(noise);
noiseAddLevelsSimple(noise, 10, 1.0, -5.0, 5.0, 0.5);
noiseValidate(noise);
noiseNormalizeAmplitude(noise, 0.0, 1.0, 0);
for (x = 0; x < 1000; x++)
{
double value = noiseGet1DTotal(noise, 0.01 * (double)x);
ck_assert_double_in_range(value, 0.0, 1.0);
}
/* Asymmetric range */
noiseClearLevels(noise);
noiseAddLevelsSimple(noise, 10, 1.0, 0.0, 10.0, 0.0);
noiseValidate(noise);
noiseNormalizeAmplitude(noise, 0.0, 1.0, 0);
for (x = 0; x < 1000; x++)
{
double value = noiseGet1DTotal(noise, 0.01 * (double)x);
ck_assert_double_in_range(value, 0.0, 1.0);
}
/* Complex asymmetric case */
noiseClearLevels(noise);
noiseAddLevelsSimple(noise, 3, 1.0, -2.0, 8.0, 0.3);
noiseValidate(noise);
noiseNormalizeAmplitude(noise, -2.0, 4.0, 0.0);
for (x = 0; x < 1000; x++)
{
double value = noiseGet1DTotal(noise, 0.01 * (double)x);
ck_assert_double_in_range(value, -2.0, 4.0);
}
noiseDeleteGenerator(noise);
}
END_TEST
TEST_CASE(noise, test_noise_range, test_noise_normalize)

View file

@ -15,13 +15,6 @@ INCLUDEPATH += $$PWD/..
SOURCES += main.c \ SOURCES += main.c \
test_terrain_painting.c \ test_terrain_painting.c \
test_render.c \
test_pack.c \
test_noise.c \
test_euclid.c \
test_clouds.c \
test_camera.c \
test_bruneton.c \
test_zone.c test_zone.c
HEADERS += \ HEADERS += \

View file

@ -7,6 +7,40 @@
#include <cppunit/extensions/HelperMacros.h> #include <cppunit/extensions/HelperMacros.h>
using namespace CppUnit; using namespace CppUnit;
// Transitional macros from C libcheck
static inline int _double_equals(double x, double y)
{
return fabs(x - y) < 0.00000000001;
}
static inline int _double_not_equals(double x, double y)
{
return fabs(x - y) >= 0.00000000001;
}
static inline int _double_greater(double x, double y)
{
return _double_not_equals(x, y) && (x > y);
}
static inline int _double_greater_or_equal(double x, double y)
{
return _double_equals(x, y) || (x >= y);
}
static inline int _double_less(double x, double y)
{
return _double_not_equals(x, y) && (x < y);
}
static inline int _double_less_or_equal(double x, double y)
{
return _double_equals(x, y) || (x <= y);
}
#define _ck_assert_double(F, X, O, Y) CPPUNIT_ASSERT(F(X, Y), "Assertion '"#X#O#Y"' failed: "#X"=%.12f, "#Y"=%.12f", X, Y)
#define ck_assert_double_eq(X, Y) _ck_assert_double(_double_equals, X, ==, Y)
#define ck_assert_double_ne(X, Y) _ck_assert_double(_double_not_equals, X, !=, Y)
#define ck_assert_double_gt(X, Y) _ck_assert_double(_double_greater, X, >, Y)
#define ck_assert_double_lt(X, Y) _ck_assert_double(_double_less, X, <, Y)
#define ck_assert_double_gte(X, Y) _ck_assert_double(_double_greater_or_equal, X, >=, Y)
#define ck_assert_double_lte(X, Y) _ck_assert_double(_double_less_or_equal, X, <=, Y)
class BaseTestCase: public TestFixture class BaseTestCase: public TestFixture
{ {
public: public:

View file

@ -1,6 +1,7 @@
#include "testing/common.h" #include "Bruneton_Test.h"
CPPUNIT_TEST_SUITE_REGISTRATION(Bruneton_Test);
#include "System.h" #include "System.h"
#include "rendering/Scenery.h"
#define OUTPUT_WIDTH 400 #define OUTPUT_WIDTH 400
#define OUTPUT_HEIGHT 300 #define OUTPUT_HEIGHT 300
@ -10,7 +11,7 @@ static Color _postProcessFragment(Renderer* renderer, Vector3 location, void* da
return renderer->atmosphere->applyAerialPerspective(renderer, location, COLOR_BLACK).final; return renderer->atmosphere->applyAerialPerspective(renderer, location, COLOR_BLACK).final;
} }
START_TEST(testBrunetonAerialPerspective) Bruneton_Test::testBrunetonAerialPerspective()
{ {
Renderer* renderer = sceneryCreateStandardRenderer(); Renderer* renderer = sceneryCreateStandardRenderer();
renderer->render_width = 800; renderer->render_width = 800;
@ -37,9 +38,8 @@ START_TEST(testBrunetonAerialPerspective)
rendererDelete(renderer); rendererDelete(renderer);
} }
END_TEST
START_TEST(testBrunetonAerialPerspective1) Bruneton_Test::testBrunetonAerialPerspective1()
{ {
AtmosphereDefinition* atmo = AtmosphereDefinitionClass.create(); AtmosphereDefinition* atmo = AtmosphereDefinitionClass.create();
sceneryGetAtmosphere(atmo); sceneryGetAtmosphere(atmo);
@ -73,9 +73,3 @@ START_TEST(testBrunetonAerialPerspective1)
rendererDelete(renderer); rendererDelete(renderer);
} }
END_TEST
TEST_CASE(bruneton,
testBrunetonAerialPerspective,
testBrunetonAerialPerspective1)

18
src/tests/Bruneton_Test.h Normal file
View file

@ -0,0 +1,18 @@
#ifndef BRUNETON_TEST_H
#define BRUNETON_TEST_H
#include "BaseTestCase.h"
class Bruneton_Test: public BaseTestCase
{
public:
CPPUNIT_TEST_SUITE(Bruneton_Test);
CPPUNIT_TEST(testBrunetonAerialPerspective);
CPPUNIT_TEST(testBrunetonAerialPerspective1);
CPPUNIT_TEST_SUITE_END();
void testBrunetonAerialPerspective();
void testBrunetonAerialPerspective1();
};
#endif // BRUNETON_TEST_H

View file

@ -1,8 +1,9 @@
#include "testing/common.h" #include "Camera_Test.h"
CPPUNIT_TEST_SUITE_REGISTRATION(Camera_Test);
#include "rendering/camera.h" #include "camera.h"
START_TEST(test_camera_definition) void Camera_Test::test_camera_definition()
{ {
CameraDefinition* cam; CameraDefinition* cam;
cam = cameraCreateDefinition(); cam = cameraCreateDefinition();
@ -15,9 +16,8 @@ START_TEST(test_camera_definition)
cameraDeleteDefinition(cam); cameraDeleteDefinition(cam);
} }
END_TEST
START_TEST(test_camera_projection) void Camera_Test::test_camera_projection()
{ {
CameraDefinition* cam; CameraDefinition* cam;
cam = cameraCreateDefinition(); cam = cameraCreateDefinition();
@ -35,9 +35,8 @@ START_TEST(test_camera_projection)
cameraDeleteDefinition(cam); cameraDeleteDefinition(cam);
} }
END_TEST
START_TEST(test_camera_depth) void Camera_Test::test_camera_depth()
{ {
CameraDefinition* cam; CameraDefinition* cam;
cam = cameraCreateDefinition(); cam = cameraCreateDefinition();
@ -55,10 +54,3 @@ START_TEST(test_camera_depth)
cameraDeleteDefinition(cam); cameraDeleteDefinition(cam);
} }
END_TEST
TEST_CASE(camera,
test_camera_definition,
test_camera_projection,
test_camera_depth)

20
src/tests/Camera_Test.h Normal file
View file

@ -0,0 +1,20 @@
#ifndef CAMERA_TEST_H
#define CAMERA_TEST_H
#include "BaseTestCase.h"
class Camera_Test: public BaseTestCase
{
public:
CPPUNIT_TEST_SUITE(Camera_Test);
CPPUNIT_TEST(test_camera_definition);
CPPUNIT_TEST(test_camera_projection);
CPPUNIT_TEST(test_camera_depth);
CPPUNIT_TEST_SUITE_END();
void test_camera_definition();
void test_camera_projection();
void test_camera_depth();
};
#endif // CAMERA_TEST_H

View file

@ -1,4 +1,5 @@
#include "testing/common.h" #include "Clouds_Test.h"
CPPUNIT_TEST_SUITE_REGISTRATION(Clouds_Test);
#include "rendering/renderer.h" #include "rendering/renderer.h"
#include "rendering/tools.h" #include "rendering/tools.h"
@ -6,7 +7,7 @@
#include "rendering/clouds/clo_density.h" #include "rendering/clouds/clo_density.h"
#include "rendering/clouds/clo_walking.h" #include "rendering/clouds/clo_walking.h"
START_TEST(test_clouds_density) Clouds_Test::test_clouds_density()
{ {
/* Setup */ /* Setup */
double x, y, z; double x, y, z;
@ -85,7 +86,7 @@ START_TEST(test_clouds_density)
} }
END_TEST END_TEST
START_TEST(test_clouds_walking_boundaries) Clouds_Test::test_clouds_walking_boundaries()
{ {
Vector3 start, end; Vector3 start, end;
int result; int result;
@ -190,7 +191,6 @@ START_TEST(test_clouds_walking_boundaries)
cloudsGetLayerType().callback_delete(layer); cloudsGetLayerType().callback_delete(layer);
} }
END_TEST
static double _getLayerDensitySinX(Renderer* renderer, CloudsLayerDefinition* layer, Vector3 location) static double _getLayerDensitySinX(Renderer* renderer, CloudsLayerDefinition* layer, Vector3 location)
{ {
@ -209,7 +209,7 @@ static double _getEdgeDensitySquared(Renderer* renderer, CloudsLayerDefinition*
} }
START_TEST(test_clouds_walking) Clouds_Test::test_clouds_walking()
{ {
/* Init */ /* Init */
CloudsLayerDefinition* layer; CloudsLayerDefinition* layer;
@ -421,9 +421,8 @@ START_TEST(test_clouds_walking)
cloudsGetLayerType().callback_delete(layer); cloudsGetLayerType().callback_delete(layer);
rendererDelete(renderer); rendererDelete(renderer);
} }
END_TEST
START_TEST(test_clouds_walking_local) Clouds_Test::test_clouds_walking_local()
{ {
/* Init */ /* Init */
CloudsLayerDefinition* layer; CloudsLayerDefinition* layer;
@ -465,10 +464,3 @@ START_TEST(test_clouds_walking_local)
ck_assert_double_eq(segment->end.global_density, 0.809016994375); ck_assert_double_eq(segment->end.global_density, 0.809016994375);
ck_assert_double_eq(segment->end.local_density, 0.654508497187); ck_assert_double_eq(segment->end.local_density, 0.654508497187);
} }
END_TEST
TEST_CASE(clouds,
test_clouds_density,
test_clouds_walking,
test_clouds_walking_boundaries,
test_clouds_walking_local)

22
src/tests/Clouds_Test.h Normal file
View file

@ -0,0 +1,22 @@
#ifndef CLOUDS_TEST_H
#define CLOUDS_TEST_H
#include "BaseTestCase.h"
class Clouds_Test: public BaseTestCase
{
public:
CPPUNIT_TEST_SUITE(Clouds_Test);
CPPUNIT_TEST(test_clouds_density);
CPPUNIT_TEST(test_clouds_walking_boundaries);
CPPUNIT_TEST(test_clouds_walking);
CPPUNIT_TEST(test_clouds_walking_local);
CPPUNIT_TEST_SUITE_END();
void test_clouds_density();
void test_clouds_walking_boundaries();
void test_clouds_walking();
void test_clouds_walking_local();
};
#endif // CLOUDS_TEST_H

View file

@ -1,4 +1,6 @@
#include "testing/common.h" #include "Euclid_Test.h"
CPPUNIT_TEST_SUITE_REGISTRATION(Euclid_Test);
#include "rendering/tools/euclid.h" #include "rendering/tools/euclid.h"
static inline int _Vector3_cmp(Vector3 v1, Vector3 v2) static inline int _Vector3_cmp(Vector3 v1, Vector3 v2)
@ -12,7 +14,7 @@ static inline void _Vector3_str(Vector3 v, char* buffer, int length)
} }
DEFINE_COMPARE_ASSERT(Vector3, _Vector3_cmp, _Vector3_str); DEFINE_COMPARE_ASSERT(Vector3, _Vector3_cmp, _Vector3_str);
START_TEST(test_euclid_angles) Euclid_Test::test_euclid_angles()
{ {
ck_assert_double_eq(euclidGet2DAngle(0.0, 0.0), 0.0); ck_assert_double_eq(euclidGet2DAngle(0.0, 0.0), 0.0);
@ -38,9 +40,7 @@ START_TEST(test_euclid_angles)
ck_assert_double_eq(euclidGet2DAngle(-0.5, -0.5), 5.0 * M_PI_4); ck_assert_double_eq(euclidGet2DAngle(-0.5, -0.5), 5.0 * M_PI_4);
} }
END_TEST Euclid_Test::test_vectors()
START_TEST(test_vectors)
{ {
Vector3 v1, v2, v3; Vector3 v1, v2, v3;
@ -58,9 +58,7 @@ START_TEST(test_vectors)
ck_assert_generic_eq(Vector3, v3Scale(v1, -0.5), v3); ck_assert_generic_eq(Vector3, v3Scale(v1, -0.5), v3);
} }
END_TEST Euclid_Test::test_vectors_spherical()
START_TEST(test_vectors_spherical)
{ {
Vector3 v1; Vector3 v1;
VectorSpherical v2; VectorSpherical v2;
@ -128,7 +126,3 @@ START_TEST(test_vectors_spherical)
ck_assert_double_eq(v2.phi, 7.0 * M_PI_4); ck_assert_double_eq(v2.phi, 7.0 * M_PI_4);
ck_assert_double_eq(v2.theta, M_PI_2 - 0.955316618125); ck_assert_double_eq(v2.theta, M_PI_2 - 0.955316618125);
} }
END_TEST
TEST_CASE(euclid, test_euclid_angles, test_vectors, test_vectors_spherical)

21
src/tests/Euclid_Test.h Normal file
View file

@ -0,0 +1,21 @@
#ifndef EUCLID_TEST_H
#define EUCLID_TEST_H
#include "BaseTestCase.h"
class Euclid_Test: public BaseTestCase
{
public:
CPPUNIT_TEST_SUITE(Euclid_Test);
CPPUNIT_TEST(test_camera_definition);
CPPUNIT_TEST(test_camera_projection);
CPPUNIT_TEST(test_camera_depth);
CPPUNIT_TEST_SUITE_END();
void test_camera_definition();
void test_camera_projection();
void test_camera_depth();
};
#endif // EUCLID_TEST_H

View file

@ -1,6 +1,8 @@
#include "Layers_Test.h" #include "Layers_Test.h"
CPPUNIT_TEST_SUITE_REGISTRATION(Layers_Test);
#include "Layers.h" #include "Layers.h"
#include "PackStream.h"
BaseDefinition* _construc1(Layers*) BaseDefinition* _construc1(Layers*)
{ {
@ -166,5 +168,3 @@ void Layers_Test::testLegacyLayers()
delete layers1; delete layers1;
CPPUNIT_ASSERT_EQUAL(0, _legacy_instance_count); CPPUNIT_ASSERT_EQUAL(0, _legacy_instance_count);
} }
CPPUNIT_TEST_SUITE_REGISTRATION(Layers_Test);

View file

@ -0,0 +1,76 @@
#include "NoiseGenerator_Test.h"
CPPUNIT_TEST_SUITE_REGISTRATION(NoiseGenerator_Test);
#include "NoiseGenerator.h"
void NoiseGenerator_Test::test_noise_range()
{
NoiseGenerator* noise;
double minvalue, maxvalue;
noise = new NoiseGenerator();
noise->addLevelSimple(0.1, -1.0, 1.0);
noise->addLevelSimple(0.2, -0.5, 0.2);
noise->addLevelSimple(0.4, -0.3, 1.2);
noise->validate();
noise->getRange(&minvalue, &maxvalue);
ck_assert_double_eq(minvalue, -1.8);
ck_assert_double_eq(maxvalue, 2.4);
delete noise;
}
void NoiseGenerator_Test::test_noise_normalize()
{
int x;
NoiseGenerator* noise;
noise = new NoiseGenerator();
/* Symmetric case */
noise->addLevelsSimple( 10, 1.0, -4.0, 4.0, 0.5);
noise->validate();
noise->normalizeAmplitude( -1.0, 1.0, 0);
for (x = 0; x < 1000; x++)
{
double value = noise->get1DTotal( 0.01 * (double)x);
ck_assert_double_in_range(value, -1.0, 1.0);
}
/* Target center differs from current center */
noise->clearLevels();
noise->addLevelsSimple( 10, 1.0, -5.0, 5.0, 0.5);
noise->validate();
noise->normalizeAmplitude( 0.0, 1.0, 0);
for (x = 0; x < 1000; x++)
{
double value = noise->get1DTotal(0.01 * (double)x);
ck_assert_double_in_range(value, 0.0, 1.0);
}
/* Asymmetric range */
noise->clearLevels();
noise->addLevelsSimple(10, 1.0, 0.0, 10.0, 0.0);
noise->validate();
noise->normalizeAmplitude(0.0, 1.0, 0);
for (x = 0; x < 1000; x++)
{
double value = noise->get1DTotal(0.01 * (double)x);
ck_assert_double_in_range(value, 0.0, 1.0);
}
/* Complex asymmetric case */
noise->clearLevels();
noise->addLevelsSimple(3, 1.0, -2.0, 8.0, 0.3);
noise->validate();
noise->normalizeAmplitude(-2.0, 4.0, 0.0);
for (x = 0; x < 1000; x++)
{
double value = noise->get1DTotal(0.01 * (double)x);
ck_assert_double_in_range(value, -2.0, 4.0);
}
delete noise;
}

View file

@ -0,0 +1,18 @@
#ifndef NOISEGENERATOR_TEST_H
#define NOISEGENERATOR_TEST_H
#include "BaseTestCase.h"
class NoiseGenerator_Test: public BaseTestCase
{
public:
CPPUNIT_TEST_SUITE(NoiseGenerator_Test);
CPPUNIT_TEST(test_noise_range);
CPPUNIT_TEST(test_noise_normalize);
CPPUNIT_TEST_SUITE_END();
void test_noise_range();
void test_noise_normalize();
};
#endif // NOISEGENERATOR_TEST_H

View file

@ -1,8 +1,9 @@
#include "testing/common.h" #include "PackStream_Test.h"
CPPUNIT_TEST_SUITE_REGISTRATION(PackStream_Test);
#include "PackStream.h" #include "PackStream.h"
START_TEST(testPack) void PackStream_Test::testPack()
{ {
PackStream* stream; PackStream* stream;
int i; int i;
@ -47,8 +48,3 @@ START_TEST(testPack)
} }
packCloseStream(stream); packCloseStream(stream);
} }
END_TEST
TEST_CASE(pack,
testPack)

View file

@ -0,0 +1,16 @@
#ifndef PACKSTREAM_TEST_H
#define PACKSTREAM_TEST_H
#include "BaseTestCase.h"
class PackStream_Test: public BaseTestCase
{
public:
CPPUNIT_TEST_SUITE(PackStream_Test);
CPPUNIT_TEST(testPack);
CPPUNIT_TEST_SUITE_END();
void testPack();
};
#endif // PACKSTREAM_TEST_H

View file

@ -1,6 +1,7 @@
#include "testing/common.h" #include "Render_Test.h"
CPPUNIT_TEST_SUITE_REGISTRATION(Render_Test);
#include <math.h> #include <cmath>
#include "rendering/renderer.h" #include "rendering/renderer.h"
#include "rendering/tools.h" #include "rendering/tools.h"
@ -30,7 +31,7 @@ static Color _postProcessFragment(Renderer* renderer, Vector3 location, void* da
} }
} }
START_TEST(test_render_quad) Render_Test::test_render_quad()
{ {
Color col; Color col;
Renderer* renderer = rendererCreate(); Renderer* renderer = rendererCreate();
@ -66,7 +67,3 @@ START_TEST(test_render_quad)
rendererDelete(renderer); rendererDelete(renderer);
} }
END_TEST
TEST_CASE(render, test_render_quad)

16
src/tests/Render_Test.h Normal file
View file

@ -0,0 +1,16 @@
#ifndef RENDER_TEST_H
#define RENDER_TEST_H
#include "BaseTestCase.h"
class Render_Test: public BaseTestCase
{
public:
CPPUNIT_TEST_SUITE(Render_Test);
CPPUNIT_TEST(test_render_quad);
CPPUNIT_TEST_SUITE_END();
void test_render_quad();
};
#endif // RENDER_TEST_H

View file

@ -0,0 +1,229 @@
#include "TerrainPainting_Test.h"
CPPUNIT_TEST_SUITE_REGISTRATION(TerrainPainting_Test);
#include <cmath>
#include "rendering/tools.h"
#include "rendering/terrain/public.h"
/* Noise sin period is defined at 20.0 */
#define X_FACTOR (M_PI / 10.0)
static double _noise1dMock(double x)
{
return sin(x * X_FACTOR) * 0.5 + 0.5;
}
static double _noise2dMock(double x, double y)
{
UNUSED(y);
return sin(x * X_FACTOR) * 0.5 + 0.5;
}
static double _noise3dMock(double x, double y, double z)
{
UNUSED(y);
UNUSED(z);
return sin(x * X_FACTOR) * 0.5 + 0.5;
}
static TerrainDefinition* _setUpDefinition()
{
TerrainDefinition* terrain = (TerrainDefinition*)TerrainDefinitionClass.create();
terrain->height = 3.0;
terrain->scaling = 1.0;
noiseClearLevels(terrain->_height_noise);
NoiseLevel level = {1.0, 2.0, -1.0, 0.0, 0.0, 0.0};
noiseAddLevel(terrain->_height_noise, level, 0);
noiseSetCustomFunction(terrain->_height_noise, _noise1dMock, _noise2dMock, _noise3dMock);
return terrain;
}
static void _tearDownDefinition(TerrainDefinition* terrain)
{
TerrainDefinitionClass.destroy(terrain);
}
TerrainPainting_Test::test_terrain_painting_grid()
{
/* Set up */
TerrainDefinition* terrain = _setUpDefinition();
/* Test base grid */
ck_assert_double_eq(terrainGetGridHeight(terrain, 0, 0, 0), 0.0);
ck_assert_double_eq(terrainGetGridHeight(terrain, 0, 1, 0), 0.0);
ck_assert_double_eq(terrainGetGridHeight(terrain, 0, 0, 1), 0.0);
ck_assert_double_eq(terrainGetGridHeight(terrain, 1, 0, 0), sin(1.0 * X_FACTOR));
ck_assert_double_eq(terrainGetGridHeight(terrain, 2, 0, 0), sin(2.0 * X_FACTOR));
ck_assert_double_eq(terrainGetGridHeight(terrain, 3, 0, 0), sin(3.0 * X_FACTOR));
ck_assert_double_eq(terrainGetGridHeight(terrain, 4, 0, 0), sin(4.0 * X_FACTOR));
ck_assert_double_eq(terrainGetGridHeight(terrain, 5, 0, 0), 1.0);
ck_assert_double_eq(terrainGetGridHeight(terrain, 6, 0, 0), sin(4.0 * X_FACTOR));
ck_assert_double_eq(terrainGetGridHeight(terrain, -1, 0, 0), -sin(1.0 * X_FACTOR));
ck_assert_double_eq(terrainGetGridHeight(terrain, 10, 0, 0), 0.0);
ck_assert_double_eq(terrainGetGridHeight(terrain, 15, 0, 0), -1.0);
ck_assert_double_eq(terrainGetGridHeight(terrain, 20, 0, 0), 0.0);
ck_assert_double_eq(terrainGetGridHeight(terrain, -5, 0, 0), -1.0);
/* Test interpolated result */
ck_assert_double_eq(terrainGetInterpolatedHeight(terrain, 0.0, 0.0, 0, 0), 0.0);
ck_assert_double_in_range(terrainGetInterpolatedHeight(terrain, 0.5, 0.0, 0, 0), 0.1564, 0.1566);
ck_assert_double_eq(terrainGetInterpolatedHeight(terrain, 1.0, 0.0, 0, 0), sin(1.0 * X_FACTOR));
ck_assert_double_eq(terrainGetInterpolatedHeight(terrain, 0.0, 0.0, 1, 0), 0.0);
ck_assert_double_in_range(terrainGetInterpolatedHeight(terrain, 0.5, 0.0, 1, 0), 3.0 * 0.1564, 3.0 * 0.1566);
ck_assert_double_eq(terrainGetInterpolatedHeight(terrain, 1.0, 0.0, 1, 0), 3.0 * sin(1.0 * X_FACTOR));
/* Test scaling */
terrain->scaling = 2.0;
ck_assert_double_eq(terrainGetGridHeight(terrain, 0, 0, 0), 0.0);
ck_assert_double_eq(terrainGetGridHeight(terrain, 1, 0, 0), sin(1.0 * X_FACTOR));
ck_assert_double_eq(terrainGetGridHeight(terrain, 2, 0, 0), sin(2.0 * X_FACTOR));
ck_assert_double_eq(terrainGetGridHeight(terrain, 3, 0, 0), sin(3.0 * X_FACTOR));
ck_assert_double_eq(terrainGetInterpolatedHeight(terrain, 0, 0, 0, 0), 0.0);
ck_assert_double_eq(terrainGetInterpolatedHeight(terrain, 1, 0, 0, 0), sin(0.5 * X_FACTOR));
ck_assert_double_eq(terrainGetInterpolatedHeight(terrain, 2, 0, 0, 0), sin(1.0 * X_FACTOR));
ck_assert_double_eq(terrainGetInterpolatedHeight(terrain, 3, 0, 0, 0), sin(1.5 * X_FACTOR));
ck_assert_double_eq(terrainGetInterpolatedHeight(terrain, 0, 0, 1, 0), 0.0);
ck_assert_double_eq(terrainGetInterpolatedHeight(terrain, 1, 0, 1, 0), 6.0 * sin(0.5 * X_FACTOR));
ck_assert_double_eq(terrainGetInterpolatedHeight(terrain, 2, 0, 1, 0), 6.0 * sin(1.0 * X_FACTOR));
ck_assert_double_eq(terrainGetInterpolatedHeight(terrain, 3, 0, 1, 0), 6.0 * sin(1.5 * X_FACTOR));
/* Tear down */
_tearDownDefinition(terrain);
}
static void _checkBrushResultSides(TerrainDefinition* terrain, TerrainBrush* brush, double center, double midhard, double hard, double midsoft, double soft, double exter, double neg_midhard, double neg_hard, double neg_midsoft, double neg_soft, double neg_exter)
{
UNUSED(brush);
ck_assert_double_eq(terrainGetGridHeight(terrain, 0, 0, 1), center);
ck_assert_double_eq(terrainGetGridHeight(terrain, 1, 0, 1), midhard);
ck_assert_double_eq(terrainGetGridHeight(terrain, 2, 0, 1), hard);
ck_assert_double_eq(terrainGetGridHeight(terrain, 3, 0, 1), midsoft);
ck_assert_double_eq(terrainGetGridHeight(terrain, 4, 0, 1), soft);
ck_assert_double_eq(terrainGetGridHeight(terrain, 5, 0, 1), exter);
ck_assert_double_eq(terrainGetGridHeight(terrain, -1, 0, 1), neg_midhard);
ck_assert_double_eq(terrainGetGridHeight(terrain, -2, 0, 1), neg_hard);
ck_assert_double_eq(terrainGetGridHeight(terrain, -3, 0, 1), neg_midsoft);
ck_assert_double_eq(terrainGetGridHeight(terrain, -4, 0, 1), neg_soft);
ck_assert_double_eq(terrainGetGridHeight(terrain, -5, 0, 1), neg_exter);
}
static void _checkBrushResult(TerrainDefinition* terrain, TerrainBrush* brush, double center, double midhard, double hard, double midsoft, double soft, double exter, int mirror)
{
if (mirror)
{
_checkBrushResultSides(terrain, brush, center, midhard, hard, midsoft, soft, exter, -midhard, -hard, -midsoft, -soft, -exter);
}
else
{
_checkBrushResultSides(terrain, brush, center, midhard, hard, midsoft, soft, exter, midhard, hard, midsoft, soft, exter);
}
}
TerrainPainting_Test::test_terrain_painting_brush_flatten()
{
/* Set up */
TerrainDefinition* terrain = _setUpDefinition();
TerrainBrush brush = {0.0, 0.0, 2.0, 2.0, 4.0};
terrain->height = 1.0;
terrain->scaling = 1.0;
noiseForceValue(terrain->_height_noise, 0.0);
/* Test flattening center at 0.5 */
_checkBrushResult(terrain, &brush, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0);
terrainBrushFlatten(terrain->height_map, &brush, 0.5, 1.0);
_checkBrushResult(terrain, &brush, 0.5, 0.5, 0.5, 0.25, 0.0, 0.0, 0);
/* Test brush strength */
terrainClearPainting(terrain->height_map);
_checkBrushResult(terrain, &brush, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0);
terrainBrushFlatten(terrain->height_map, &brush, 0.5, 0.01);
_checkBrushResult(terrain, &brush, 0.005, 0.005, 0.005, 0.0025, 0.0, 0.0, 0);
/* Test cumulative effect */
terrainBrushFlatten(terrain->height_map, &brush, 0.5, 0.01);
_checkBrushResult(terrain, &brush, 0.00995, 0.00995, 0.00995, 0.0049875, 0.0, 0.0, 0);
/* Test with height modifier */
terrain->height = 10.0;
terrainClearPainting(terrain->height_map);
_checkBrushResult(terrain, &brush, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0);
terrainBrushFlatten(terrain->height_map, &brush, 0.5, 1.0);
_checkBrushResult(terrain, &brush, 0.05, 0.05, 0.05, 0.025, 0.0, 0.0, 0);
/* Test with scaling modifier */
terrain->height = 10.0;
terrain->scaling = 2.0;
terrainClearPainting(terrain->height_map);
_checkBrushResult(terrain, &brush, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0);
terrainBrushFlatten(terrain->height_map, &brush, 0.5, 1.0);
_checkBrushResult(terrain, &brush, 0.05, 0.05, 0.05, 0.025, 0.0, 0.0, 0);
/* Tear down */
_tearDownDefinition(terrain);
}
TerrainPainting_Test::test_terrain_painting_brush_reset()
{
/* Set up */
TerrainDefinition* terrain = _setUpDefinition();
TerrainBrush brush = {0.0, 0.0, 2.0, 2.0, 4.0};
TerrainBrush brush_full = {0.0, 0.0, 4.0, 0.0, 4.0};
terrain->height = 1.0;
terrain->scaling = 1.0;
noiseForceValue(terrain->_height_noise, 1.0);
/* Test resetting at center */
_checkBrushResult(terrain, &brush, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0);
terrainBrushFlatten(terrain->height_map, &brush_full, 2.0, 1.0);
_checkBrushResult(terrain, &brush, 2.0, 2.0, 2.0, 2.0, 2.0, 1.0, 0);
terrainBrushReset(terrain->height_map, &brush, 1.0);
_checkBrushResult(terrain, &brush, 1.0, 1.0, 1.0, 1.5, 2.0, 1.0, 0);
/* Test brush strength */
terrainClearPainting(terrain->height_map);
_checkBrushResult(terrain, &brush, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0);
terrainBrushFlatten(terrain->height_map, &brush_full, 2.0, 1.0);
_checkBrushResult(terrain, &brush, 2.0, 2.0, 2.0, 2.0, 2.0, 1.0, 0);
terrainBrushReset(terrain->height_map, &brush, 0.1);
_checkBrushResult(terrain, &brush, 1.9, 1.9, 1.9, 1.95, 2.0, 1.0, 0);
/* Test cumulative effect */
terrainBrushReset(terrain->height_map, &brush, 0.1);
_checkBrushResult(terrain, &brush, 1.81, 1.81, 1.81, 1.9025, 2.0, 1.0, 0);
/* Test with height modifier */
terrain->height = 10.0;
terrainClearPainting(terrain->height_map);
_checkBrushResult(terrain, &brush, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0);
terrainBrushFlatten(terrain->height_map, &brush_full, 2.0, 1.0);
_checkBrushResult(terrain, &brush, 1.1, 1.1, 1.1, 1.1, 1.1, 1.0, 0);
terrainBrushReset(terrain->height_map, &brush, 0.1);
_checkBrushResult(terrain, &brush, 1.099, 1.099, 1.099, 1.0995, 1.1, 1.0, 0);
/* Test with scaling modifier */
terrain->height = 10.0;
terrain->scaling = 2.0;
terrainClearPainting(terrain->height_map);
_checkBrushResult(terrain, &brush, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0);
terrainBrushFlatten(terrain->height_map, &brush_full, 2.0, 1.0);
_checkBrushResult(terrain, &brush, 1.1, 1.1, 1.1, 1.1, 1.1, 1.0, 0);
terrainBrushReset(terrain->height_map, &brush, 0.1);
_checkBrushResult(terrain, &brush, 1.099, 1.099, 1.099, 1.0995, 1.1, 1.0, 0);
/* Tear down */
_tearDownDefinition(terrain);
/* Test with scaling and the sinusoid setup (to test the basevalue sampling) */
terrain = _setUpDefinition();
terrain->height = 1.0;
terrain->scaling = 2.0;
_checkBrushResult(terrain, &brush, 0.0, 0.309016994375, 0.587785252292, 0.809016994375, 0.951056516295, 1.0, 1);
terrainBrushFlatten(terrain->height_map, &brush_full, 2.0, 1.0);
_checkBrushResultSides(terrain, &brush, 2.0, 2.0, 2.0, 2.0, 2.0, 1.0, 2.0, 2.0, 2.0, 2.0, -1.0);
terrainBrushReset(terrain->height_map, &brush, 1.0);
_checkBrushResultSides(terrain, &brush, 0.0, 0.309016994375, 0.587785252292, 2.0 - (2.0 - 0.809016994375) * 0.5, 2.0, 1.0, -0.309016994375, -0.587785252292, 2.0 - (2.0 + 0.809016994375) * 0.5, 2.0, -1.0);
_tearDownDefinition(terrain);
}

View file

@ -0,0 +1,20 @@
#ifndef TERRAINPAINTING_TEST_H
#define TERRAINPAINTING_TEST_H
#include "BaseTestCase.h"
class TerrainPainting_Test: public BaseTestCase
{
public:
CPPUNIT_TEST_SUITE(TerrainPainting_Test);
CPPUNIT_TEST(test_terrain_painting_grid);
CPPUNIT_TEST(test_terrain_painting_brush_flatten);
CPPUNIT_TEST(test_terrain_painting_brush_reset);
CPPUNIT_TEST_SUITE_END();
void test_terrain_painting_grid();
void test_terrain_painting_brush_flatten();
void test_terrain_painting_brush_reset();
};
#endif // TERRAINPAINTING_TEST_H

51
src/tests/Zone_Test.cpp Normal file
View file

@ -0,0 +1,51 @@
#include "Zone_Test.h"
CPPUNIT_TEST_SUITE_REGISTRATION(Zone_Test);
#include "rendering/tools/zone.h"
Zone_Test::testZoneAbsoluteHeight()
{
Zone* zone = zoneCreate();
zoneAddHeightRangeQuick(zone, 1.0, -1.0, 2.0, 5.0, 6.0);
ck_assert_double_eq(zoneGetValue(zone, v3(0.0, -10.0, 0.0), VECTOR_UP), 0.0);
ck_assert_double_eq(zoneGetValue(zone, v3(0.0, -2.0, 0.0), VECTOR_UP), 0.0);
ck_assert_double_eq(zoneGetValue(zone, v3(0.0, -1.0, 0.0), VECTOR_UP), 0.0);
ck_assert_double_eq(zoneGetValue(zone, v3(0.0, -0.5, 0.0), VECTOR_UP), 1.0 / 6.0);
ck_assert_double_eq(zoneGetValue(zone, v3(0.0, 0.0, 0.0), VECTOR_UP), 1.0 / 3.0);
ck_assert_double_eq(zoneGetValue(zone, v3(0.0, 0.5, 0.0), VECTOR_UP), 0.5);
ck_assert_double_eq(zoneGetValue(zone, v3(0.0, 2.0, 0.0), VECTOR_UP), 1.0);
ck_assert_double_eq(zoneGetValue(zone, v3(0.0, 2.1, 0.0), VECTOR_UP), 1.0);
ck_assert_double_eq(zoneGetValue(zone, v3(0.0, 3.5, 0.0), VECTOR_UP), 1.0);
ck_assert_double_eq(zoneGetValue(zone, v3(0.0, 4.9, 0.0), VECTOR_UP), 1.0);
ck_assert_double_eq(zoneGetValue(zone, v3(0.0, 5.0, 0.0), VECTOR_UP), 1.0);
ck_assert_double_eq(zoneGetValue(zone, v3(0.0, 5.2, 0.0), VECTOR_UP), 0.8);
ck_assert_double_eq(zoneGetValue(zone, v3(0.0, 5.7, 0.0), VECTOR_UP), 0.3);
ck_assert_double_eq(zoneGetValue(zone, v3(0.0, 6.0, 0.0), VECTOR_UP), 0.0);
ck_assert_double_eq(zoneGetValue(zone, v3(0.0, 15.0, 0.0), VECTOR_UP), 0.0);
ck_assert_double_eq(zoneGetValue(zone, v3(0.0, 150.0, 0.0), VECTOR_UP), 0.0);
zoneDelete(zone);
}
Zone_Test::testZoneRelativeHeight()
{
Zone* zone = zoneCreate();
zoneAddHeightRangeQuick(zone, 1.0, 0.2, 0.3, 0.6, 0.9);
zoneSetRelativeHeight(zone, -2.0, 2.0, 8.0);
ck_assert_double_eq(zoneGetValue(zone, v3(0.0, -10.0, 0.0), VECTOR_UP), 0.0);
ck_assert_double_eq(zoneGetValue(zone, v3(0.0, -2.1, 0.0), VECTOR_UP), 0.0);
ck_assert_double_eq(zoneGetValue(zone, v3(0.0, -2.0, 0.0), VECTOR_UP), 0.0);
ck_assert_double_eq(zoneGetValue(zone, v3(0.0, -1.0, 0.0), VECTOR_UP), 0.0);
ck_assert_double_eq(zoneGetValue(zone, v3(0.0, -0.5, 0.0), VECTOR_UP), 0.0);
ck_assert_double_eq(zoneGetValue(zone, v3(0.0, -0.2, 0.0), VECTOR_UP), 0.25);
ck_assert_double_eq(zoneGetValue(zone, v3(0.0, 0.0, 0.0), VECTOR_UP), 0.5);
ck_assert_double_eq(zoneGetValue(zone, v3(0.0, 0.3, 0.0), VECTOR_UP), 0.875);
ck_assert_double_eq(zoneGetValue(zone, v3(0.0, 0.5, 0.0), VECTOR_UP), 1.0);
ck_assert_double_eq(zoneGetValue(zone, v3(0.0, 1.0, 0.0), VECTOR_UP), 1.0);
zoneDelete(zone);
}

18
src/tests/Zone_Test.h Normal file
View file

@ -0,0 +1,18 @@
#ifndef ZONE_TEST_H
#define ZONE_TEST_H
#include "BaseTestCase.h"
class Zone_Test: public BaseTestCase
{
public:
CPPUNIT_TEST_SUITE(Zone_Test);
CPPUNIT_TEST(testZoneAbsoluteHeight);
CPPUNIT_TEST(testZoneRelativeHeight);
CPPUNIT_TEST_SUITE_END();
void testZoneAbsoluteHeight();
void testZoneRelativeHeight();
};
#endif // ZONE_TEST_H

View file

@ -10,6 +10,8 @@ void noMessageOutput(QtMsgType, const QMessageLogContext&, const QString&)
int main() int main()
{ {
paysagesInit();
qInstallMessageHandler(noMessageOutput); qInstallMessageHandler(noMessageOutput);
TextTestRunner runner; TextTestRunner runner;
@ -17,6 +19,8 @@ int main()
runner.addTest(registry.makeTest()); runner.addTest(registry.makeTest());
paysagesQuit();
return runner.run() ? 1 : 0; return runner.run() ? 1 : 0;
} }

View file

@ -2,7 +2,7 @@ TEMPLATE = app
CONFIG += console CONFIG += console
CONFIG -= app_bundle CONFIG -= app_bundle
TARGET = paysages-tests2 TARGET = paysages-tests
unix { unix {
CONFIG += link_pkgconfig CONFIG += link_pkgconfig
@ -11,11 +11,29 @@ unix {
SOURCES += main.cpp \ SOURCES += main.cpp \
Layers_Test.cpp \ Layers_Test.cpp \
BaseTestCase.cpp BaseTestCase.cpp \
Camera_Test.cpp \
NoiseGenerator_Test.cpp \
PackStream_Test.cpp \
Bruneton_Test.cpp \
Clouds_Test.cpp \
Euclid_Test.cpp \
Render_Test.cpp \
TerrainPainting_Test.cpp \
Zone_Test.cpp
HEADERS += \ HEADERS += \
Layers_Test.h \ Layers_Test.h \
BaseTestCase.h BaseTestCase.h \
Camera_Test.h \
NoiseGenerator_Test.h \
PackStream_Test.h \
Bruneton_Test.h \
Clouds_Test.h \
Euclid_Test.h \
Render_Test.h \
TerrainPainting_Test.h \
Zone_Test.h
win32:CONFIG(release, debug|release): LIBS += -L$$OUT_PWD/../system/release/ -lpaysages_system win32:CONFIG(release, debug|release): LIBS += -L$$OUT_PWD/../system/release/ -lpaysages_system
else:win32:CONFIG(debug, debug|release): LIBS += -L$$OUT_PWD/../system/debug/ -lpaysages_system else:win32:CONFIG(debug, debug|release): LIBS += -L$$OUT_PWD/../system/debug/ -lpaysages_system