diff options
author | dharcourt@chromium.org <dharcourt@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-10-23 04:35:39 +0000 |
---|---|---|
committer | dharcourt@chromium.org <dharcourt@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-10-23 04:35:39 +0000 |
commit | a0100c9d99acc92d6801f8d113873898e004e4d9 (patch) | |
tree | 3342514a4e05aa012bad59de485b35836a65e837 | |
parent | 458e9521995681381912ece90eb2a8ea210ae07d (diff) | |
download | chromium_src-a0100c9d99acc92d6801f8d113873898e004e4d9.zip chromium_src-a0100c9d99acc92d6801f8d113873898e004e4d9.tar.gz chromium_src-a0100c9d99acc92d6801f8d113873898e004e4d9.tar.bz2 |
Fixed 31 / 3 failure in Calculator default app.
A problem with the rounding logic was causing any operation whose result
was a decimal larger than 10 with more than 7 digits after the decimal
to fail. (31 / 3) is one such operation. This was fixed and a test was
added for this case. Comments were also added to the tests to describing
some other test cases that need to be tested for and/or fixed. The
simplest fix to the rounding problem involved adding support for the
display of exponential values, which will probably fix a couple of
aspects of http://crbug/156453.
BUG=156448
BUG=156453
Review URL: https://chromiumcodereview.appspot.com/11183051
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@163504 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | chrome/common/extensions/docs/examples/apps/calculator/app/model.js | 28 | ||||
-rw-r--r-- | chrome/common/extensions/docs/examples/apps/calculator/tests/model_tests.js | 89 |
2 files changed, 108 insertions, 9 deletions
diff --git a/chrome/common/extensions/docs/examples/apps/calculator/app/model.js b/chrome/common/extensions/docs/examples/apps/calculator/app/model.js index 295a297..ba7a258 100644 --- a/chrome/common/extensions/docs/examples/apps/calculator/app/model.js +++ b/chrome/common/extensions/docs/examples/apps/calculator/app/model.js @@ -111,12 +111,32 @@ Model.prototype.calculate_ = function(operator, operand) { /** * Returns the string representation of the passed in value rounded to the - * model's precision, or "+Infinity" or "-Infinity" on overflow. + * model's precision, or "E" on overflow. * * @private */ Model.prototype.round_ = function(x) { - var rounded = String(Number(x.toFixed(this.precision - 1))); - var overflow = (rounded.replace(/[^0-9]/g, '').length > this.precision); - return (overflow || Math.abs(x) == Infinity) ? 'E' : rounded; + var exponent = Number(x.toExponential(this.precision - 1).split('e')[1]); + var digits = this.digits_(exponent); + var exponential = x.toExponential(digits).replace(/\.?0+e/, 'e'); + var fixed = (Math.abs(exponent) < this.precision && exponent > -7); + return !digits ? 'E' : fixed ? String(Number(exponential)) : exponential; +} + +/** + * Returns the appropriate number of digits to include of a number based on + * its size. + * + * @private + */ +Model.prototype.digits_ = function(exponent) { + return (isNaN(exponent) || exponent < -199 || exponent > 199) ? 0 : + (exponent < -99) ? (this.precision - 1 - 5) : + (exponent < -9) ? (this.precision - 1 - 4) : + (exponent < -6) ? (this.precision - 1 - 3) : + (exponent < 0) ? (this.precision - 1 + exponent) : + (exponent < this.precision) ? (this.precision - 1) : + (exponent < 10) ? (this.precision - 1 - 3) : + (exponent < 100) ? (this.precision - 1 - 4) : + (this.precision - 1 - 5); } diff --git a/chrome/common/extensions/docs/examples/apps/calculator/tests/model_tests.js b/chrome/common/extensions/docs/examples/apps/calculator/tests/model_tests.js index b52fe19..1b447ed 100644 --- a/chrome/common/extensions/docs/examples/apps/calculator/tests/model_tests.js +++ b/chrome/common/extensions/docs/examples/apps/calculator/tests/model_tests.js @@ -10,11 +10,90 @@ window.runTests = function(log) { var run = window.calculatorTestRun.create(); - // TODO(dharcourt@chromium.org): Organize and beef up these tests. - // TODO(dharcourt@chromium.org): test.run("*", '~ = [-0]'); - // TODO(dharcourt@chromium.org): test.run("*", '~42 [[-42]]'); - // TODO(dharcourt@chromium.org): test.run("*", '1 / 3 * 3 = [[1]]'); - // TODO(dharcourt@chromium.org): Test {nega,posi}tive {under,over}flows. + // --------------------------------------------------------------------------- + // Test fixes for <http://crbug.com/156448>: + + run.test('Twenty eight can be divided by three', '28 / 3 = [9.33333333]'); + run.test('Twenty nine can be divided by three', '29 / 3 = [9.66666667]'); + run.test('Thirty can be divided by three', '30 / 3 = [10]'); + run.test('Thirty one can be divided by three', '31 / 3 = [10.3333333]'); + run.test('Thirty two can be divided by three', '32 / 3 = [10.6666667]'); + run.test('Thirty three can be divided by three', '33 / 3 = [11]'); + + // --------------------------------------------------------------------------- + // Test fixes for <http://crbug.com/156449>: + + // run.test('Equals without operator results in operand value', + // '123 = [123]'); + + // TODO(dharcourt): Test the display for the expected output. + // run.test('Successive operators replace each other.', + // '123 + - * / [123 / null] and no previous [* + *]'); + + // --------------------------------------------------------------------------- + // Test fixes for <http://crbug.com/156450>: + + // run.test('Operand can be erased and replaced', + // '123 + 456 < < < < 789 = [789]'); + + // TODO(dharcourt): Test the display for the expected output. + // run.test('Erase is ignored after equals.', '123 + 456 = < [579]'); + // run.test('Erase is ignored after zero result.', '123 - 123 = < [9]'); + + // TODO(dharcourt): Test the display for the expected output. + // run.test('Erasing an operand makes it blank.', + // '123 + 456 < < < [null + null]'); + + // --------------------------------------------------------------------------- + // Test fixes for <http://crbug.com/156451>: + + // run.test('Negation applies to zero', '~ [[-0]]'); + // run.test('Negation applies before input', '~ 123 = [[-123]]'); + // run.test('Negation applies after input is erased', + // '123 < < < ~ 456 [[-456]]'); + // run.test('Negation is preserved when input is erased', + // '123 ~ < < < 456 [[-456]]'); + // run.test('Negation supports small values', + // '0.0000001 ~ [[-0.0000001]]'); + + // run.test('Addition resets negated zeros', '~ + [null + [0]]'); + // run.test('Subtraction resets negated zeros', '~ - [null - [0]]'); + // run.test('Multiplication resets negated zeros', '~ * [null * [0]]'); + // run.test('Division resets negated zeros', '~ / [null / [0]]'); + // run.test('Equals resets negated zeros', '~ = [0 null [0]]'); + + // --------------------------------------------------------------------------- + // Test fixes for <http://crbug.com/156452>: + + // TODO(dharcourt): Test the display for the expected output. + // TODO(dharcourt): Make the test utilities support 'error'. + // run.test('Errors results are spelled out', '1 / 0 = [[error]]'); + // run.test('Addition treats errors as zero', + // '1 / 0 = [error] + [0 + [0]] 123 = [123]'); + // run.test('Subtraction treats errors as zero', + // '1 / 0 = [error] - [0 - [0]] 123 = [-123]'); + // run.test('Multiplication treats errors as zero', + // '1 / 0 = [error] * [0 * [0]] 123 = [0]'); + // run.test('Division treats errors as zero', + // '1 / 0 = [error] / [0 / [0]] 123 = [0]'); + // run.test('Equals treats errors as zero', + // '1 / 0 = [error] = [0]'); + + // --------------------------------------------------------------------------- + // Test fixes for <http://crbug.com/156453>: + + // run.test('Common operations are reversible', + // '1 / 3 * 3 = [[1]]'); + + // run.test('Large numbers are displayed as exponentials', + // '12345678 * 10 = [[1.23456e8]]'); + // run.test('Small numbers are displayed as exponentials', + // '0.0000001 / 10 = [[1e-8]]'); + + // --------------------------------------------------------------------------- + // Other tests. + // TODO(dharcourt): Organize and beef up these tests. + // TODO(dharcourt): Test {nega,posi}tive {under,over}flows. run.test("Initialization", function(model) { run.verify(null, model.accumulator, 'Accumulator'); |