-
Notifications
You must be signed in to change notification settings - Fork 120
Comprehensive jsnum tests #1818
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ds26gte
wants to merge
59
commits into
brownplt:horizon
Choose a base branch
from
ds26gte:jsnum-tests
base: horizon
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
59 commits
Select commit
Hold shift + click to select a range
f3a3930
Added tests/jsnum-test.js to test methods in js-numbers.js that aren'…
ds26gte b414392
Merge branch 'fromFixnum-fix' into jsnum-tests
ds26gte a6485eb
- jsnums-test.js: enhance #1812
ds26gte 812e409
fromString(): Rational.makeInstance already takes care of den == 1
ds26gte a1837be
- makeNumericBinop tests
ds26gte f2d40dd
jsnums-tests.js: Add tests for Rational methods
ds26gte b8442a2
- more Rational.* tests
ds26gte 6e0e0ef
scrub unnecessary defs from jsnums-test.js
ds26gte 74efc26
add Roughnum.* tests
ds26gte bc4f135
- add arrayEquals to help test structural equality
ds26gte 6beef5f
- numerator, denominator methods take errbacks param
ds26gte 66122ae
Merge branch 'horizon' into jsnum-tests
ds26gte cc52770
jsnums-test.js: add test for gcd, lcm
ds26gte cb4e69e
- add tests for number predicates, sign, zfill, liftFixnumInteger
ds26gte 1b7a85e
add tests for number casts
ds26gte e791660
- add tests for nthRoot() and integerNthRoot() #1812
ds26gte 8c85b1f
- BigInteger canonicalizer bnpClamp() #1823
ds26gte 74b589c
- test that bignums indeed have unique representations
ds26gte 8d75cd8
tests for
ds26gte 3b9cad4
- test BigInteger's {copy,sub,multiply,{d,}{l,r}Shift}To
ds26gte cb5b948
test for bnp{Squareto,DivRemTo,Exp,IsEven,ModInt}
ds26gte 15914a9
- makeInteger{UnOp,Binop}: use & propagate errbacks appropriately
ds26gte 09a47af
test bnpToRadix
ds26gte d727abd
- ensure all calls to equals(), lessThan{,OrEqual}() take errbacks
ds26gte 0bdd499
test toRepeatingDecimal() for non-valid args
ds26gte 00b90b9
- add num-gcd courtesy @blerner #1427
ds26gte d3d00ad
remove all (commented) debugging console.log's
ds26gte 1ec7a95
jsnums-test.js: Add test for toRepeatingDecimal() mistakenly called with
ds26gte 7b63e0f
- lift def of getReside() up from toRepeatingDecimal()
ds26gte 2a22911
simplify toRepeatingDecimal(). Now that getResidue() is out,
ds26gte d58e838
jsnums-test.js: test toRepeatingDecimal errors, with and without corr…
ds26gte 298e87c
Following's defs & calls need explicit errbacks propagration:
ds26gte b952582
Following defined but unused -- identify for now, potentially remove …
ds26gte e962b78
Don't α-rename potential unusued functions
ds26gte e29c32b
`make test` should run tests/jsnums-test/jsnums-test.js
ds26gte 486dd9c
arrayEquals() not needed in jsnums-test.js
ds26gte 3c0f18f
function and method isInteger() call should take errbacks
ds26gte 66d11d2
- Ensure function and method toFixnum() calls take errbacks
ds26gte 1e5c790
Merge branch 'horizon' into jsnum-tests
ds26gte b8e6c6f
add tests that liftFixnumInteger on non-int fixnum produces valid rat…
ds26gte 0ddee45
- {Rational,BigInteger}.log() should take advantage of log() instead of
ds26gte d489ce1
add errbacks to stray add multiply subtract expt
ds26gte f2663ca
ensure negate() gets an errbacks arg
ds26gte 4e104da
ensure floor() calls get errbacks
ds26gte 8606db2
ensure numerator() calls get errbacks
ds26gte 70a7be6
ensure abs() sign() sqrt() get errbacks arg
ds26gte 2b840c3
ensure liftFixnumInteger() calls get errbacks arg
ds26gte 551610f
- isInteger() - don't take errbacks as it's standard JS!
ds26gte 79314f8
- define InternalCompilerErrorErrbacks for errbaks that shouldn't be …
ds26gte 3c6f202
- Function toRational() doesn't take errbacks but passes InternalComp…
ds26gte 605dee2
Function toRoughnum() doesn't take errbacks but passes InternalCompil…
ds26gte fbbccc3
throwInternalCompilerError() uses throw new Error()
ds26gte a95a176
- toFixnum() -- check if arg is (box)number before trying method
ds26gte 94d209d
- remove errbacks param from: bnpExp bnModPowInt bnPow bnModInverse
ds26gte 671d80e
No errbacks param for: abs equalsAnyZero floor ceiling round roundEven
ds26gte 38b91c6
Simplify defs of {greater,less}Than{,OrEqual}
ds26gte 89c8c37
Ensure sqr() calls take errbacks
ds26gte 0fac202
BitInteger.prototype.sqrt def doesn't need to be wrapped in an IIFE
ds26gte 190468d
Revert "Simplify defs of {greater,less}Than{,OrEqual}"
ds26gte File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| // To test: Build Pyret, then cd to this directory, and type | ||
| // node jsnums-test.js | ||
|
|
||
| var Jasmine = require('jasmine'); | ||
| var jazz = new Jasmine(); | ||
| const R = require("requirejs"); | ||
| var build = process.env["PHASE"] || "build/phaseA"; | ||
| R.config({ | ||
| waitSeconds: 15000, | ||
| paths: { | ||
| "trove": "../../" + build + "/trove", | ||
| "js": "../../" + build + "/js", | ||
| "compiler": "../../" + build + "/arr/compiler", | ||
| "jglr": "../../lib/jglr", | ||
| "pyret-base": "../../" + build | ||
| } | ||
| }); | ||
| R(["pyret-base/js/js-numbers"], function(JN) { | ||
| var sampleErrorBacks = { | ||
| throwDomainError: function() { throw 'domainError'; }, | ||
| throwLogNonPositive: function() { throw 'logNonPositive'; }, | ||
| }; | ||
| function test(actual, expected, testname, toks) { | ||
| if (actual === expected) { | ||
| return true; | ||
| } else { | ||
| var allToks = "Str was " + JSON.stringify(testname) + "\n"; | ||
| for (var t = 0; t < toks.length; t++) { | ||
| if (t > 0) allToks += "\n"; | ||
| allToks += "Tok[" + t + "] = " + toks[t].toString(true) | ||
| + " at pos " + toks[t].pos.toString(true); | ||
| } | ||
| allToks += "Expected " + JSON.stringify(expected) + ", but got " + JSON.stringify(actual) | ||
| + " in " + JSON.stringify(testname); | ||
| expect(allToks).toBe(""); | ||
| return false; | ||
| } | ||
| } | ||
| function testPos(tok, expected, str, toks) { | ||
| if (tok.pos.endChar - tok.pos.startChar == expected.length) { | ||
| return true; | ||
| } else { | ||
| test(str.slice(tok.pos.startChar, tok.pos.endChar), expected, str, toks); | ||
| return false; | ||
| } | ||
| } | ||
| describe("check functions that don't allow testing via Pyret programs", function() { | ||
|
|
||
| it("fromString", function() { | ||
| expect(JN.fromString("5", sampleErrorBacks)).toEqual(5); | ||
|
|
||
| var bigIntStr = "1" + new Array(309 + 1).join("0"); // 1 followed by 309 0s | ||
| expect(JN.fromString(bigIntStr, sampleErrorBacks)).toEqual(JN.makeBignum(bigIntStr)); | ||
|
|
||
| // console.log(JN.fromString("1e1", sampleErrorBacks)); | ||
| // console.log(JN.fromString("10", sampleErrorBacks)); | ||
| // console.log(JN.makeBignum("1e1", sampleErrorBacks)); | ||
| // console.log(JN.makeBignum("10", sampleErrorBacks).toFixnum()); | ||
|
|
||
| expect(JN.fromString("1e1", sampleErrorBacks)).toBe(10); | ||
| expect(JN.fromString("1e30", sampleErrorBacks)).toEqual(JN.makeBignum("1e30")); | ||
| expect(JN.fromString("1e140", sampleErrorBacks)).toEqual(JN.makeBignum("1e140")); | ||
|
|
||
| // for large bignums (> 1e140 ?), fromString() and makeBignum() can give structurally | ||
| // unequal results. so the following fail: | ||
| // expect(JN.fromString("1e141", sampleErrorBacks)).toEqual(JN.makeBignum("1e141")); | ||
| // expect(JN.fromString("1e307", sampleErrorBacks)).toEqual(JN.makeBignum("1e307")); | ||
| // expect(JN.fromString("1e309", sampleErrorBacks)).toEqual(JN.makeBignum("1e309")); | ||
|
|
||
| // but they're operationally equivalent! | ||
| expect(JN.equals(JN.fromString("1e141", sampleErrorBacks), | ||
| JN.makeBignum("1e141"), | ||
| sampleErrorBacks)).toBe(true); | ||
|
|
||
| // fromString() and makeBignum() give different but operationally same bignums | ||
| // console.log('*************************'); | ||
| // console.log(JN.fromString("1e307", sampleErrorBacks)); | ||
| // console.log('-------------------------'); | ||
| // console.log(JN.makeBignum("1e307")); | ||
| // console.log('-------------------------'); | ||
| // console.log(JN.multiply(1, JN.makeBignum("1e307"), sampleErrorBacks)['40']); | ||
| // console.log('*************************'); | ||
|
|
||
| expect(JN.fromString("1e311", sampleErrorBacks)).toEqual(JN.makeBignum("1e311")); | ||
| expect(JN.fromString("1/2", sampleErrorBacks)).toEqual(JN.makeRational(1, 2)); | ||
| expect(JN.fromString("355/113", sampleErrorBacks)).toEqual(JN.makeRational(355, 113)); | ||
| expect(JN.fromString("1.5e3", sampleErrorBacks)).toEqual(1500); | ||
| expect(JN.fromString("~2.718281828", sampleErrorBacks)).toEqual(JN.makeRoughnum(2.718281828)); | ||
| expect(JN.fromString("not-a-string", sampleErrorBacks)).toBe(false); | ||
|
|
||
| }); | ||
|
|
||
| it("fromFixnum", function() { | ||
|
|
||
| expect(JN.fromFixnum(5, sampleErrorBacks)).toEqual(5); | ||
| expect(JN.fromFixnum(1/2, sampleErrorBacks)).toEqual(JN.makeRational(1, 2)); | ||
| expect(JN.fromFixnum(1.5e3, sampleErrorBacks)).toEqual(1500); | ||
| expect(JN.fromFixnum(1e311, sampleErrorBacks)).toBe(false); | ||
|
|
||
| }); | ||
|
|
||
| it("bnpExp", function() { | ||
| // BigInteger.*.expt calls bnPow, wch calls bnpExp | ||
| // shd raise exc for too-large | ||
| expect(function() { JN.makeBignum(2).expt(JN.makeBignum(0xffffffff + 1), sampleErrorBacks); }).toThrow('domainError'); | ||
|
|
||
| // BigInteger.*.log | ||
| // shd raise exc for arg <= 0 | ||
| expect(function() { JN.makeBignum(-1).log(sampleErrorBacks); }).toThrow('logNonPositive'); | ||
| }); | ||
|
|
||
| it("arithmetic", function() { | ||
|
|
||
| }); | ||
|
|
||
| it("trig functions", function() { | ||
| // BigInteger.*asin | ||
| // shd raise exception for arg outside [-1, +1] | ||
| // but this is not testable via Pyret, because args are always sane | ||
| // by the time this method is called | ||
| expect(function() { JN.makeBignum(-1.5).asin(sampleErrorBacks); }).toThrow('domainError'); | ||
| expect(function() { JN.makeBignum(+1.5).asin(sampleErrorBacks); }).toThrow('domainError'); | ||
|
|
||
| // BigInteger.*acos | ||
| // shd raise exc for arg < -1 or > 1 | ||
| expect(function() { JN.makeBignum(-1.5).acos(sampleErrorBacks); }).toThrow('domainError'); | ||
| expect(function() { JN.makeBignum(+1.5).acos(sampleErrorBacks); }).toThrow('domainError'); | ||
|
|
||
| // BigInteger.*.atan | ||
| // should work | ||
| expect(JN.makeBignum(0).atan(sampleErrorBacks)).toEqual(0); | ||
|
|
||
| // atan2 (perhaps Pyret test is enough) | ||
| expect(function () { | ||
| JN.atan2(JN.makeBignum(0), JN.makeBignum(0), sampleErrorBacks); | ||
| }).toThrow('domainError'); | ||
|
|
||
| // BigInteger.*.sin | ||
| // should work | ||
| expect(JN.makeBignum(0).sin(sampleErrorBacks)).toEqual(0); | ||
|
|
||
| // BigInteger.*.cos | ||
| // should work | ||
| expect(JN.makeBignum(0).cos(sampleErrorBacks)).toEqual(1); | ||
|
|
||
| // BigInteger.*.tan | ||
| // should work | ||
| expect(JN.makeBignum(0).tan(sampleErrorBacks)).toEqual(0); | ||
|
|
||
|
|
||
| }); | ||
| }); | ||
|
|
||
| jazz.execute(); | ||
|
|
||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ds26gte why are these commented out? We should be testing to make sure they fail, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The idea is that they should not fail, but were. I kept them around so I could solve the underlying problem as to why they were failing.
Now that I've canonicalized bigint representation -- i.e, every bigint now has a only one representation, not multiple as before --, these tests no longer fail, and are now uncommented.