|
| 1 | +// Copyright 2013 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +// @dart = 2.10 |
| 6 | +import 'package:test/bootstrap/browser.dart'; |
| 7 | +import 'package:test/test.dart'; |
| 8 | + |
| 9 | +import 'package:ui/ui.dart'; |
| 10 | + |
| 11 | +/// The epsilon of tolerable double precision error. |
| 12 | +/// |
| 13 | +/// This is used in various places in the framework to allow for floating point |
| 14 | +/// precision loss in calculations. Differences below this threshold are safe |
| 15 | +/// to disregard. |
| 16 | +const double precisionErrorTolerance = 1e-10; |
| 17 | + |
| 18 | +void main() { |
| 19 | + internalBootstrapBrowserTest(() => testMain); |
| 20 | +} |
| 21 | + |
| 22 | +// These tests should be kept in sync with the VM tests in |
| 23 | +// testing/dart/lerp_test.dart. |
| 24 | +void testMain() { |
| 25 | + test('lerpDouble should return null if and only if both inputs are null', () { |
| 26 | + expect(lerpDouble(null, null, 1.0), isNull); |
| 27 | + expect(lerpDouble(5.0, null, 0.25), isNotNull); |
| 28 | + expect(lerpDouble(null, 5.0, 0.25), isNotNull); |
| 29 | + |
| 30 | + expect(lerpDouble(5, null, 0.25), isNotNull); |
| 31 | + expect(lerpDouble(null, 5, 0.25), isNotNull); |
| 32 | + }); |
| 33 | + |
| 34 | + test('lerpDouble should treat a null input as 0 if the other input is non-null', () { |
| 35 | + expect(lerpDouble(null, 10.0, 0.25), closeTo(2.5, precisionErrorTolerance)); |
| 36 | + expect(lerpDouble(10.0, null, 0.25), closeTo(7.5, precisionErrorTolerance)); |
| 37 | + |
| 38 | + expect(lerpDouble(null, 10, 0.25), closeTo(2.5, precisionErrorTolerance)); |
| 39 | + expect(lerpDouble(10, null, 0.25), closeTo(7.5, precisionErrorTolerance)); |
| 40 | + }); |
| 41 | + |
| 42 | + test('lerpDouble should handle interpolation values < 0.0', () { |
| 43 | + expect(lerpDouble(0.0, 10.0, -5.0), closeTo(-50.0, precisionErrorTolerance)); |
| 44 | + expect(lerpDouble(10.0, 0.0, -5.0), closeTo(60.0, precisionErrorTolerance)); |
| 45 | + |
| 46 | + expect(lerpDouble(0, 10, -5), closeTo(-50, precisionErrorTolerance)); |
| 47 | + expect(lerpDouble(10, 0, -5), closeTo(60, precisionErrorTolerance)); |
| 48 | + }); |
| 49 | + |
| 50 | + test('lerpDouble should return the start value at 0.0', () { |
| 51 | + expect(lerpDouble(2.0, 10.0, 0.0), 2.0); |
| 52 | + expect(lerpDouble(10.0, 2.0, 0.0), 10.0); |
| 53 | + |
| 54 | + expect(lerpDouble(2, 10, 0), 2); |
| 55 | + expect(lerpDouble(10, 2, 0), 10); |
| 56 | + }); |
| 57 | + |
| 58 | + test('lerpDouble should interpolate between two values', () { |
| 59 | + expect(lerpDouble(0.0, 10.0, 0.25), closeTo(2.5, precisionErrorTolerance)); |
| 60 | + expect(lerpDouble(10.0, 0.0, 0.25), closeTo(7.5, precisionErrorTolerance)); |
| 61 | + |
| 62 | + expect(lerpDouble(0, 10, 0.25), closeTo(2.5, precisionErrorTolerance)); |
| 63 | + expect(lerpDouble(10, 0, 0.25), closeTo(7.5, precisionErrorTolerance)); |
| 64 | + |
| 65 | + // Exact answer: 20.0 - 1.0e-29 |
| 66 | + expect(lerpDouble(10.0, 1.0e30, 1.0e-29), closeTo(20.0, precisionErrorTolerance)); |
| 67 | + |
| 68 | + // Exact answer: 5.0 + 5.0e29 |
| 69 | + expect(lerpDouble(10.0, 1.0e30, 0.5), closeTo(5.0e29, precisionErrorTolerance)); |
| 70 | + }); |
| 71 | + |
| 72 | + test('lerpDouble should return the end value at 1.0', () { |
| 73 | + expect(lerpDouble(2.0, 10.0, 1.0), 10.0); |
| 74 | + expect(lerpDouble(10.0, 2.0, 1.0), 2.0); |
| 75 | + |
| 76 | + expect(lerpDouble(0, 10, 5), 50); |
| 77 | + expect(lerpDouble(10, 0, 5), -40); |
| 78 | + |
| 79 | + expect(lerpDouble(1.0e30, 10.0, 1.0), 10.0); |
| 80 | + expect(lerpDouble(10.0, 1.0e30, 0.0), 10.0); |
| 81 | + }); |
| 82 | + |
| 83 | + test('lerpDouble should handle interpolation values > 1.0', () { |
| 84 | + expect(lerpDouble(0.0, 10.0, 5.0), closeTo(50.0, precisionErrorTolerance)); |
| 85 | + expect(lerpDouble(10.0, 0.0, 5.0), closeTo(-40.0, precisionErrorTolerance)); |
| 86 | + |
| 87 | + expect(lerpDouble(0, 10, 5), closeTo(50, precisionErrorTolerance)); |
| 88 | + expect(lerpDouble(10, 0, 5), closeTo(-40, precisionErrorTolerance)); |
| 89 | + }); |
| 90 | + |
| 91 | + test('lerpDouble should return input value in all cases if begin/end are equal', () { |
| 92 | + expect(lerpDouble(10.0, 10.0, 5.0), 10.0); |
| 93 | + expect(lerpDouble(10.0, 10.0, double.nan), 10.0); |
| 94 | + expect(lerpDouble(10.0, 10.0, double.infinity), 10.0); |
| 95 | + expect(lerpDouble(10.0, 10.0, -double.infinity), 10.0); |
| 96 | + |
| 97 | + expect(lerpDouble(10, 10, 5.0), 10.0); |
| 98 | + expect(lerpDouble(10, 10, double.nan), 10.0); |
| 99 | + expect(lerpDouble(10, 10, double.infinity), 10.0); |
| 100 | + expect(lerpDouble(10, 10, -double.infinity), 10.0); |
| 101 | + |
| 102 | + expect(lerpDouble(double.nan, double.nan, 5.0), isNaN); |
| 103 | + expect(lerpDouble(double.nan, double.nan, double.nan), isNaN); |
| 104 | + expect(lerpDouble(double.nan, double.nan, double.infinity), isNaN); |
| 105 | + expect(lerpDouble(double.nan, double.nan, -double.infinity), isNaN); |
| 106 | + |
| 107 | + expect(lerpDouble(double.infinity, double.infinity, 5.0), double.infinity); |
| 108 | + expect(lerpDouble(double.infinity, double.infinity, double.nan), double.infinity); |
| 109 | + expect(lerpDouble(double.infinity, double.infinity, double.infinity), double.infinity); |
| 110 | + expect(lerpDouble(double.infinity, double.infinity, -double.infinity), double.infinity); |
| 111 | + |
| 112 | + expect(lerpDouble(-double.infinity, -double.infinity, 5.0), -double.infinity); |
| 113 | + expect(lerpDouble(-double.infinity, -double.infinity, double.nan), -double.infinity); |
| 114 | + expect(lerpDouble(-double.infinity, -double.infinity, double.infinity), -double.infinity); |
| 115 | + expect(lerpDouble(-double.infinity, -double.infinity, -double.infinity), -double.infinity); |
| 116 | + }); |
| 117 | + |
| 118 | + test('lerpDouble should throw AssertionError if interpolation value is NaN and a != b', () { |
| 119 | + expectAssertion(() => lerpDouble(0.0, 10.0, double.nan)); |
| 120 | + }); |
| 121 | + |
| 122 | + test('lerpDouble should throw AssertionError if interpolation value is +/- infinity and a != b', () { |
| 123 | + expectAssertion(() => lerpDouble(0.0, 10.0, double.infinity)); |
| 124 | + expectAssertion(() => lerpDouble(0.0, 10.0, -double.infinity)); |
| 125 | + }); |
| 126 | + |
| 127 | + test('lerpDouble should throw AssertionError if either start or end are NaN', () { |
| 128 | + expectAssertion(() => lerpDouble(double.nan, 10.0, 5.0)); |
| 129 | + expectAssertion(() => lerpDouble(0.0, double.nan, 5.0)); |
| 130 | + }); |
| 131 | + |
| 132 | + test('lerpDouble should throw AssertionError if either start or end are +/- infinity', () { |
| 133 | + expectAssertion(() => lerpDouble(double.infinity, 10.0, 5.0)); |
| 134 | + expectAssertion(() => lerpDouble(-double.infinity, 10.0, 5.0)); |
| 135 | + expectAssertion(() => lerpDouble(0.0, double.infinity, 5.0)); |
| 136 | + expectAssertion(() => lerpDouble(0.0, -double.infinity, 5.0)); |
| 137 | + }); |
| 138 | +} |
| 139 | + |
| 140 | +/// Asserts that `callback` throws an [AssertionError]. |
| 141 | +/// |
| 142 | +/// Verifies that the specified callback throws an [AssertionError] when |
| 143 | +/// running in with assertions enabled. When asserts are not enabled, such as |
| 144 | +/// when running using a release-mode VM with default settings, this acts as a |
| 145 | +/// no-op. |
| 146 | +void expectAssertion(Function callback) { |
| 147 | + bool assertsEnabled = false; |
| 148 | + assert(() { |
| 149 | + assertsEnabled = true; |
| 150 | + return true; |
| 151 | + }()); |
| 152 | + if (assertsEnabled) { |
| 153 | + bool threw = false; |
| 154 | + try { |
| 155 | + callback(); |
| 156 | + } catch (e) { |
| 157 | + expect(e is AssertionError, true); |
| 158 | + threw = true; |
| 159 | + } |
| 160 | + expect(threw, true); |
| 161 | + } |
| 162 | +} |
0 commit comments