Add final test material
This commit is contained in:
parent
fcef1277f3
commit
4ecaa1a216
22
tests/Final/Q1 Racket/balance.rkt
Normal file
22
tests/Final/Q1 Racket/balance.rkt
Normal file
@ -0,0 +1,22 @@
|
||||
#lang racket
|
||||
(define (balance lst)
|
||||
(define (helper lst counter)
|
||||
(cond
|
||||
[(empty? lst) counter] ;;base case
|
||||
[(list? (first lst)) (helper (rest lst) counter)] ;;unwrap list
|
||||
[(eq? (first lst) 'debit) (helper (rest lst) (- counter last))] ;;if debit subtract the amount
|
||||
[(eq? (first lst) 'credit) (helper (rest lst) (+ counter last))] ;;if credit add the amount
|
||||
[else (helper (rest lst) counter)]
|
||||
)
|
||||
)
|
||||
(helper lst 0)
|
||||
)
|
||||
|
||||
;; 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))
|
0
tests/Final/Q1 Racket/balance.rkt~
Normal file
0
tests/Final/Q1 Racket/balance.rkt~
Normal file
19
tests/Final/Q2 Python/invert.py
Normal file
19
tests/Final/Q2 Python/invert.py
Normal file
@ -0,0 +1,19 @@
|
||||
def invert(lst):
|
||||
lst = list(lst) # Makes sure arugment is a list
|
||||
|
||||
#list_range = range(0, len(lst)) # Code I might have used if I used a dictionary comprehension
|
||||
|
||||
|
||||
ret_dict = dict() # Create empty dict
|
||||
counter = 0 # Create counter
|
||||
|
||||
for i in lst:
|
||||
ret_dict[i] = counter # Assign each element of list to
|
||||
# its postion in list
|
||||
|
||||
counter = counter + 1 # Increment counter
|
||||
|
||||
if (len(lst) > len(ret_dict)): # Check if the length of new dict is less than
|
||||
return None # input list, if so, there is duplicates so return none
|
||||
|
||||
return ret_dict # Return created dictionary
|
13
tests/Final/Q2 Python/test_invert.py
Normal file
13
tests/Final/Q2 Python/test_invert.py
Normal file
@ -0,0 +1,13 @@
|
||||
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
|
50
tests/Final/Q3 Javascript/expression.js
Normal file
50
tests/Final/Q3 Javascript/expression.js
Normal file
@ -0,0 +1,50 @@
|
||||
class Expression {
|
||||
constructor(op, left, right) {
|
||||
this.op = op
|
||||
this.left = left
|
||||
this.right = right
|
||||
}
|
||||
|
||||
eval() {
|
||||
return evalExpression(this)
|
||||
}
|
||||
}
|
||||
|
||||
function evalExpression(expr) {
|
||||
let tempLeft
|
||||
let tempRight
|
||||
if (typeof(expr.left) === "object") { // Check if left type is another expression
|
||||
tempLeft = evalExpression(expr.left)
|
||||
}
|
||||
else {
|
||||
tempLeft = expr.left
|
||||
}
|
||||
if (typeof(expr.right) === "object") { // Check if right type is another expression
|
||||
tempRight = evalExpression(expr.right)
|
||||
}
|
||||
else {
|
||||
tempRight = expr.right
|
||||
}
|
||||
if (typeof(tempLeft) === "number" & typeof(tempRight) === "number") { // Make sure both inputs are number
|
||||
if (expr.op === "+") {
|
||||
return tempLeft + tempRight
|
||||
}
|
||||
else if(expr.op === "-") {
|
||||
return tempLeft - tempRight
|
||||
}
|
||||
else if(expr.op === "*") {
|
||||
return tempLeft * tempRight
|
||||
}
|
||||
else if(expr.op === "/") {
|
||||
return tempLeft / tempRight
|
||||
}
|
||||
else { // Case for when there is no valid provided operator
|
||||
return "Invalid operator syntax"
|
||||
}
|
||||
}
|
||||
else { //Case for when the left or right are not numbers
|
||||
return "Invalid number syntax"
|
||||
}
|
||||
}
|
||||
|
||||
exports.Expression = Expression;
|
71
tests/Final/Q3 Javascript/spec/expression.spec.js
Normal file
71
tests/Final/Q3 Javascript/spec/expression.spec.js
Normal file
@ -0,0 +1,71 @@
|
||||
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));
|
||||
});
|
||||
|
||||
describe("floating point", //Testing floating point
|
||||
function() {
|
||||
let addPointFive = new Expression("+", 0.5, 0.5)
|
||||
let timesPointFive = new Expression("*", 0.5, 10)
|
||||
let minusPointFive = new Expression("-", 10, 0.5)
|
||||
let dividePointFive = new Expression("/", 0.5, 0.1)
|
||||
|
||||
it("+", () => expect(addPointFive.eval()).toBe(1.0));
|
||||
it("*", () => expect(timesPointFive.eval()).toBe(5.0));
|
||||
it("-", () => expect(minusPointFive.eval()).toBe(9.5));
|
||||
it("/", () => expect(dividePointFive.eval()).toBe(5.0));
|
||||
});
|
||||
|
||||
|
||||
describe("bad input", //Testing bad inputs
|
||||
function() {
|
||||
let invalidStringLeft = new Expression("+", "five", 6)
|
||||
let invalidStringRight = new Expression("+", 5, "six")
|
||||
let invalidOperator = new Expression("^", 6, 9)
|
||||
|
||||
it("Invalid String on left side", () => expect(invalidStringLeft.eval()).toBe("Invalid number syntax"));
|
||||
it("Invalid String on right side", () => expect(invalidStringRight.eval()).toBe("Invalid number syntax"));
|
||||
it("invalid operator", () => expect(invalidOperator.eval()).toBe("Invalid operator syntax"));
|
||||
});
|
||||
|
||||
|
||||
describe("compound bad input", //Testing bad inputs in compound cases
|
||||
function() {
|
||||
let invalidNumber = new Expression("+", "five", 6)
|
||||
let invalidOperator = new Expression("^", 6, 9)
|
||||
|
||||
let semiValidCompound = new Expression("+", six_plus_nine, invalidNumber)
|
||||
let completlyBadCompound = new Expression("+", invalidNumber, invalidOperator)
|
||||
|
||||
it("semi-valid", () => expect(semiValidCompound.eval()).toBe("Invalid number syntax"));
|
||||
it("invalid", () => expect(completlyBadCompound.eval()).toBe("Invalid number syntax")); //Expected to be invalid number because it is the first error to be encountered
|
||||
});
|
13
tests/Final/Q3 Javascript/spec/support/jasmine.json
Normal file
13
tests/Final/Q3 Javascript/spec/support/jasmine.json
Normal file
@ -0,0 +1,13 @@
|
||||
{
|
||||
"spec_dir": "spec",
|
||||
"spec_files": [
|
||||
"**/*[sS]pec.?(m)js"
|
||||
],
|
||||
"helpers": [
|
||||
"helpers/**/*.?(m)js"
|
||||
],
|
||||
"env": {
|
||||
"stopSpecOnExpectationFailure": false,
|
||||
"random": true
|
||||
}
|
||||
}
|
71
tests/Final/tests.txt
Normal file
71
tests/Final/tests.txt
Normal file
@ -0,0 +1,71 @@
|
||||
;; 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)
|
Loading…
Reference in New Issue
Block a user