Skip to content

Commit

Permalink
テストを追加
Browse files Browse the repository at this point in the history
  • Loading branch information
yasuhito committed Aug 5, 2023
1 parent 61ebbf2 commit 5c7cba3
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/common/src/complex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export class Complex {
}

isApproximatelyEqualTo(other: number | Complex | unknown, epsilon: number): boolean {
if (other instanceof Complex || typeof other === 'number') {
if (typeof other === 'number' || other instanceof Complex) {
const d = this.minus(Complex.from(other))
return Math.abs(d.real) <= epsilon && Math.abs(d.imag) <= epsilon && d.abs() <= epsilon
}
Expand Down
62 changes: 62 additions & 0 deletions packages/common/test/complex.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,4 +360,66 @@ describe('Complex', () => {
})
})
})

describe('norm2', () => {
describe('0+0i', () => {
test('equal 0', () => {
expect(Complex.ZERO.norm2()).toBe(0)
})
})

describe('1+0i', () => {
test('equal 1', () => {
expect(Complex.ONE.norm2()).toBe(1)
})
})

describe('0+i', () => {
test('equal 1', () => {
expect(Complex.I.norm2()).toBe(1)
})
})

describe('1+i', () => {
test('equal 2', () => {
expect(new Complex(1, 1).norm2()).toBe(2)
})
})

describe('2+0i', () => {
test('equal 4', () => {
expect(new Complex(2, 0).norm2()).toBe(4)
})
})

describe('0+2i', () => {
test('equal 4', () => {
expect(new Complex(0, 2).norm2()).toBe(4)
})
})

describe('-2+0i', () => {
test('equal 4', () => {
expect(new Complex(-2, 0).norm2()).toBe(4)
})
})

describe('0-2i', () => {
test('equal 4', () => {
expect(new Complex(0, -2).norm2()).toBe(4)
})
})

describe('2+3i', () => {
test('equal 13', () => {
expect(new Complex(2, 3).norm2()).toBe(13)
})
})

describe('-3-4i', () => {
test('equal 25', () => {
expect(new Complex(-3, -4).norm2()).toBe(25)
})
})
})
})

0 comments on commit 5c7cba3

Please sign in to comment.