summaryrefslogtreecommitdiff
path: root/AK/Tests
diff options
context:
space:
mode:
authorCesar Torres <shortanemoia@protonmail.com>2021-03-19 22:23:48 +0100
committerAndreas Kling <kling@serenityos.org>2021-03-27 10:20:55 +0100
commitf4f5a1c0e748ba4681e001d44a59211abbea3261 (patch)
tree49e5493d6b0117fe92e86162228fe01811578617 /AK/Tests
parent0d5e1e9df18f051d59fbb40954b47bc678c3d5ef (diff)
downloadserenity-f4f5a1c0e748ba4681e001d44a59211abbea3261.zip
AK: Add complex number library
Useful for diverse algorithms. Also added some tests for it.
Diffstat (limited to 'AK/Tests')
-rw-r--r--AK/Tests/CMakeLists.txt1
-rw-r--r--AK/Tests/TestComplex.cpp65
2 files changed, 66 insertions, 0 deletions
diff --git a/AK/Tests/CMakeLists.txt b/AK/Tests/CMakeLists.txt
index 391bfbd0e0..5fc624697c 100644
--- a/AK/Tests/CMakeLists.txt
+++ b/AK/Tests/CMakeLists.txt
@@ -16,6 +16,7 @@ set(AK_TEST_SOURCES
TestCircularDeque.cpp
TestCircularDuplexStream.cpp
TestCircularQueue.cpp
+ TestComplex.cpp
TestDistinctNumeric.cpp
TestDoublyLinkedList.cpp
TestEndian.cpp
diff --git a/AK/Tests/TestComplex.cpp b/AK/Tests/TestComplex.cpp
new file mode 100644
index 0000000000..9cccd525f1
--- /dev/null
+++ b/AK/Tests/TestComplex.cpp
@@ -0,0 +1,65 @@
+/*
+ * Copyright (c) 2021, Cesar Torres <shortanemoia@protonmail.com>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <AK/Complex.h>
+#include <AK/TestSuite.h>
+
+TEST_CASE(Complex)
+{
+ auto a = Complex<float> { 1.f, 1.f };
+ auto b = complex_real_unit<double> + Complex<double> { 0, 1 } * 1;
+ EXPECT_APPROXIMATE(a.real(), b.real());
+ EXPECT_APPROXIMATE(a.imag(), b.imag());
+
+#ifdef AKCOMPLEX_CAN_USE_MATH_H
+ EXPECT_APPROXIMATE((complex_imag_unit<float> - complex_imag_unit<float>).magnitude(), 0);
+ EXPECT_APPROXIMATE((complex_imag_unit<float> + complex_real_unit<float>).magnitude(), sqrt(2));
+
+ auto c = Complex<double> { 0., 1. };
+ auto d = Complex<double>::from_polar(1., M_PI / 2.);
+ EXPECT_APPROXIMATE(c.real(), d.real());
+ EXPECT_APPROXIMATE(c.imag(), d.imag());
+
+ c = Complex<double> { -1., 1. };
+ d = Complex<double>::from_polar(sqrt(2.), 3. * M_PI / 4.);
+ EXPECT_APPROXIMATE(c.real(), d.real());
+ EXPECT_APPROXIMATE(c.imag(), d.imag());
+ EXPECT_APPROXIMATE(d.phase(), 3. * M_PI / 4.);
+ EXPECT_APPROXIMATE(c.magnitude(), d.magnitude());
+ EXPECT_APPROXIMATE(c.magnitude(), sqrt(2.));
+#endif
+ EXPECT_EQ((complex_imag_unit<double> * complex_imag_unit<double>).real(), -1.);
+ EXPECT_EQ((complex_imag_unit<double> / complex_imag_unit<double>).real(), 1.);
+
+ EXPECT_EQ(Complex(1., 10.) == (Complex<double>(1., 0.) + Complex(0., 10.)), true);
+ EXPECT_EQ(Complex(1., 10.) != (Complex<double>(1., 1.) + Complex(0., 10.)), true);
+#ifdef AKCOMPLEX_CAN_USE_MATH_H
+ EXPECT_EQ(approx_eq(Complex<int>(1), Complex<float>(1.0000004f)), true);
+ EXPECT_APPROXIMATE(cexp(Complex<double>(0., 1.) * M_PI).real(), -1.);
+#endif
+}
+
+TEST_MAIN(Complex)