diff tests/g2.h @ 0:d098192f01d9

Initial commit to the repository.
author Brian Neal <bgneal@gmail.com>
date Sat, 19 Mar 2011 19:53:12 -0500
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/g2.h	Sat Mar 19 19:53:12 2011 -0500
@@ -0,0 +1,68 @@
+#ifndef FRUCTOSE_MAIN_TEST_X2_H
+#define FRUCTOSE_MAIN_TEST_X2_H
+
+#include <stdexcept>
+#include <cmath>
+
+#include "fructose/fructose.h"
+
+// These tests are rigged so that some of them fail.
+// The tests include exercising floating point comparisons
+// and exception handling.
+
+namespace {
+    void throw_a_runtime_error()
+    {
+        throw std::runtime_error("this is a runtime error");
+    }
+}
+
+FRUCTOSE_STRUCT(misc_tests)
+{
+    FRUCTOSE_TEST(exceptions)
+    {
+        fructose_assert_exception(throw_a_runtime_error(), std::logic_error);
+        fructose_assert_no_exception(throw_a_runtime_error());
+    }
+
+    FRUCTOSE_TEST(loopdata)
+    {
+        static const struct {
+            int line_number;
+            int a;
+            int b;
+            int expected_result;
+        } data[] = {
+            {__LINE__, 3, 4, 12}
+          , {__LINE__, 1, 50, 50}
+          , {__LINE__, 5, 12, 60}
+          , {__LINE__, 6, 6, 37}
+          , {__LINE__, 7, 10, 70}
+        };
+
+        for (std::size_t i = 0; i < sizeof(data)/sizeof(data[0]); ++i)
+        {
+            if (verbose())
+            {
+                std::cout << "Testing to see if "
+                          << data[i].a << " * " << data[i].b 
+                          << " = " << data[i].expected_result
+                          << std::endl;
+            }
+            int result = data[i].a * data[i].b;
+            fructose_loop1_assert(data[i].line_number, i,
+                                 result == data[i].expected_result);
+        }
+    }
+
+    FRUCTOSE_TEST(floatingpoint)
+    {
+        double my_pi = 3.141592654;
+        double calc_pi = 4.0 * atan(1.0);
+        fructose_assert_double_eq_rel_abs(my_pi, calc_pi, 0.01, 0.01);
+        fructose_assert_double_eq(my_pi, calc_pi);
+        fructose_assert_double_ne(my_pi, calc_pi);
+        fructose_assert_double_ne_rel_abs(my_pi, calc_pi, 0.01, 0.01);
+    }
+};
+#endif