Added unit testing for terrain grid

This commit is contained in:
Michaël Lemaire 2013-06-09 23:03:37 +02:00
parent 47de568004
commit a4ea010b86
4 changed files with 105 additions and 0 deletions

View file

@ -229,6 +229,13 @@ void noiseSetFunction(NoiseGenerator* generator, NoiseFunction* function)
noiseValidate(generator); noiseValidate(generator);
} }
void noiseSetCustomFunction(NoiseGenerator* generator, double (*func1d)(double x), double (*func2d)(double x, double y), double (*func3d)(double x, double y, double z))
{
generator->_func_noise_1d = func1d;
generator->_func_noise_2d = func2d;
generator->_func_noise_3d = func3d;
}
void noiseSetFunctionParams(NoiseGenerator* generator, NoiseFunctionAlgorithm algorithm, double ridge_factor, double curve_factor) void noiseSetFunctionParams(NoiseGenerator* generator, NoiseFunctionAlgorithm algorithm, double ridge_factor, double curve_factor)
{ {
NoiseFunction function = {algorithm, ridge_factor, curve_factor}; NoiseFunction function = {algorithm, ridge_factor, curve_factor};

View file

@ -46,6 +46,7 @@ void noiseCopy(NoiseGenerator* source, NoiseGenerator* destination);
void noiseValidate(NoiseGenerator* generator); void noiseValidate(NoiseGenerator* generator);
void noiseRandomizeOffsets(NoiseGenerator* generator); void noiseRandomizeOffsets(NoiseGenerator* generator);
NoiseFunction noiseGetFunction(NoiseGenerator* generator); NoiseFunction noiseGetFunction(NoiseGenerator* generator);
void noiseSetCustomFunction(NoiseGenerator* generator, double (*func1d)(double x), double (*func2d)(double x, double y), double (*func3d)(double x, double y, double z));
void noiseSetFunction(NoiseGenerator* generator, NoiseFunction* function); void noiseSetFunction(NoiseGenerator* generator, NoiseFunction* function);
void noiseSetFunctionParams(NoiseGenerator* generator, NoiseFunctionAlgorithm algorithm, double ridge_factor, double curve_factor); void noiseSetFunctionParams(NoiseGenerator* generator, NoiseFunctionAlgorithm algorithm, double ridge_factor, double curve_factor);
void noiseForceValue(NoiseGenerator* generator, double value); void noiseForceValue(NoiseGenerator* generator, double value);

View file

@ -7,6 +7,7 @@ extern void test_euclid_case(Suite* s);
extern void test_camera_case(Suite* s); extern void test_camera_case(Suite* s);
extern void test_clouds_case(Suite* s); extern void test_clouds_case(Suite* s);
extern void test_noise_case(Suite* s); extern void test_noise_case(Suite* s);
extern void test_terrain_painting_case(Suite* s);
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
@ -20,6 +21,7 @@ int main(int argc, char** argv)
test_camera_case(s); test_camera_case(s);
test_clouds_case(s); test_clouds_case(s);
test_noise_case(s); test_noise_case(s);
test_terrain_painting_case(s);
SRunner *sr = srunner_create(s); SRunner *sr = srunner_create(s);
srunner_run_all(sr, CK_NORMAL); srunner_run_all(sr, CK_NORMAL);

View file

@ -0,0 +1,95 @@
#include "testing/common.h"
#include <math.h>
#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);
}
START_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);
}
END_TEST
TEST_CASE(terrain_painting, test_terrain_painting_grid)