curves_test.dart 20.2 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
import 'dart:math' as math;

7
import 'package:flutter/widgets.dart';
8
import 'package:flutter_test/flutter_test.dart';
9 10 11 12

void main() {
  test('toString control test', () {
    expect(Curves.linear, hasOneLineDescription);
13 14
    expect(const SawTooth(3), hasOneLineDescription);
    expect(const Interval(0.25, 0.75), hasOneLineDescription);
15
    expect(const Interval(0.25, 0.75, curve: Curves.ease), hasOneLineDescription);
16 17 18
  });

  test('Curve flipped control test', () {
19
    const Curve ease = Curves.ease;
20
    final Curve flippedEase = ease.flipped;
21 22 23 24 25 26
    expect(flippedEase.transform(0.0), lessThan(0.001));
    expect(flippedEase.transform(0.5), lessThan(ease.transform(0.5)));
    expect(flippedEase.transform(1.0), greaterThan(0.999));
    expect(flippedEase, hasOneLineDescription);
  });

27
  test('Threshold has a threshold', () {
28
    const Curve step = Threshold(0.25);
29 30 31 32 33 34
    expect(step.transform(0.0), 0.0);
    expect(step.transform(0.24), 0.0);
    expect(step.transform(0.25), 1.0);
    expect(step.transform(0.26), 1.0);
    expect(step.transform(1.0), 1.0);
  });
35

36 37 38 39 40 41 42 43 44 45 46
  void assertMaximumSlope(Curve curve, double maximumSlope) {
    const double delta = 0.005;
    for (double x = 0.0; x < 1.0 - delta; x += delta) {
      final double deltaY = curve.transform(x) - curve.transform(x + delta);
      assert(deltaY.abs() < delta * maximumSlope, '${curve.toString()} discontinuous at $x');
    }
  }

  test('Curve is continuous', () {
    assertMaximumSlope(Curves.linear, 20.0);
    assertMaximumSlope(Curves.decelerate, 20.0);
47
    assertMaximumSlope(Curves.fastOutSlowIn, 20.0);
48
    assertMaximumSlope(Curves.slowMiddle, 20.0);
49 50 51 52 53 54
    assertMaximumSlope(Curves.bounceIn, 20.0);
    assertMaximumSlope(Curves.bounceOut, 20.0);
    assertMaximumSlope(Curves.bounceInOut, 20.0);
    assertMaximumSlope(Curves.elasticOut, 20.0);
    assertMaximumSlope(Curves.elasticInOut, 20.0);
    assertMaximumSlope(Curves.ease, 20.0);
55

56
    assertMaximumSlope(Curves.easeIn, 20.0);
57 58 59 60 61 62 63 64
    assertMaximumSlope(Curves.easeInSine, 20.0);
    assertMaximumSlope(Curves.easeInQuad, 20.0);
    assertMaximumSlope(Curves.easeInCubic, 20.0);
    assertMaximumSlope(Curves.easeInQuart, 20.0);
    assertMaximumSlope(Curves.easeInQuint, 20.0);
    assertMaximumSlope(Curves.easeInExpo, 20.0);
    assertMaximumSlope(Curves.easeInCirc, 20.0);

65
    assertMaximumSlope(Curves.easeOut, 20.0);
66 67 68
    assertMaximumSlope(Curves.easeOutSine, 20.0);
    assertMaximumSlope(Curves.easeOutQuad, 20.0);
    assertMaximumSlope(Curves.easeOutCubic, 20.0);
69
    assertMaximumSlope(Curves.easeInOutCubicEmphasized, 20.0);
70 71 72 73 74 75 76 77
    assertMaximumSlope(Curves.easeOutQuart, 20.0);
    assertMaximumSlope(Curves.easeOutQuint, 20.0);
    assertMaximumSlope(Curves.easeOutExpo, 20.0);
    assertMaximumSlope(Curves.easeOutCirc, 20.0);

    // Curves.easeInOutExpo is discontinuous at its midpoint, so not included
    // here

78
    assertMaximumSlope(Curves.easeInOut, 20.0);
79 80 81 82 83 84
    assertMaximumSlope(Curves.easeInOutSine, 20.0);
    assertMaximumSlope(Curves.easeInOutQuad, 20.0);
    assertMaximumSlope(Curves.easeInOutCubic, 20.0);
    assertMaximumSlope(Curves.easeInOutQuart, 20.0);
    assertMaximumSlope(Curves.easeInOutQuint, 20.0);
    assertMaximumSlope(Curves.easeInOutCirc, 20.0);
85 86
  });

87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
  void expectStaysInBounds(Curve curve) {
    expect(curve.transform(0.0), inInclusiveRange(0.0, 1.0));
    expect(curve.transform(0.1), inInclusiveRange(0.0, 1.0));
    expect(curve.transform(0.2), inInclusiveRange(0.0, 1.0));
    expect(curve.transform(0.3), inInclusiveRange(0.0, 1.0));
    expect(curve.transform(0.4), inInclusiveRange(0.0, 1.0));
    expect(curve.transform(0.5), inInclusiveRange(0.0, 1.0));
    expect(curve.transform(0.6), inInclusiveRange(0.0, 1.0));
    expect(curve.transform(0.7), inInclusiveRange(0.0, 1.0));
    expect(curve.transform(0.8), inInclusiveRange(0.0, 1.0));
    expect(curve.transform(0.9), inInclusiveRange(0.0, 1.0));
    expect(curve.transform(1.0), inInclusiveRange(0.0, 1.0));
  }

  test('Bounce stays in bounds', () {
    expectStaysInBounds(Curves.bounceIn);
    expectStaysInBounds(Curves.bounceOut);
    expectStaysInBounds(Curves.bounceInOut);
  });

  List<double> estimateBounds(Curve curve) {
108 109 110 111 112 113 114 115 116 117 118 119 120
    final List<double> values = <double>[
      curve.transform(0.0),
      curve.transform(0.1),
      curve.transform(0.2),
      curve.transform(0.3),
      curve.transform(0.4),
      curve.transform(0.5),
      curve.transform(0.6),
      curve.transform(0.7),
      curve.transform(0.8),
      curve.transform(0.9),
      curve.transform(1.0),
    ];
121 122 123 124 125 126 127

    return <double>[
      values.reduce(math.min),
      values.reduce(math.max),
    ];
  }

128
  test('Elastic overshoots its bounds', () {
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
    expect(Curves.elasticIn, hasOneLineDescription);
    expect(Curves.elasticOut, hasOneLineDescription);
    expect(Curves.elasticInOut, hasOneLineDescription);

    List<double> bounds;
    bounds = estimateBounds(Curves.elasticIn);
    expect(bounds[0], lessThan(0.0));
    expect(bounds[1], lessThanOrEqualTo(1.0));
    bounds = estimateBounds(Curves.elasticOut);
    expect(bounds[0], greaterThanOrEqualTo(0.0));
    expect(bounds[1], greaterThan(1.0));
    bounds = estimateBounds(Curves.elasticInOut);
    expect(bounds[0], lessThan(0.0));
    expect(bounds[1], greaterThan(1.0));
  });
144

145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
  test('Back overshoots its bounds', () {
    expect(Curves.easeInBack, hasOneLineDescription);
    expect(Curves.easeOutBack, hasOneLineDescription);
    expect(Curves.easeInOutBack, hasOneLineDescription);

    List<double> bounds;
    bounds = estimateBounds(Curves.easeInBack);
    expect(bounds[0], lessThan(0.0));
    expect(bounds[1], lessThanOrEqualTo(1.0));
    bounds = estimateBounds(Curves.easeOutBack);
    expect(bounds[0], greaterThanOrEqualTo(0.0));
    expect(bounds[1], greaterThan(1.0));
    bounds = estimateBounds(Curves.easeInOutBack);
    expect(bounds[0], lessThan(0.0));
    expect(bounds[1], greaterThan(1.0));
  });

162 163 164
  test('Decelerate does so', () {
    expect(Curves.decelerate, hasOneLineDescription);

165
    final List<double> bounds = estimateBounds(Curves.decelerate);
166 167 168
    expect(bounds[0], greaterThanOrEqualTo(0.0));
    expect(bounds[1], lessThanOrEqualTo(1.0));

169 170
    final double d1 = Curves.decelerate.transform(0.2) - Curves.decelerate.transform(0.0);
    final double d2 = Curves.decelerate.transform(1.0) - Curves.decelerate.transform(0.8);
171 172
    expect(d2, lessThan(d1));
  });
173

174 175 176 177 178 179 180 181 182
  test('ThreePointCubic interpolates midpoint', () {
    const ThreePointCubic test = ThreePointCubic(
      Offset(0.05, 0), Offset(0.133333, 0.06),
      Offset(0.166666, 0.4),
      Offset(0.208333, 0.82), Offset(0.25, 1),
    );
    expect(test.transform(0.166666), equals(0.4));
  });

183
  test('Invalid transform parameter should assert', () {
184 185
    expect(() => const SawTooth(2).transform(-0.0001), throwsAssertionError);
    expect(() => const SawTooth(2).transform(1.0001), throwsAssertionError);
186

187 188
    expect(() => const Interval(0.0, 1.0).transform(-0.0001), throwsAssertionError);
    expect(() => const Interval(0.0, 1.0).transform(1.0001), throwsAssertionError);
189

190 191
    expect(() => const Threshold(0.5).transform(-0.0001), throwsAssertionError);
    expect(() => const Threshold(0.5).transform(1.0001), throwsAssertionError);
192

193 194
    expect(() => const ElasticInCurve().transform(-0.0001), throwsAssertionError);
    expect(() => const ElasticInCurve().transform(1.0001), throwsAssertionError);
195

196 197
    expect(() => const ElasticOutCurve().transform(-0.0001), throwsAssertionError);
    expect(() => const ElasticOutCurve().transform(1.0001), throwsAssertionError);
198

199 200
    expect(() => const Cubic(0.42, 0.0, 0.58, 1.0).transform(-0.0001), throwsAssertionError);
    expect(() => const Cubic(0.42, 0.0, 0.58, 1.0).transform(1.0001), throwsAssertionError);
201

202 203 204
    expect(() => Curves.easeInOutCubicEmphasized.transform(-0.0001), throwsAssertionError);
    expect(() => Curves.easeInOutCubicEmphasized.transform(1.0001), throwsAssertionError);

205 206
    expect(() => Curves.decelerate.transform(-0.0001), throwsAssertionError);
    expect(() => Curves.decelerate.transform(1.0001), throwsAssertionError);
207

208 209
    expect(() => Curves.bounceIn.transform(-0.0001), throwsAssertionError);
    expect(() => Curves.bounceIn.transform(1.0001), throwsAssertionError);
210

211 212
    expect(() => Curves.bounceOut.transform(-0.0001), throwsAssertionError);
    expect(() => Curves.bounceOut.transform(1.0001), throwsAssertionError);
213

214 215
    expect(() => Curves.bounceInOut.transform(-0.0001), throwsAssertionError);
    expect(() => Curves.bounceInOut.transform(1.0001), throwsAssertionError);
216 217
  });

218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
  test('Curve transform method should return 0.0 for t=0.0 and 1.0 for t=1.0', () {
    expect(const SawTooth(2).transform(0), 0);
    expect(const SawTooth(2).transform(1), 1);

    expect(const Interval(0, 1).transform(0), 0);
    expect(const Interval(0, 1).transform(1), 1);

    expect(const Threshold(0.5).transform(0), 0);
    expect(const Threshold(0.5).transform(1), 1);

    expect(const ElasticInCurve().transform(0), 0);
    expect(const ElasticInCurve().transform(1), 1);

    expect(const ElasticOutCurve().transform(0), 0);
    expect(const ElasticOutCurve().transform(1), 1);

    expect(const ElasticInOutCurve().transform(0), 0);
    expect(const ElasticInOutCurve().transform(1), 1);

    expect(Curves.linear.transform(0), 0);
    expect(Curves.linear.transform(1), 1);

    expect(Curves.easeInOutExpo.transform(0), 0);
    expect(Curves.easeInOutExpo.transform(1), 1);

243 244 245
    expect(Curves.easeInOutCubicEmphasized.transform(0), 0);
    expect(Curves.easeInOutCubicEmphasized.transform(1), 1);

246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
    expect(const FlippedCurve(Curves.easeInOutExpo).transform(0), 0);
    expect(const FlippedCurve(Curves.easeInOutExpo).transform(1), 1);

    expect(Curves.decelerate.transform(0), 0);
    expect(Curves.decelerate.transform(1), 1);

    expect(Curves.bounceIn.transform(0), 0);
    expect(Curves.bounceIn.transform(1), 1);

    expect(Curves.bounceOut.transform(0), 0);
    expect(Curves.bounceOut.transform(1), 1);

    expect(Curves.bounceInOut.transform(0), 0);
    expect(Curves.bounceInOut.transform(1), 1);
  });

262 263 264
  test('CatmullRomSpline interpolates values properly', () {
    final CatmullRomSpline curve = CatmullRomSpline(
      const <Offset>[
265
        Offset.zero,
266 267 268 269 270 271 272 273 274 275
        Offset(0.01, 0.25),
        Offset(0.2, 0.25),
        Offset(0.33, 0.25),
        Offset(0.5, 1.0),
        Offset(0.66, 0.75),
        Offset(1.0, 1.0),
      ],
      startHandle: const Offset(0.0, -0.3),
      endHandle: const Offset(1.3, 1.3),
    );
276 277 278 279 280 281 282 283 284 285 286
    const double tolerance = 1e-6;
    expect(curve.transform(0.0).dx, moreOrLessEquals(0.0, epsilon: tolerance));
    expect(curve.transform(0.0).dy, moreOrLessEquals(0.0, epsilon: tolerance));
    expect(curve.transform(0.25).dx, moreOrLessEquals(0.0966945, epsilon: tolerance));
    expect(curve.transform(0.25).dy, moreOrLessEquals(0.2626806, epsilon: tolerance));
    expect(curve.transform(0.5).dx, moreOrLessEquals(0.33, epsilon: tolerance));
    expect(curve.transform(0.5).dy, moreOrLessEquals(0.25, epsilon: tolerance));
    expect(curve.transform(0.75).dx, moreOrLessEquals(0.570260, epsilon: tolerance));
    expect(curve.transform(0.75).dy, moreOrLessEquals(0.883085, epsilon: tolerance));
    expect(curve.transform(1.0).dx, moreOrLessEquals(1.0, epsilon: tolerance));
    expect(curve.transform(1.0).dy, moreOrLessEquals(1.0, epsilon: tolerance));
287
  });
288

289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
  test('CatmullRomSpline enforces contract', () {
    expect(() {
      CatmullRomSpline(const <Offset>[]);
    }, throwsAssertionError);
    expect(() {
      CatmullRomSpline(const <Offset>[Offset.zero]);
    }, throwsAssertionError);
    expect(() {
      CatmullRomSpline(const <Offset>[Offset.zero, Offset.zero]);
    }, throwsAssertionError);
    expect(() {
      CatmullRomSpline(const <Offset>[Offset.zero, Offset.zero, Offset.zero]);
    }, throwsAssertionError);
    expect(() {
      CatmullRomSpline(const <Offset>[Offset.zero, Offset.zero, Offset.zero, Offset.zero], tension: -1.0);
    }, throwsAssertionError);
    expect(() {
      CatmullRomSpline(const <Offset>[Offset.zero, Offset.zero, Offset.zero, Offset.zero], tension: 2.0);
    }, throwsAssertionError);
  });
309

310 311 312
  test('CatmullRomSpline interpolates values properly when precomputed', () {
    final CatmullRomSpline curve = CatmullRomSpline.precompute(
      const <Offset>[
313
        Offset.zero,
314 315 316 317 318 319 320 321 322 323
        Offset(0.01, 0.25),
        Offset(0.2, 0.25),
        Offset(0.33, 0.25),
        Offset(0.5, 1.0),
        Offset(0.66, 0.75),
        Offset(1.0, 1.0),
      ],
      startHandle: const Offset(0.0, -0.3),
      endHandle: const Offset(1.3, 1.3),
    );
324 325 326 327 328 329 330 331 332 333 334
    const double tolerance = 1e-6;
    expect(curve.transform(0.0).dx, moreOrLessEquals(0.0, epsilon: tolerance));
    expect(curve.transform(0.0).dy, moreOrLessEquals(0.0, epsilon: tolerance));
    expect(curve.transform(0.25).dx, moreOrLessEquals(0.0966945, epsilon: tolerance));
    expect(curve.transform(0.25).dy, moreOrLessEquals(0.2626806, epsilon: tolerance));
    expect(curve.transform(0.5).dx, moreOrLessEquals(0.33, epsilon: tolerance));
    expect(curve.transform(0.5).dy, moreOrLessEquals(0.25, epsilon: tolerance));
    expect(curve.transform(0.75).dx, moreOrLessEquals(0.570260, epsilon: tolerance));
    expect(curve.transform(0.75).dy, moreOrLessEquals(0.883085, epsilon: tolerance));
    expect(curve.transform(1.0).dx, moreOrLessEquals(1.0, epsilon: tolerance));
    expect(curve.transform(1.0).dy, moreOrLessEquals(1.0, epsilon: tolerance));
335
  });
336

337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
  test('CatmullRomSpline enforces contract when precomputed', () {
    expect(() {
      CatmullRomSpline.precompute(const <Offset>[]);
    }, throwsAssertionError);
    expect(() {
      CatmullRomSpline.precompute(const <Offset>[Offset.zero]);
    }, throwsAssertionError);
    expect(() {
      CatmullRomSpline.precompute(const <Offset>[Offset.zero, Offset.zero]);
    }, throwsAssertionError);
    expect(() {
      CatmullRomSpline.precompute(const <Offset>[Offset.zero, Offset.zero, Offset.zero]);
    }, throwsAssertionError);
    expect(() {
      CatmullRomSpline.precompute(const <Offset>[Offset.zero, Offset.zero, Offset.zero, Offset.zero], tension: -1.0);
    }, throwsAssertionError);
    expect(() {
      CatmullRomSpline.precompute(const <Offset>[Offset.zero, Offset.zero, Offset.zero, Offset.zero], tension: 2.0);
    }, throwsAssertionError);
  });
357

358 359 360 361 362 363 364 365 366 367 368 369
  test('CatmullRomCurve interpolates given points correctly', () {
    final CatmullRomCurve curve = CatmullRomCurve(
      const <Offset>[
        Offset(0.2, 0.25),
        Offset(0.33, 0.25),
        Offset(0.5, 1.0),
        Offset(0.8, 0.75),
      ],
    );

    // These values are approximations.
    const double tolerance = 1e-6;
370 371 372 373 374 375 376 377
    expect(curve.transform(0.0), moreOrLessEquals(0.0, epsilon: tolerance));
    expect(curve.transform(0.01), moreOrLessEquals(0.012874734350170863, epsilon: tolerance));
    expect(curve.transform(0.2), moreOrLessEquals(0.24989646045277542, epsilon: tolerance));
    expect(curve.transform(0.33), moreOrLessEquals(0.250037698527661, epsilon: tolerance));
    expect(curve.transform(0.5), moreOrLessEquals(0.9999057323235939, epsilon: tolerance));
    expect(curve.transform(0.6), moreOrLessEquals(0.9357294964536621, epsilon: tolerance));
    expect(curve.transform(0.8), moreOrLessEquals(0.7500423402378034, epsilon: tolerance));
    expect(curve.transform(1.0), moreOrLessEquals(1.0, epsilon: tolerance));
378
  });
379

380 381 382 383 384 385 386 387 388 389 390 391
  test('CatmullRomCurve interpolates given points correctly when precomputed', () {
    final CatmullRomCurve curve = CatmullRomCurve.precompute(
      const <Offset>[
        Offset(0.2, 0.25),
        Offset(0.33, 0.25),
        Offset(0.5, 1.0),
        Offset(0.8, 0.75),
      ],
    );

    // These values are approximations.
    const double tolerance = 1e-6;
392 393 394 395 396 397 398 399
    expect(curve.transform(0.0), moreOrLessEquals(0.0, epsilon: tolerance));
    expect(curve.transform(0.01), moreOrLessEquals(0.012874734350170863, epsilon: tolerance));
    expect(curve.transform(0.2), moreOrLessEquals(0.24989646045277542, epsilon: tolerance));
    expect(curve.transform(0.33), moreOrLessEquals(0.250037698527661, epsilon: tolerance));
    expect(curve.transform(0.5), moreOrLessEquals(0.9999057323235939, epsilon: tolerance));
    expect(curve.transform(0.6), moreOrLessEquals(0.9357294964536621, epsilon: tolerance));
    expect(curve.transform(0.8), moreOrLessEquals(0.7500423402378034, epsilon: tolerance));
    expect(curve.transform(1.0), moreOrLessEquals(1.0, epsilon: tolerance));
400
  });
401

402 403 404 405 406 407 408 409 410 411 412 413 414
  test('CatmullRomCurve enforces contract', () {
    expect(() {
      CatmullRomCurve(const <Offset>[]);
    }, throwsAssertionError);
    expect(() {
      CatmullRomCurve(const <Offset>[Offset.zero]);
    }, throwsAssertionError);
    expect(() {
      CatmullRomCurve(const <Offset>[Offset.zero, Offset.zero]);
    }, throwsAssertionError);

    // Monotonically increasing in X.
    expect(
415 416 417 418 419 420 421 422
      CatmullRomCurve.validateControlPoints(
        const <Offset>[
          Offset(0.2, 0.25),
          Offset(0.01, 0.25),
        ],
      ),
      isFalse,
    );
423 424 425 426 427 428 429 430 431 432 433
    expect(() {
      CatmullRomCurve(
        const <Offset>[
          Offset(0.2, 0.25),
          Offset(0.01, 0.25),
        ],
      );
    }, throwsAssertionError);

    // X within range (0.0, 1.0).
    expect(
434 435 436 437 438 439 440 441
      CatmullRomCurve.validateControlPoints(
        const <Offset>[
          Offset(0.2, 0.25),
          Offset(1.01, 0.25),
        ],
      ),
      isFalse,
    );
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
    expect(() {
      CatmullRomCurve(
        const <Offset>[
          Offset(0.2, 0.25),
          Offset(1.01, 0.25),
        ],
      );
    }, throwsAssertionError);

    // Not multi-valued in Y at x=0.0.
    expect(
      CatmullRomCurve.validateControlPoints(
        const <Offset>[
          Offset(0.05, 0.50),
          Offset(0.50, 0.50),
          Offset(0.75, 0.75),
        ],
      ),
      isFalse,
    );
    expect(() {
      CatmullRomCurve(
        const <Offset>[
          Offset(0.05, 0.50),
          Offset(0.50, 0.50),
          Offset(0.75, 0.75),
        ],
      );
    }, throwsAssertionError);

    // Not multi-valued in Y at x=1.0.
    expect(
      CatmullRomCurve.validateControlPoints(
        const <Offset>[
          Offset(0.25, 0.25),
          Offset(0.50, 0.50),
          Offset(0.95, 0.51),
        ],
      ),
      isFalse,
    );
    expect(() {
      CatmullRomCurve(
        const <Offset>[
          Offset(0.25, 0.25),
          Offset(0.50, 0.50),
          Offset(0.95, 0.51),
        ],
      );
    }, throwsAssertionError);

    // Not multi-valued in Y in between x = 0.0 and x = 1.0.
    expect(
      CatmullRomCurve.validateControlPoints(
        const <Offset>[
          Offset(0.5, 0.05),
          Offset(0.5, 0.95),
        ],
      ),
      isFalse,
    );
    expect(() {
      CatmullRomCurve(
        const <Offset>[
          Offset(0.5, 0.05),
          Offset(0.5, 0.95),
        ],
      );
    }, throwsAssertionError);
  });
512

513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
  test('CatmullRomCurve enforces contract when precomputed', () {
    expect(() {
      CatmullRomCurve.precompute(const <Offset>[]);
    }, throwsAssertionError);
    expect(() {
      CatmullRomCurve.precompute(const <Offset>[Offset.zero]);
    }, throwsAssertionError);
    expect(() {
      CatmullRomCurve.precompute(const <Offset>[Offset.zero, Offset.zero]);
    }, throwsAssertionError);

    // Monotonically increasing in X.
    expect(() {
      CatmullRomCurve.precompute(
        const <Offset>[
          Offset(0.2, 0.25),
          Offset(0.01, 0.25),
        ],
      );
    }, throwsAssertionError);

    // X within range (0.0, 1.0).
    expect(() {
      CatmullRomCurve.precompute(
        const <Offset>[
          Offset(0.2, 0.25),
          Offset(1.01, 0.25),
        ],
      );
    }, throwsAssertionError);

    // Not multi-valued in Y at x=0.0.
    expect(() {
      CatmullRomCurve.precompute(
        const <Offset>[
          Offset(0.05, 0.50),
          Offset(0.50, 0.50),
          Offset(0.75, 0.75),
        ],
      );
    }, throwsAssertionError);

    // Not multi-valued in Y at x=1.0.
    expect(() {
      CatmullRomCurve.precompute(
        const <Offset>[
          Offset(0.25, 0.25),
          Offset(0.50, 0.50),
          Offset(0.95, 0.51),
        ],
      );
    }, throwsAssertionError);

    // Not multi-valued in Y in between x = 0.0 and x = 1.0.
    expect(() {
      CatmullRomCurve.precompute(
        const <Offset>[
          Offset(0.5, 0.05),
          Offset(0.5, 0.95),
        ],
      );
    }, throwsAssertionError);
  });
576
}