# Khan/khan-exercises

Add matrix transpose test and update other tests to use 'equal' inste…

```…ad of 'equals' to work with qunit 2009+

Summary: Our qunit tests are um, out of date.

Test Plan: Tested locally

Reviewers: eater, alpert

Reviewed By: alpert

1 parent cee3c85 commit 6714c746ba84a97bcf8d5fc75785644c4a4857c2 Stephanie H. Chang committed Jan 24, 2013
Showing with 208 additions and 170 deletions.
1. +1 −1 test/qunit
2. +20 −20 utils/test/calculus.js
3. +52 −52 utils/test/expressions.js
4. +2 −0 utils/test/index.html
5. +23 −23 utils/test/math.js
6. +36 −0 utils/test/matrix.js
7. +9 −9 utils/test/polynomials.js
8. +65 −65 utils/test/tmpl.js
Submodule qunit updated from bd6a75 to 515de7
 @@ -15,8 +15,8 @@ neg_coefs[1] = 6; neg_coefs[2] =-2; test("ddxPolynomial - Differentiate Polynomials", function() { - equals((KhanUtil.ddxPolynomial(new KhanUtil.Polynomial(2, 5, coefs, "x", null))).toString(), "25x^{4}+16x^{3}+9x^{2}+4x", "differentiate 5x^{5}+4x^{4}+3x^{3}+2x^{2}" ); - equals((KhanUtil.ddxPolynomial(new KhanUtil.Polynomial(-1, 2, neg_coefs, "x", null))).toString(), "-4x+6+x^{-2}", "differentiate -2x^{2}+6x+4-x^{-1}" ); + equal((KhanUtil.ddxPolynomial(new KhanUtil.Polynomial(2, 5, coefs, "x", null))).toString(), "25x^{4}+16x^{3}+9x^{2}+4x", "differentiate 5x^{5}+4x^{4}+3x^{3}+2x^{2}" ); + equal((KhanUtil.ddxPolynomial(new KhanUtil.Polynomial(-1, 2, neg_coefs, "x", null))).toString(), "-4x+6+x^{-2}", "differentiate -2x^{2}+6x+4-x^{-1}" ); }); test("PowerRule - helper object for polynomial differentiation", function(){ @@ -26,8 +26,8 @@ test("PowerRule - helper object for polynomial differentiation", function(){ ok(powerRule.ddxFText, "null constructor produces a displayable differentiated function"); powerRule = new KhanUtil.PowerRule(2, 5, coefs, "x"); - equals(powerRule.fText,"5x^{5}+4x^{4}+3x^{3}+2x^{2}","check it correctly converts polynomial to LaTeX"); - equals(powerRule.ddxFText,"25x^{4}+16x^{3}+9x^{2}+4x", "check it correctly converts the differential of the polynomial to LaTeX" ); + equal(powerRule.fText,"5x^{5}+4x^{4}+3x^{3}+2x^{2}","check it correctly converts polynomial to LaTeX"); + equal(powerRule.ddxFText,"25x^{4}+16x^{3}+9x^{2}+4x", "check it correctly converts the differential of the polynomial to LaTeX" ); for (var index in powerRule.wrongsText){ notEqual(powerRule.wrongsText[index],powerRule.ddxFText,"none of the wrong answers should match the right one"); @@ -38,30 +38,30 @@ test("PowerRule - helper object for polynomial differentiation", function(){ test ( "Hints for PowerRule", function(){ var powerRule = new KhanUtil.PowerRule ( -1, 2, neg_coefs, "x", KhanUtil.funcNotation ( "x", 1 ) ); //-2x^{2}+6x+4-x^{-1} - equals ( powerRule.hints.length, 4 ); - equals ( powerRule.hints[0], "f'(-2x^{2}) = 2 \\cdot -2x^{2-1} = -4x" ); - equals ( powerRule.hints[1], "f'(6x) = 1 \\cdot 6x^{1-1} = 6" ); - equals ( powerRule.hints[2], "f'(4) = 0 \\cdot 4x^{0-1} = 0" ); - equals ( powerRule.hints[3], "f'(-x^{-1}) = -1 \\cdot -1x^{-1-1} = x^{-2}"); + equal ( powerRule.hints.length, 4 ); + equal ( powerRule.hints[0], "f'(-2x^{2}) = 2 \\cdot -2x^{2-1} = -4x" ); + equal ( powerRule.hints[1], "f'(6x) = 1 \\cdot 6x^{1-1} = 6" ); + equal ( powerRule.hints[2], "f'(4) = 0 \\cdot 4x^{0-1} = 0" ); + equal ( powerRule.hints[3], "f'(-x^{-1}) = -1 \\cdot -1x^{-1-1} = x^{-2}"); powerRule = new KhanUtil.PowerRule ( -1, 2, neg_coefs, "x", KhanUtil.funcNotation ( "x", 4 ) ); //-2x^{2}+6x+4-x^{-1} - equals ( powerRule.hints.length, 4 ); - equals ( powerRule.hints[0], "f(x)=-2x^{2} \\implies \\frac{d}{dx}f(x) = 2 \\cdot -2x^{2-1} = -4x" ); + equal ( powerRule.hints.length, 4 ); + equal ( powerRule.hints[0], "f(x)=-2x^{2} \\implies \\frac{d}{dx}f(x) = 2 \\cdot -2x^{2-1} = -4x" ); }); test("funcNotation - helper for randomly choosing a notation for the function", function(){ ok(KhanUtil.funcNotation().f, "generates a notation for the function"); ok(KhanUtil.funcNotation().ddxF, "generates a notation for the function derivative"); - equals(KhanUtil.funcNotation("x",1).f, "f(x)","index works and variable is substituted"); - equals(KhanUtil.funcNotation("x",1).ddxF,"f'(x)","index works and variable is substituted"); + equal(KhanUtil.funcNotation("x",1).f, "f(x)","index works and variable is substituted"); + equal(KhanUtil.funcNotation("x",1).ddxF,"f'(x)","index works and variable is substituted"); ok(KhanUtil.funcNotation("x",1000).f,"randomly choose a notation if out of range"); - equals(KhanUtil.funcNotation("x",0).diffHint,"y=Ax^{n} \\implies \\frac{dy}{dx}=n \\cdot Ax^{n-1}", "check diffHint"); - equals(KhanUtil.funcNotation("b",1).diffHint,"f'(Ab^{n})=n \\cdot Ab^{n-1}","check diffHint"); - equals(KhanUtil.funcNotation("x",2).diffHint,"g'(Ax^{n})=n \\cdot Ax^{n-1}","check diffHint"); - equals(KhanUtil.funcNotation("b",3).diffHint,"y=Ab^{n} \\implies y'=n \\cdot Ab^{n-1}","check diffHint"); - equals(KhanUtil.funcNotation("x",4).diffHint,"f(x)=Ax^{n} \\implies \\frac{d}{dx}f(x)=n \\cdot Ax^{n-1}","check diffHint"); - equals(KhanUtil.funcNotation("b",5).diffHint,"a=Ab^{n} \\implies a'=n \\cdot Ab^{n-1}","check diffHint"); - equals(KhanUtil.funcNotation("x",6).diffHint,"a=Ax^{n} \\implies \\frac{da}{dx}=n \\cdot Ax^{n-1}","check diffHint"); + equal(KhanUtil.funcNotation("x",0).diffHint,"y=Ax^{n} \\implies \\frac{dy}{dx}=n \\cdot Ax^{n-1}", "check diffHint"); + equal(KhanUtil.funcNotation("b",1).diffHint,"f'(Ab^{n})=n \\cdot Ab^{n-1}","check diffHint"); + equal(KhanUtil.funcNotation("x",2).diffHint,"g'(Ax^{n})=n \\cdot Ax^{n-1}","check diffHint"); + equal(KhanUtil.funcNotation("b",3).diffHint,"y=Ab^{n} \\implies y'=n \\cdot Ab^{n-1}","check diffHint"); + equal(KhanUtil.funcNotation("x",4).diffHint,"f(x)=Ax^{n} \\implies \\frac{d}{dx}f(x)=n \\cdot Ax^{n-1}","check diffHint"); + equal(KhanUtil.funcNotation("b",5).diffHint,"a=Ab^{n} \\implies a'=n \\cdot Ab^{n-1}","check diffHint"); + equal(KhanUtil.funcNotation("x",6).diffHint,"a=Ax^{n} \\implies \\frac{da}{dx}=n \\cdot Ax^{n-1}","check diffHint"); }); })();
 @@ -7,81 +7,81 @@ var exprStripColor = KhanUtil.exprStripColor; var exprSimplifyAssociative = KhanUtil.exprSimplifyAssociative; test( "Expression formatter", function() { - equals( expr([ "-", 1 ]), "-1", "-1"); - equals( expr([ "+", 1, 2 ]), "1+2", "1 + 2" ); - equals( expr([ "+", "elephant", "potato" ]), "elephant+potato", "random strings" ); + equal( expr([ "-", 1 ]), "-1", "-1"); + equal( expr([ "+", 1, 2 ]), "1+2", "1 + 2" ); + equal( expr([ "+", "elephant", "potato" ]), "elephant+potato", "random strings" ); - equals( expr([ "-", 1, 2 ]), "1-2", "1 - 2" ); - equals( expr([ "-", 1, 2, 3, -4 ]), "1-2-3-(-4)", "1-2-3-(-4)" ); - equals( expr([ "-", 1 ]), "-1", "-1" ); - equals( expr([ "-", -1 ]), "-(-1)", "-(-1)" ); - equals( expr([ "-", 2 ]), "-2", "-2" ); - equals( expr([ "-", -2 ]), "-(-2)", "-(-2)" ); + equal( expr([ "-", 1, 2 ]), "1-2", "1 - 2" ); + equal( expr([ "-", 1, 2, 3, -4 ]), "1-2-3-(-4)", "1-2-3-(-4)" ); + equal( expr([ "-", 1 ]), "-1", "-1" ); + equal( expr([ "-", -1 ]), "-(-1)", "-(-1)" ); + equal( expr([ "-", 2 ]), "-2", "-2" ); + equal( expr([ "-", -2 ]), "-(-2)", "-(-2)" ); - equals( expr([ "*", "x", "y" ]), "xy", "x * y" ); - equals( expr([ "*", 2, 4 ]), "(2)(4)", "2 * 4" ); - equals( expr([ "*", 2, 4, "x" ]), "(2)(4)(x)", "2 * 4 * x" ); + equal( expr([ "*", "x", "y" ]), "xy", "x * y" ); + equal( expr([ "*", 2, 4 ]), "(2)(4)", "2 * 4" ); + equal( expr([ "*", 2, 4, "x" ]), "(2)(4)(x)", "2 * 4 * x" ); - equals( expr([ "*", 2, ["^", 4, 2 ] ]), "2(4^{2})", "2 * 4^2" ); - equals( expr([ "*", 2, ["^", 0, 2 ] ]), "2(0^{2})", "2 * 0^2" ); - equals( expr([ "*", 2, ["^", -3, 2 ] ]), "2(-3)^{2}", "2 * (-3)^2" ); + equal( expr([ "*", 2, ["^", 4, 2 ] ]), "2(4^{2})", "2 * 4^2" ); + equal( expr([ "*", 2, ["^", 0, 2 ] ]), "2(0^{2})", "2 * 0^2" ); + equal( expr([ "*", 2, ["^", -3, 2 ] ]), "2(-3)^{2}", "2 * (-3)^2" ); - equals( expr([ "/", 5, 3 ]), "5/3", "5 / 3" ); + equal( expr([ "/", 5, 3 ]), "5/3", "5 / 3" ); - equals( expr([ "^", "x", 2 ]), "x^{2}", "x^2" ); - equals( expr([ "^", [ "*", "x", "y" ], 2 ]), "(xy)^{2}", "(xy)^2" ); - equals( expr([ "^", [ "*", "x", "y" ], [ "+", 2, 3 ] ]), "(xy)^{2+3}", "(xy)^{2+3}" ); + equal( expr([ "^", "x", 2 ]), "x^{2}", "x^2" ); + equal( expr([ "^", [ "*", "x", "y" ], 2 ]), "(xy)^{2}", "(xy)^2" ); + equal( expr([ "^", [ "*", "x", "y" ], [ "+", 2, 3 ] ]), "(xy)^{2+3}", "(xy)^{2+3}" ); - equals( expr([ "sin", "x" ]), "\\sin{x}", "sin x" ); - equals( expr([ "sin", [ "*", "x", "y" ] ]), "\\sin{(xy)}", "sin xy" ); - equals( expr([ "sin", [ "+", "x", "y" ] ]), "\\sin{(x+y)}", "sin(x + y)" ); + equal( expr([ "sin", "x" ]), "\\sin{x}", "sin x" ); + equal( expr([ "sin", [ "*", "x", "y" ] ]), "\\sin{(xy)}", "sin xy" ); + equal( expr([ "sin", [ "+", "x", "y" ] ]), "\\sin{(x+y)}", "sin(x + y)" ); - equals( expr([ "*", 2, [ "sqrt", 5 ] ]), "2\\sqrt{5}", "2 sqrt(5)" ); - equals( expr([ "*", [ "+", "w", "x" ], "y" ]), "(w+x)(y)", "(w + x) * y" ); + equal( expr([ "*", 2, [ "sqrt", 5 ] ]), "2\\sqrt{5}", "2 sqrt(5)" ); + equal( expr([ "*", [ "+", "w", "x" ], "y" ]), "(w+x)(y)", "(w + x) * y" ); - equals( expr([ "+-", "x" ]), "\\pm x", "+- x" ); - equals( expr([ "+-", "x", "y" ]), "x \\pm y", "x +- y" ); - equals( expr([ "+-", [ "+", "x", "y" ] ]), "\\pm (x+y)", "x +- y" ); + equal( expr([ "+-", "x" ]), "\\pm x", "+- x" ); + equal( expr([ "+-", "x", "y" ]), "x \\pm y", "x +- y" ); + equal( expr([ "+-", [ "+", "x", "y" ] ]), "\\pm (x+y)", "x +- y" ); - equals( expr([ "+", [ "*", 2, [ "^", 3, 2 ] ], [ "*", -3, 3 ], 4 ]), "2(3^{2})+(-3)(3)+4", "issue 90" ); - equals( expr([ "+", [ "*", 2, [ "^", 3, "x" ] ], [ "*", -3, "x" ], 4 ]), "2(3^{x})-3x+4", "issue 90" ); - equals( expr([ "*", -2, [ "^", "x", 2 ] ]), "-2x^{2}", "polynomial term" ); + equal( expr([ "+", [ "*", 2, [ "^", 3, 2 ] ], [ "*", -3, 3 ], 4 ]), "2(3^{2})+(-3)(3)+4", "issue 90" ); + equal( expr([ "+", [ "*", 2, [ "^", 3, "x" ] ], [ "*", -3, "x" ], 4 ]), "2(3^{x})-3x+4", "issue 90" ); + equal( expr([ "*", -2, [ "^", "x", 2 ] ]), "-2x^{2}", "polynomial term" ); - equals( expr([ "-", [ "+", 1, 2 ] ]), "-(1+2)", "-1*(1+2)" ); - equals( expr([ "-", [ "+", 1, -2 ] ]), "-(1-2)", "-1*(1-2)" ); - equals( expr([ "*", 3, [ "+", 1, -2 ], 4 ]), "3(1-2)(4)", "3 * (1-2) * 4" ); - equals( expr([ "*", 3, [ "-", 1, -2 ], 4 ]), "3(1-(-2))(4)", "3 * (1-(-2)) * 4" ); - equals( expr([ "+", 1, [ "-", [ "*", 2, 3, 4 ] ], 5, 6 ]), "1-(2)(3)(4)+5+6", "1-(2)(3)(4)+5+6" ); + equal( expr([ "-", [ "+", 1, 2 ] ]), "-(1+2)", "-1*(1+2)" ); + equal( expr([ "-", [ "+", 1, -2 ] ]), "-(1-2)", "-1*(1-2)" ); + equal( expr([ "*", 3, [ "+", 1, -2 ], 4 ]), "3(1-2)(4)", "3 * (1-2) * 4" ); + equal( expr([ "*", 3, [ "-", 1, -2 ], 4 ]), "3(1-(-2))(4)", "3 * (1-(-2)) * 4" ); + equal( expr([ "+", 1, [ "-", [ "*", 2, 3, 4 ] ], 5, 6 ]), "1-(2)(3)(4)+5+6", "1-(2)(3)(4)+5+6" ); // Test colors - equals( expr([ "*", 4, [ "+", 2, [ "color", "blue", 2 ] ] ]), "4(2+\\color{blue}{2})", "4(2+\\color{blue}{2})" ); - equals( expr([ "*", 4, [ "color", "blue", 2 ] ]), "(4)(\\color{blue}{2})", "(4)(\\color{blue}{2})" ); + equal( expr([ "*", 4, [ "+", 2, [ "color", "blue", 2 ] ] ]), "4(2+\\color{blue}{2})", "4(2+\\color{blue}{2})" ); + equal( expr([ "*", 4, [ "color", "blue", 2 ] ]), "(4)(\\color{blue}{2})", "(4)(\\color{blue}{2})" ); }); test( "Expression evaluator", function() { - equals( expr([ "+", 2, 4 ], true ), 6, "2 + 4" ); - equals( expr([ "*", 2, 4 ], true ), 8, "2 * 4" ); - equals( expr([ "-", 2, 4 ], true ), -2, "2 - 4" ); - equals( expr([ "/", 2, 4 ], true ), 0.5, "2 / 4" ); - equals( expr([ "^", 2, 4 ], true ), 16, "2 ^ 4" ); - equals( expr([ "frac", 2, 4 ], true ), 0.5, "2 `frac` 4" ); - equals( expr([ "sqrt", 65536 ], true ), 256, "sqrt 65536" ); - equals( expr([ "+", [ "*", 2, 4 ], 6 ], true ), 14, "2 * 4 + 6" ); + equal( expr([ "+", 2, 4 ], true ), 6, "2 + 4" ); + equal( expr([ "*", 2, 4 ], true ), 8, "2 * 4" ); + equal( expr([ "-", 2, 4 ], true ), -2, "2 - 4" ); + equal( expr([ "/", 2, 4 ], true ), 0.5, "2 / 4" ); + equal( expr([ "^", 2, 4 ], true ), 16, "2 ^ 4" ); + equal( expr([ "frac", 2, 4 ], true ), 0.5, "2 `frac` 4" ); + equal( expr([ "sqrt", 65536 ], true ), 256, "sqrt 65536" ); + equal( expr([ "+", [ "*", 2, 4 ], 6 ], true ), 14, "2 * 4 + 6" ); // Test colors - equals( expr([ "*", 4, [ "+", 2, [ "color", "blue", 2 ] ] ], true), 16, "4*(2+\\color{blue}{2})" ); - equals( expr([ "*", 4, [ "color", "blue", 2 ] ], true), 8, "(4)(\\color{blue}{2})" ); + equal( expr([ "*", 4, [ "+", 2, [ "color", "blue", 2 ] ] ], true), 16, "4*(2+\\color{blue}{2})" ); + equal( expr([ "*", 4, [ "color", "blue", 2 ] ], true), 8, "(4)(\\color{blue}{2})" ); }); test( "Expression utilities", function() { //remove colors - equals( expr(exprStripColor([ "color", "green", 17 ])), "17", "color outside" ); - equals( expr(exprStripColor([ "*", 4, [ "+", 2, [ "color", "blue", 2 ] ] ])), "4(2+2)", "color inside" ); + equal( expr(exprStripColor([ "color", "green", 17 ])), "17", "color outside" ); + equal( expr(exprStripColor([ "*", 4, [ "+", 2, [ "color", "blue", 2 ] ] ])), "4(2+2)", "color inside" ); //simplify an expression - equals( expr(exprSimplifyAssociative([ "+", 1, [ "+", [ "+", 2, 3 ], 4 ] ])), "1+2+3+4", "Simplify 1+((2+3)+4)" ); - equals( expr(exprSimplifyAssociative([ "*", [ "*", [ "*", 2, 3 ], 4 ], 5 ])), "(2)(3)(4)(5)", "Simplify ((2*3)*4)*5" ); - equals( expr(exprSimplifyAssociative([ "*", [ "*", [ "*", [ "+", 1, [ "+", [ "+", 2, 3, [ "*", [ "*", [ "*", 2, 3 ], 4 ], 5 ] ], 4 ] ], 3 ], 4 ], 5 ])), "(1+2+3+(2)(3)(4)(5)+4)(3)(4)(5)", "Simplify alternating multiplication and addition" ); + equal( expr(exprSimplifyAssociative([ "+", 1, [ "+", [ "+", 2, 3 ], 4 ] ])), "1+2+3+4", "Simplify 1+((2+3)+4)" ); + equal( expr(exprSimplifyAssociative([ "*", [ "*", [ "*", 2, 3 ], 4 ], 5 ])), "(2)(3)(4)(5)", "Simplify ((2*3)*4)*5" ); + equal( expr(exprSimplifyAssociative([ "*", [ "*", [ "*", [ "+", 1, [ "+", [ "+", 2, 3, [ "*", [ "*", [ "*", 2, 3 ], 4 ], 5 ] ], 4 ] ], 3 ], 4 ], 5 ])), "(1+2+3+(2)(3)(4)(5)+4)(3)(4)(5)", "Simplify alternating multiplication and addition" ); }); })();
 @@ -44,6 +44,8 @@ + +

Utility Test Suite

 @@ -6,31 +6,31 @@ test( "math miscellanea", 44, function() { deepEqual( KhanUtil.digits(376), [ 6, 7, 3 ], "digits(376)" ); deepEqual( KhanUtil.integerToDigits(376), [ 3, 7, 6 ], "integerToDigits(376)" ); - equals( KhanUtil.getGCD(216, 1024), 8, "gcd(216, 1024)" ); - equals( KhanUtil.getGCD(512341, 2325183), 1, "gcd(512341, 2325183)" ); - equals( KhanUtil.getGCD(53110108, 109775188), 68, "gcd(53110108, 109775188)" ); - equals( KhanUtil.getGCD(-21, 14), 7, "gcd(-21, 14)" ); - equals( KhanUtil.getGCD(-21, -14), 7, "gcd(-21, -14)" ); - equals( KhanUtil.getGCD(123, 1), 1, "gcd(123, 1)" ); - equals( KhanUtil.getGCD(123, 1), 1, "gcd(123, 1)" ); - equals( KhanUtil.getGCD(123, 123), 123, "gcd(123, 123)" ); - equals( KhanUtil.getGCD(169, 26, -52), 13, "gcd(169, 26, -52)" ); + equal( KhanUtil.getGCD(216, 1024), 8, "gcd(216, 1024)" ); + equal( KhanUtil.getGCD(512341, 2325183), 1, "gcd(512341, 2325183)" ); + equal( KhanUtil.getGCD(53110108, 109775188), 68, "gcd(53110108, 109775188)" ); + equal( KhanUtil.getGCD(-21, 14), 7, "gcd(-21, 14)" ); + equal( KhanUtil.getGCD(-21, -14), 7, "gcd(-21, -14)" ); + equal( KhanUtil.getGCD(123, 1), 1, "gcd(123, 1)" ); + equal( KhanUtil.getGCD(123, 1), 1, "gcd(123, 1)" ); + equal( KhanUtil.getGCD(123, 123), 123, "gcd(123, 123)" ); + equal( KhanUtil.getGCD(169, 26, -52), 13, "gcd(169, 26, -52)" ); - equals( KhanUtil.getLCM(216, 1024), 27648, "lcm(216, 1024)" ); - equals( KhanUtil.getLCM(216, -1024), 27648, "lcm(216, -1024)" ); - equals( KhanUtil.getLCM(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), 2520, "lcm(1..10)" ); + equal( KhanUtil.getLCM(216, 1024), 27648, "lcm(216, 1024)" ); + equal( KhanUtil.getLCM(216, -1024), 27648, "lcm(216, -1024)" ); + equal( KhanUtil.getLCM(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), 2520, "lcm(1..10)" ); - equals( KhanUtil.isPrime(1), false, "primeq 1" ); - equals( KhanUtil.isPrime(2), true, "primeq 2" ); - equals( KhanUtil.isPrime(216), false, "primeq 216" ); - equals( KhanUtil.isPrime(127), true, "primeq 127" ); - equals( KhanUtil.isPrime(129), false, "primeq 129" ); + equal( KhanUtil.isPrime(1), false, "primeq 1" ); + equal( KhanUtil.isPrime(2), true, "primeq 2" ); + equal( KhanUtil.isPrime(216), false, "primeq 216" ); + equal( KhanUtil.isPrime(127), true, "primeq 127" ); + equal( KhanUtil.isPrime(129), false, "primeq 129" ); - equals( KhanUtil.isOdd(0), false, "oddq 0" ); - equals( KhanUtil.isOdd(1), true, "oddq 1" ); + equal( KhanUtil.isOdd(0), false, "oddq 0" ); + equal( KhanUtil.isOdd(1), true, "oddq 1" ); - equals( KhanUtil.isEven(0), true, "evenq 0" ); - equals( KhanUtil.isEven(1), false, "evenq 1" ); + equal( KhanUtil.isEven(0), true, "evenq 0" ); + equal( KhanUtil.isEven(1), false, "evenq 1" ); deepEqual( KhanUtil.getPrimeFactorization( 6 ), [ 2, 3 ], "factor 6" ); deepEqual( KhanUtil.getPrimeFactorization( 23 ), [ 23 ], "factor 23" ); @@ -46,8 +46,8 @@ test( "math miscellanea", 44, function() { deepEqual( KhanUtil.getMultiples( 7, 80 ), [ 7, 14, 21, 28, 35, 42, 49, 56, 63, 70, 77 ], "multiples 7, 80" ); deepEqual( KhanUtil.getMultiples( 7, 83 ), [ 7, 14, 21, 28, 35, 42, 49, 56, 63, 70, 77 ], "multiples 7, 83" ); - equals( KhanUtil.roundTo( 2, Math.PI ), 3.14, "roundTo 2, pi" ); - equals( KhanUtil.roundTo( 0, Math.PI ), 3, "roundTo 0, pi" ); + equal( KhanUtil.roundTo( 2, Math.PI ), 3.14, "roundTo 2, pi" ); + equal( KhanUtil.roundTo( 0, Math.PI ), 3, "roundTo 0, pi" ); deepEqual( KhanUtil.toFraction( 4/8 ), [ 1, 2 ], "4/8" ); deepEqual( KhanUtil.toFraction( 0.666 ), [ 333, 500 ], "0.666" );
 @@ -0,0 +1,36 @@ +module("matrix"); + +(function() { + +test("matrixTranspose", function() { + + function matrixToString(m) { + return m.join("; "); + } + + function testMatrixTranspose(m, text) { + + m = KhanUtil.makeMatrix(m); + var n = KhanUtil.matrixTranspose(m); + var d = KhanUtil.matrixTranspose(n); + + var ms = matrixToString(m); + if (text) { + ms = ms + ": " + text; + } + + deepEqual(m, d, ms); + } + + testMatrixTranspose([[1, 2, 3, 4]]); + testMatrixTranspose([[1,2],[3,4]]); + + testMatrixTranspose([[1],[2],[3,4]], "ill-formatted"); + testMatrixTranspose([[1], [2], [3], [4]]); + + testMatrixTranspose([[1, 2], [3, 4], [5, 6]]); + testMatrixTranspose([[1, 2, 3], [4, 5, 6]]); + testMatrixTranspose([[1, 2, 3], [4, 5, 6], [7, 8, 9]]); +}); + +})();
 @@ -12,23 +12,23 @@ polyCoefs[2] = -3; polyCoefs[4] = 2; test("Polynomial constructor defaults", function(){ - equals((new KhanUtil.Polynomial(-1, 4, polyCoefs)).toString(),"2x^{4}-3x^{2}+12x+5+3x^{-1}", "defaults variable name to x"); + equal((new KhanUtil.Polynomial(-1, 4, polyCoefs)).toString(),"2x^{4}-3x^{2}+12x+5+3x^{-1}", "defaults variable name to x"); ok((new KhanUtil.Polynomial(-1, 4)).toString(), "randomly generate coefs (3rd param) if not passed"); - equals(new KhanUtil.Polynomial(-1, 4, polyCoefs).getNumberOfTerms(),5,"should only have 5 terms as no 3 coef"); + equal(new KhanUtil.Polynomial(-1, 4, polyCoefs).getNumberOfTerms(),5,"should only have 5 terms as no 3 coef"); }); test("Polynomial evalOf", function(){ - equals((new KhanUtil.Polynomial(-1, 4, polyCoefs, polyX)).evalOf(1),19,"2*1^4-3*1^2+12*1+5+3*1^-1 = 2-3+12+5+3 = 19"); + equal((new KhanUtil.Polynomial(-1, 4, polyCoefs, polyX)).evalOf(1),19,"2*1^4-3*1^2+12*1+5+3*1^-1 = 2-3+12+5+3 = 19"); }); test( "Polynomial extractFromExpr", function(){ var polynomial = new KhanUtil.Polynomial(-1, 4, polyCoefs); - equals(polynomial.getCoefAndDegreeForTerm( 0 ).coef,2,"leading term is 2x^4"); - equals(polynomial.getCoefAndDegreeForTerm( 0 ).degree,4,"leading term is 2x^4"); - equals(polynomial.getCoefAndDegreeForTerm( 1 ).coef,-3,"second term is -3x^2"); - equals(polynomial.getCoefAndDegreeForTerm( 1 ).degree,2,"second term is -3x^2"); - equals(polynomial.getCoefAndDegreeForTerm( 2 ).coef,12,"third term is 12x"); - equals(polynomial.getCoefAndDegreeForTerm( 2 ).degree,1,"third term is 12x"); + equal(polynomial.getCoefAndDegreeForTerm( 0 ).coef,2,"leading term is 2x^4"); + equal(polynomial.getCoefAndDegreeForTerm( 0 ).degree,4,"leading term is 2x^4"); + equal(polynomial.getCoefAndDegreeForTerm( 1 ).coef,-3,"second term is -3x^2"); + equal(polynomial.getCoefAndDegreeForTerm( 1 ).degree,2,"second term is -3x^2"); + equal(polynomial.getCoefAndDegreeForTerm( 2 ).coef,12,"third term is 12x"); + equal(polynomial.getCoefAndDegreeForTerm( 2 ).degree,1,"third term is 12x"); }); })();