Changeset 25709c3 in mainline


Ignore:
Timestamp:
2018-07-05T21:41:24Z (6 years ago)
Author:
Dzejrou <dzejrou@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
937de98
Parents:
15f407a
git-author:
Dzejrou <dzejrou@…> (2018-05-15 15:44:05)
git-committer:
Dzejrou <dzejrou@…> (2018-07-05 21:41:24)
Message:

cpp: added tests for basic complex arithmetic

Location:
uspace/lib/cpp
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/cpp/include/internal/test/tests.hpp

    r15f407a r25709c3  
    229229        private:
    230230            void test_algorithms();
     231            void test_complex();
    231232    };
    232233}
  • uspace/lib/cpp/src/internal/test/numeric.cpp

    r15f407a r25709c3  
    2727 */
    2828
     29#include <iostream> // TODO: remove
     30
    2931#include <array>
     32#include <complex>
    3033#include <initializer_list>
    3134#include <internal/test/tests.hpp>
    3235#include <numeric>
    3336#include <utility>
     37
     38using namespace std::literals;
     39using namespace complex_literals;
    3440
    3541namespace std::test
     
    4147
    4248        test_algorithms();
     49        test_complex();
    4350
    4451        return end();
     
    147154        );
    148155    }
     156
     157    void numeric_test::test_complex()
     158    {
     159        auto c1 = 1.f + 2.5if;
     160        test_eq("complex literals pt1", c1.real(), 1.f);
     161        test_eq("complex literals pt2", c1.imag(), 2.5f);
     162
     163        std::complex<double> c2{2.0, 0.5};
     164        test_eq("complex value initialization", c2, (2.0 + 0.5i));
     165
     166        std::complex<double> c3{c2};
     167        test_eq("complex copy initialization", c3, (2.0 + 0.5i));
     168
     169        std::complex<double> c4{c1};
     170        test_eq("complex conversion initialization", c4, (1.0 + 2.5i));
     171
     172        test_eq("complex sum", ((1.0 + 2.5i) + (3.0 + 0.5i)), (4.0 + 3.0i));
     173        test_eq("complex sub", ((2.0 + 3.0i) - (1.0 + 5.0i)), (1.0 - 2.0i));
     174        test_eq("complex mul", ((2.0 + 2.0i) * (2.0 + 3.0i)), (-2.0 + 10.0i));
     175        test_eq("complex div", ((2.0 - 1.0i) / (3.0 + 4.0i)), (0.08 - 0.44i));
     176        test_eq("complex unary minus", -(1.0 + 1.0i), (-1.0 - 1.0i));
     177        test_eq("complex abs", std::abs(2.0 - 4.0i), 20.0);
     178        test_eq("complex real", std::real(2.0 + 3.0i), 2.0);
     179        test_eq("complex imag", std::imag(2.0 + 3.0i), 3.0);
     180    }
    149181}
    150182
Note: See TracChangeset for help on using the changeset viewer.