Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame^] | 1 | // Copyright 2009 the V8 project authors. All rights reserved. |
| 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | // Test fast div and mod. |
| 29 | |
| 30 | function divmod(div_func, mod_func, x, y) { |
| 31 | var div_answer = (div_func)(x); |
| 32 | assertEquals(x / y, div_answer, x + "/" + y); |
| 33 | var mod_answer = (mod_func)(x); |
| 34 | assertEquals(x % y, mod_answer, x + "%" + y); |
| 35 | var minus_div_answer = (div_func)(-x); |
| 36 | assertEquals(-x / y, minus_div_answer, "-" + x + "/" + y); |
| 37 | var minus_mod_answer = (mod_func)(-x); |
| 38 | assertEquals(-x % y, minus_mod_answer, "-" + x + "%" + y); |
| 39 | } |
| 40 | |
| 41 | |
| 42 | function run_tests_for(divisor) { |
| 43 | print("(function(left) { return left / " + divisor + "; })"); |
| 44 | var div_func = this.eval("(function(left) { return left / " + divisor + "; })"); |
| 45 | var mod_func = this.eval("(function(left) { return left % " + divisor + "; })"); |
| 46 | var exp; |
| 47 | // Strange number test. |
| 48 | divmod(div_func, mod_func, 0, divisor); |
| 49 | divmod(div_func, mod_func, 1 / 0, divisor); |
| 50 | // Floating point number test. |
| 51 | for (exp = -1024; exp <= 1024; exp += 8) { |
| 52 | divmod(div_func, mod_func, Math.pow(2, exp), divisor); |
| 53 | divmod(div_func, mod_func, 0.9999999 * Math.pow(2, exp), divisor); |
| 54 | divmod(div_func, mod_func, 1.0000001 * Math.pow(2, exp), divisor); |
| 55 | } |
| 56 | // Integer number test. |
| 57 | for (exp = 0; exp <= 32; exp++) { |
| 58 | divmod(div_func, mod_func, 1 << exp, divisor); |
| 59 | divmod(div_func, mod_func, (1 << exp) + 1, divisor); |
| 60 | divmod(div_func, mod_func, (1 << exp) - 1, divisor); |
| 61 | } |
| 62 | divmod(div_func, mod_func, Math.floor(0x1fffffff / 3), divisor); |
| 63 | divmod(div_func, mod_func, Math.floor(-0x20000000 / 3), divisor); |
| 64 | } |
| 65 | |
| 66 | |
| 67 | var divisors = [ |
| 68 | 0, |
| 69 | 1, |
| 70 | 2, |
| 71 | 3, |
| 72 | 4, |
| 73 | 5, |
| 74 | 6, |
| 75 | 7, |
| 76 | 8, |
| 77 | 9, |
| 78 | 10, |
| 79 | 0x1000000, |
| 80 | 0x40000000, |
| 81 | 12, |
| 82 | 60, |
| 83 | 100, |
| 84 | 1000 * 60 * 60 * 24]; |
| 85 | |
| 86 | for (var i = 0; i < divisors.length; i++) { |
| 87 | run_tests_for(divisors[i]); |
| 88 | } |