72 lines
2.4 KiB
Plaintext
72 lines
2.4 KiB
Plaintext
|
;; Racket
|
||
|
(module+ test
|
||
|
(require rackunit)
|
||
|
(check-equal? (balance (list (list 'credit 5))) 5)
|
||
|
(check-equal? (balance (list '(debit 5))) -5)
|
||
|
(check-equal? (balance '((debit 11) (credit 3))) -8)
|
||
|
(check-equal? (balance '((debit 3) (credit 5))) 2)
|
||
|
(check-equal? (balance '((debit 5) (credit 23) (debit 23) (credit 5))) 0))
|
||
|
|
||
|
# Python
|
||
|
from invert import invert
|
||
|
def test_empty():
|
||
|
assert invert([]) == {}
|
||
|
def test_simple():
|
||
|
invert(["three","two","one"]) == {"three": 0, "two":1, "one":2}
|
||
|
def test_duplicate():
|
||
|
assert invert(["bob","bob"]) == None
|
||
|
def test_numeric():
|
||
|
assert invert(range(0,6)) == { 0:0, 1:1, 2:2, 3:3, 4:4, 5:5 }
|
||
|
def test_invert():
|
||
|
L=[-8,"pineapple",3]
|
||
|
D=invert(L)
|
||
|
assert [ L[D[j]] for j in L ] == L
|
||
|
|
||
|
// JavaScript
|
||
|
Expression = require("../expression.js").Expression;
|
||
|
|
||
|
let six_plus_nine = new Expression('+', 6, 9);
|
||
|
let six_times_nine = new Expression('*', 6, 9);
|
||
|
let six_minus_nine = new Expression('-', 6, 9);
|
||
|
let sixteen_div_eight = new Expression('/', 16, 8);
|
||
|
let compound1 = new Expression('+', six_times_nine, six_plus_nine)
|
||
|
let compound2 = new Expression('*', six_times_nine, compound1)
|
||
|
let compound3 = new Expression('+', compound2, 3)
|
||
|
|
||
|
describe("constructor",
|
||
|
function() {
|
||
|
let one = new Expression("+",0,1);
|
||
|
it("not null", () => expect(one).not.toBe(null));
|
||
|
it("op", () => expect(one.op).toBe("+"));
|
||
|
it("left", () => expect(one.left).toBe(0));
|
||
|
it("right", () => expect(one.right).toBe(1));
|
||
|
});
|
||
|
|
||
|
describe("simple",
|
||
|
function() {
|
||
|
it("+", () => expect(six_plus_nine.eval()).toBe(15));
|
||
|
it("-", () => expect(six_minus_nine.eval()).toBe(-3));
|
||
|
it("*", () => expect(six_times_nine.eval()).toBe(54));
|
||
|
it("/", () => expect(sixteen_div_eight.eval()).toBe(2));
|
||
|
});
|
||
|
|
||
|
describe("compound",
|
||
|
function() {
|
||
|
it("1", () => expect(compound1.eval()).toBe(69));
|
||
|
it("2", () => expect(compound2.eval()).toBe(3726));
|
||
|
it("3", () => expect(compound3.eval()).toBe(3729));
|
||
|
});
|
||
|
|
||
|
|
||
|
# Octave
|
||
|
%!shared P1,P3
|
||
|
%! P1=[7,5,-3];
|
||
|
%! P3=[1,2,3;4,5,6;7,8,9];
|
||
|
%
|
||
|
%!assert(manypoly(P1,0),7,eps)
|
||
|
%!assert(manypoly(P1,1),9,eps)
|
||
|
%!assert(manypoly(P1,5),7+5*5-3*25,eps)
|
||
|
%!assert(manypoly(P3,0),[1;4;7],eps)
|
||
|
%!assert(manypoly(P3,1),[6;15;24],eps)
|
||
|
%!assert(manypoly(P3,2),[1+2*2+3*4;4+5*2+6*4;7+8*2+9*4],eps)
|