chiark / gitweb /
Create some simple fractal known-answer tests to validate new maths types
authorRoss Younger <onyx-commits@impropriety.org.uk>
Sun, 27 Jan 2013 09:14:28 +0000 (22:14 +1300)
committerRoss Younger <onyx-commits@impropriety.org.uk>
Sun, 27 Jan 2013 09:25:52 +0000 (22:25 +1300)
Makefile.am
libfractal/Fractal.cpp
libfractal/Fractal.h
test/FractalKAT.cpp [new file with mode: 0644]

index e13827d..940ba5f 100644 (file)
@@ -124,7 +124,8 @@ b2test_SOURCES=test/b2test.cpp \
                                        test/MockFractal.h test/MockFractal.cpp \
                                        test/MockPalette.h \
                                        test/MockPrefs.h test/MockPrefs.cpp \
-                                       test/Plot3Test.cpp test/Render2Test.cpp
+                                       test/Plot3Test.cpp test/Render2Test.cpp \
+                                       test/FractalKAT.cpp
 
 b2test_LDADD= libgtest.a $(all_ldadd)
 b2test_DEPENDENCIES= libgtest.a $(all_libs)
index c831777..3887836 100644 (file)
@@ -44,6 +44,16 @@ void Fractal::FractalCommon::load_base() {
        load_Misc();
 }
 
+void Fractal::FractalCommon::unload_registry() {
+       auto all = registry.names();
+       for (auto it : all) {
+               auto f = registry.get(it);
+               registry.dereg(it);
+               delete f;
+       }
+       base_loaded = false;
+}
+
 struct MathsInfo {
        Maths::MathsType val;
        const char* name;
index 6f5a318..cc7d2cd 100644 (file)
@@ -113,6 +113,9 @@ public:
        // Call on startup to load the base fractals.
        static void load_base();
 
+       // Unloads the fractal registry, if this is somehow useful (e.g. valgrind tests)
+       static void unload_registry();
+
        // What is the most appropriate maths type to use for this pixel size?
        // Returns v_max if nothing suits.
        static Maths::MathsType select_maths_type(Fractal::Value pixsize);
diff --git a/test/FractalKAT.cpp b/test/FractalKAT.cpp
new file mode 100644 (file)
index 0000000..ef6fbab
--- /dev/null
@@ -0,0 +1,80 @@
+/*  FractalKAT.cpp: Fractal known-answer tests
+    Copyright (C) 2013 Ross Younger
+
+    This program is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include <gtest/gtest.h>
+#include "Fractal.h"
+
+using namespace Fractal;
+
+struct TestData {
+       Point coords;
+       int iters;
+       TestData(Value x, Value y, int its) : coords(x,y), iters(its) {}
+};
+
+#define MAXITERS 255
+
+// Points and expected values determined using one test case, others validated against it
+TestData vectors[] = {
+               { 0, 0, MAXITERS },
+               { -1.3096, -0.0036, MAXITERS },
+               { -0.186658359, 0.852167414, 36 },
+               { 1.0, 1.0, 4 },
+};
+
+
+class FractalKAT : public ::testing::TestWithParam<Maths::MathsType> {
+public:
+       FractalImpl *impl;
+
+       virtual void SetUp() {
+               FractalCommon::load_base();
+               impl = FractalCommon::registry.get("Mandelbrot");
+               if (impl==0) throw "Cannot find my fractal!";
+       }
+       virtual void TearDown() {
+               FractalCommon::unload_registry();
+       }
+
+       void run_vector(TestData& v) {
+               PointData data;
+               impl->prepare_pixel(v.coords, data);
+               impl->plot_pixel(MAXITERS, data, GetParam());
+               EXPECT_EQ(data.iter, v.iters);
+               if (v.iters != MAXITERS)
+                       EXPECT_TRUE(data.nomore);
+       }
+
+       void run_vectors() {
+               if (GetParam() == Maths::MathsType::MAX)
+                       return;
+               for (unsigned i=0; i < sizeof(vectors)/sizeof(*vectors); i++)
+                       run_vector(vectors[i]);
+       }
+};
+
+TEST_P(FractalKAT, AnswersCorrect) {
+       run_vectors();
+}
+
+#define DO_TYPES(type,name,minpix) Maths::MathsType::name,
+
+INSTANTIATE_TEST_CASE_P(AllMathTypes, FractalKAT,
+               ::testing::Values(
+                               ALL_MATHS_TYPES(DO_TYPES)
+                               Maths::MathsType::MAX // dummy to terminate
+                               ));