this repo has no description
at trunk 103 lines 3.6 kB view raw
1// Copyright (c) Facebook, Inc. and its affiliates. (http://www.facebook.com) 2#include "complex-builtins.h" 3 4#include <cmath> 5 6#include "gtest/gtest.h" 7 8#include "builtins.h" 9#include "frame.h" 10#include "object-builtins.h" 11#include "runtime.h" 12#include "test-utils.h" 13 14namespace py { 15namespace testing { 16 17using ComplexBuiltinsTest = RuntimeFixture; 18 19TEST_F(ComplexBuiltinsTest, AddWithIntReturnsComplex) { 20 HandleScope scope(thread_); 21 Complex c(&scope, runtime_->newComplex(1, 2)); 22 Int i(&scope, runtime_->newInt(10)); 23 Object result_obj(&scope, runBuiltin(METH(complex, __add__), c, i)); 24 ASSERT_FALSE(result_obj.isError()); 25 Complex result(&scope, *result_obj); 26 EXPECT_EQ(result.real(), 11); 27 EXPECT_EQ(result.imag(), 2); 28} 29 30TEST_F(ComplexBuiltinsTest, IntAddWithComplexReturnsComplex) { 31 HandleScope scope(thread_); 32 Int i(&scope, runtime_->newInt(10)); 33 Complex c(&scope, runtime_->newComplex(1, 2)); 34 Object result_obj(&scope, Interpreter::binaryOperation( 35 thread_, Interpreter::BinaryOp::ADD, i, c)); 36 ASSERT_FALSE(result_obj.isError()); 37 Complex result(&scope, *result_obj); 38 EXPECT_EQ(result.real(), 11); 39 EXPECT_EQ(result.imag(), 2); 40} 41 42TEST_F(ComplexBuiltinsTest, BuiltinBaseIsComplex) { 43 HandleScope scope(thread_); 44 Type complex_type(&scope, runtime_->typeAt(LayoutId::kComplex)); 45 EXPECT_EQ(complex_type.builtinBase(), LayoutId::kComplex); 46} 47 48TEST_F(ComplexBuiltinsTest, ComplexMultipliesComplex) { 49 HandleScope scope(thread_); 50 Complex c1(&scope, runtime_->newComplex(1, 2)); 51 Complex c2(&scope, runtime_->newComplex(-1.5, 5)); 52 Object result_obj(&scope, runBuiltin(METH(complex, __mul__), c1, c2)); 53 ASSERT_FALSE(result_obj.isError()); 54 Complex result(&scope, *result_obj); 55 EXPECT_EQ(result.real(), -11.5); 56 EXPECT_EQ(result.imag(), 2); 57} 58 59TEST_F(ComplexBuiltinsTest, ComplexDividesComplexRealSmallerThanImag) { 60 HandleScope scope(thread_); 61 Complex c1(&scope, runtime_->newComplex(-1, 2)); 62 Complex c2(&scope, runtime_->newComplex(1, 2)); 63 Object result_obj(&scope, runBuiltin(METH(complex, __truediv__), c1, c2)); 64 ASSERT_FALSE(result_obj.isError()); 65 Complex result(&scope, *result_obj); 66 EXPECT_EQ(result.real(), 0.6); 67 EXPECT_EQ(result.imag(), 0.8); 68} 69 70TEST_F(ComplexBuiltinsTest, ComplexDividesComplexRealLargerThanImag) { 71 HandleScope scope(thread_); 72 Complex c1(&scope, runtime_->newComplex(-1, 2)); 73 Complex c2(&scope, runtime_->newComplex(2, 1)); 74 Object result_obj(&scope, runBuiltin(METH(complex, __truediv__), c1, c2)); 75 ASSERT_FALSE(result_obj.isError()); 76 Complex result(&scope, *result_obj); 77 EXPECT_EQ(result.real(), 0); 78 EXPECT_EQ(result.imag(), 1); 79} 80 81TEST_F(ComplexBuiltinsTest, ComplexDividesComplexWithNan) { 82 HandleScope scope(thread_); 83 Complex c1(&scope, runtime_->newComplex(-1, 2)); 84 Complex c2(&scope, runtime_->newComplex(2, kDoubleNaN)); 85 Object result_obj(&scope, runBuiltin(METH(complex, __truediv__), c1, c2)); 86 ASSERT_FALSE(result_obj.isError()); 87 Complex result(&scope, *result_obj); 88 EXPECT_TRUE(std::isnan(result.real())); 89 EXPECT_TRUE(std::isnan(result.imag())); 90} 91 92TEST_F(ComplexBuiltinsTest, ComplexDividesByZero) { 93 HandleScope scope(thread_); 94 Complex c1(&scope, runtime_->newComplex(-1, 2)); 95 Complex c2(&scope, runtime_->newComplex(0, 0)); 96 Object result_obj(&scope, runBuiltin(METH(complex, __truediv__), c1, c2)); 97 ASSERT_TRUE(result_obj.isError()); 98 EXPECT_TRUE(raisedWithStr(*result_obj, LayoutId::kZeroDivisionError, 99 "complex division by zero")); 100} 101 102} // namespace testing 103} // namespace py