Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/ui/lerp.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
part of dart.ui;

/// Linearly interpolate between two numbers.
// TODO(cbracken): Consider making a and b non-nullable.
// https://github.com/flutter/flutter/issues/64617
double? lerpDouble(num? a, num? b, double t) {
if (a == null && b == null)
return null;
Expand Down
46 changes: 46 additions & 0 deletions testing/dart/lerp_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// @dart = 2.10
import 'dart:ui';

import 'package:test/test.dart';

void main() {
test('lerpDouble should return null if and only if both inputs are null', () {
expect(lerpDouble(null, null, 1.0), isNull);
expect(lerpDouble(5.0, null, 0.25), isNotNull);
expect(lerpDouble(null, 5.0, 0.25), isNotNull);
});

test('lerpDouble should treat a null input as 0 if the other input is non-null', () {
expect(lerpDouble(null, 10.0, 0.25), 2.5);
expect(lerpDouble(10.0, null, 0.25), 7.5);
});

test('lerpDouble should handle interpolation values < 0.0', () {
expect(lerpDouble(0.0, 10.0, -5.0), -50.0);
expect(lerpDouble(10.0, 0.0, -5.0), 60.0);
});

test('lerpDouble should return the start value at 0.0', () {
expect(lerpDouble(2.0, 10.0, 0.0), 2.0);
expect(lerpDouble(10.0, 2.0, 0.0), 10.0);
});

test('lerpDouble should interpolate between two values', () {
expect(lerpDouble(0.0, 10.0, 0.25), 2.5);
expect(lerpDouble(10.0, 0.0, 0.25), 7.5);
});

test('lerpDouble should return the end value at 1.0', () {
expect(lerpDouble(2.0, 10.0, 1.0), 10.0);
expect(lerpDouble(10.0, 2.0, 1.0), 2.0);
});

test('lerpDouble should handle interpolation values > 1.0', () {
expect(lerpDouble(0.0, 10.0, 5.0), 50.0);
expect(lerpDouble(10.0, 0.0, 5.0), -40.0);
});
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we could also test infinities, NaNs, very large numbers, very small numbers...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call. I'll land this as-is for the moment and make the wiki updates so that @matthew-carroll is unblocked on his work, but will add those in a followup PR.