Skip to content

Commit

Permalink
Added modulo and truncation
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickroberts committed Dec 20, 2021
1 parent b7fca7b commit d605cf6
Show file tree
Hide file tree
Showing 13 changed files with 151 additions and 4 deletions.
7 changes: 5 additions & 2 deletions src/complex.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import mock from './__fixtures__/mock';
import Complex from './complex';
import { Component, principal } from './internal';
import { real, imag, abs, arg, norm } from './accessors';
import { add, sub, mul, div, pow, toString } from './methods';
import { add, sub, mul, div, mod, pow, toString } from './methods';

jest.mock('./internal/principal');
jest.mock('./accessors/real');
Expand All @@ -16,6 +16,7 @@ jest.mock('./methods/add');
jest.mock('./methods/sub');
jest.mock('./methods/mul');
jest.mock('./methods/div');
jest.mock('./methods/mod');
jest.mock('./methods/pow');
jest.mock('./methods/toString');

Expand All @@ -30,6 +31,7 @@ beforeEach(() => {
mock(sub).mockClear();
mock(mul).mockClear();
mock(div).mockClear();
mock(mod).mockClear();
mock(pow).mockClear();
});

Expand Down Expand Up @@ -90,11 +92,12 @@ describe.each<['real' | 'imag' | 'abs' | 'arg' | 'norm', typeof real]>([
});
});

describe.each<['add' | 'sub' | 'mul' | 'div' | 'pow', typeof add]>([
describe.each<['add' | 'sub' | 'mul' | 'div' | 'mod' | 'pow', typeof add]>([
['add', add],
['sub', sub],
['mul', mul],
['div', div],
['mod', mod],
['pow', pow],
])('methods', (name, impl) => {
describe(`Complex.prototype.${name}`, () => {
Expand Down
6 changes: 5 additions & 1 deletion src/complex.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Component, principal } from './internal';
import { real, imag, abs, arg, norm } from './accessors';
import { add, sub, mul, div, pow, toString } from './methods';
import { add, sub, mul, div, mod, pow, toString } from './methods';

export default class Complex {
/** @internal */
Expand Down Expand Up @@ -63,6 +63,10 @@ export default class Complex {
return div(Complex, this, z);
}

public mod(this: Complex, z: Complex): Complex {
return mod(Complex, this, z);
}

public pow(this: Complex, z: Complex): Complex {
return pow(Complex, this, z);
}
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ export { default as Complex } from './complex';
export { E, I, LN10, LN2, LOG10E, LOG2E, PI, SQRT1_2, SQRT2 } from './constants';
export {
acos, acosh, add, asin, asinh, atan, atanh, cartesian, cbrt, conj, cos, cosh, div, exp, from, log,
mul, polar, pow, proj, sin, sinh, sqrt, sub, tan, tanh,
mod, mul, polar, pow, proj, sin, sinh, sqrt, sub, tan, tanh, trunc,
} from './static';
export { BinaryExpression, CallExpression, Expression, Identifier, Literal, UnaryExpression, parse } from './parser';
1 change: 1 addition & 0 deletions src/methods/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ export { default as add } from './add';
export { default as sub } from './sub';
export { default as mul } from './mul';
export { default as div } from './div';
export { default as mod } from './mod';
export { default as pow } from './pow';
export { default as toString } from './toString';
36 changes: 36 additions & 0 deletions src/methods/mod.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import mock from '../__fixtures__/mock';

import Complex from '../complex';
import div from './div';
import mul from './mul';
import sub from './sub';
import trunc from './trunc';
import sut from './mod';

jest.mock('./div');
jest.mock('./sub');
jest.mock('./mul');
jest.mock('./trunc');

it('should delegate implementation to alternate form', () => {
const lhs = {} as Complex;
const rhs = {} as Complex;
const divLhsRhs = {} as Complex;
const truncDivLhsRhs = {} as Complex;
const mulTruncDivLhsRhsRhs = {} as Complex;
const subLhsMulTruncDivLhsRhsRhs = {} as Complex;

mock(div).mockReturnValueOnce(divLhsRhs);
mock(trunc).mockReturnValueOnce(truncDivLhsRhs);
mock(mul).mockReturnValueOnce(mulTruncDivLhsRhsRhs);
mock(sub).mockReturnValueOnce(subLhsMulTruncDivLhsRhsRhs);

const actual = sut(Complex, lhs, rhs);

expect(div).toHaveBeenCalledWith(Complex, lhs, rhs);
expect(trunc).toHaveBeenCalledWith(Complex, divLhsRhs);
expect(mul).toHaveBeenCalledWith(Complex, truncDivLhsRhs, rhs);
expect(sub).toHaveBeenCalledWith(Complex, lhs, mulTruncDivLhsRhsRhs);

expect(actual).toBe(subLhsMulTruncDivLhsRhsRhs);
});
9 changes: 9 additions & 0 deletions src/methods/mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Complex from '../complex';
import div from './div';
import mul from './mul';
import sub from './sub';
import trunc from './trunc';

export default (Ctor: typeof Complex, lhs: Complex, rhs: Complex): Complex => (
sub(Ctor, lhs, mul(Ctor, trunc(Ctor, div(Ctor, lhs, rhs)), rhs))
);
30 changes: 30 additions & 0 deletions src/methods/trunc.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import _ from '../__fixtures__/any/number';
import mock from '../__fixtures__/mock';

import Complex from '../complex';
import Component from '../internal/component';
import { real, imag } from '../accessors';
import sut from './trunc';

jest.mock('../complex');
jest.mock('../accessors/real');
jest.mock('../accessors/imag');

it('should initialize polar components from cartesian components', () => {
const testReal = 3.5;
const testImag = -4.5;
const z = new Complex(testReal, testImag, _, _, Component.CARTESIAN);
const expected = {} as Complex;

mock(Complex).mockClear();
mock(Complex).mockReturnValueOnce(expected);

const actual = sut(Complex, z);

expect(real).toHaveBeenCalledWith(Complex, z);
expect(imag).toHaveBeenCalledWith(Complex, z);
expect(Complex).toHaveBeenCalledWith(
Math.trunc(testReal), Math.trunc(testImag), _, _, Component.CARTESIAN,
);
expect(actual).toBe(expected);
});
9 changes: 9 additions & 0 deletions src/methods/trunc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Complex from '../complex';
import Component from '../internal/component';
import { real, imag } from '../accessors';

const trunc = (Ctor: typeof Complex, z: Complex): Complex => new Ctor(
Math.trunc(real(Ctor, z)), Math.trunc(imag(Ctor, z)), 0, 0, Component.CARTESIAN,
);

export default trunc;
2 changes: 2 additions & 0 deletions src/static/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export { default as div } from './div';
export { default as exp } from './exp';
export { default as from } from './from';
export { default as log } from './log';
export { default as mod } from './mod';
export { default as mul } from './mul';
export { default as polar } from './polar';
export { default as pow } from './pow';
Expand All @@ -24,3 +25,4 @@ export { default as sqrt } from './sqrt';
export { default as sub } from './sub';
export { default as tan } from './tan';
export { default as tanh } from './tanh';
export { default as trunc } from './trunc';
21 changes: 21 additions & 0 deletions src/static/mod.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import mock from '../__fixtures__/mock';

import Complex from '../complex';
import mod from '../methods/mod';
import sut from './mod';

jest.mock('../complex');
jest.mock('../methods/mod');

it('should delegate to mod method', () => {
const lhs = {} as Complex;
const rhs = {} as Complex;
const expected = {} as Complex;

mock(mod).mockReturnValueOnce(expected);

const actual = sut(lhs, rhs);

expect(mod).toHaveBeenCalledWith(Complex, lhs, rhs);
expect(actual).toBe(expected);
});
6 changes: 6 additions & 0 deletions src/static/mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import Complex from '../complex';
import _mod from '../methods/mod';

const mod = (lhs: Complex, rhs: Complex): Complex => _mod(Complex, lhs, rhs);

export default mod;
20 changes: 20 additions & 0 deletions src/static/trunc.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import mock from '../__fixtures__/mock';

import Complex from '../complex';
import trunc from '../methods/trunc';
import sut from './trunc';

jest.mock('../complex');
jest.mock('../methods/trunc');

it('should delegate to trunc method', () => {
const z = {} as Complex;
const expected = {} as Complex;

mock(trunc).mockReturnValueOnce(expected);

const actual = sut(z);

expect(trunc).toHaveBeenCalledWith(Complex, z);
expect(actual).toBe(expected);
});
6 changes: 6 additions & 0 deletions src/static/trunc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import Complex from '../complex';
import _trunc from '../methods/trunc';

const trunc = (z: Complex): Complex => _trunc(Complex, z);

export default trunc;

0 comments on commit d605cf6

Please sign in to comment.