Files
c3c/test/unit/stdlib/math/math_complex.c3
Christoffer Lerno 5c77c9a754 - Change distinct -> typedef.
- Order of attribute declaration is changed for `alias`.
- Added `LANGUAGE_DEV_VERSION` env constant.
- Rename `anyfault` -> `fault`.
- Changed `fault` -> `faultdef`.
- Added `attrdef` instead of `alias` for attribute aliases.
2025-03-15 20:10:47 +01:00

60 lines
1.3 KiB
Plaintext

module math_tests @test;
import math_tests::complex;
alias ComplexDouble @local = ComplexType {double};
alias ComplexInt @local = ComplexType {int};
module math_tests::complex {ElementType} @test;
import std::math;
alias ComplexType = Complex {ElementType};
fn void complex_mul_imaginary()
{
ComplexType i = complex::IMAGINARY {ElementType};
assert(i.mul(i).equals((ComplexType){-1, 0}));
assert(i.mul(i).mul(i).equals((ComplexType){0, -1}));
}
fn void complex_add()
{
ComplexType a = {3, 4};
ComplexType b = {1, 2};
assert(a.add(b).equals((ComplexType){4, 6}));
assert(a.add_each(1).equals((ComplexType){4, 5}));
}
fn void complex_sub()
{
ComplexType a = {3, 4};
ComplexType b = {1, 2};
assert(a.sub(b).equals((ComplexType){2, 2}));
assert(a.sub_each(1).equals((ComplexType){2, 3}));
}
fn void complex_scale()
{
ComplexType a = {2, 1};
assert(a.scale(2).equals((ComplexType){4, 2}));
}
fn void complex_conjugate()
{
ComplexType a = {3, 4};
assert(a.conjugate().equals((ComplexType){3, -4}));
}
fn void complex_inverse() @if(types::is_float(ElementType))
{
ComplexType a = {3, 4};
assert(a.inverse().mul(a).equals(complex::IDENTITY{ElementType}));
}
fn void complex_div() @if(types::is_float(ElementType))
{
ComplexType a = {2, 5};
ComplexType b = {4, -1};
assert(a.div(b).equals((ComplexType){3.0/17.0, 22.0/17.0}));
}