File: tween_test.go

package info (click to toggle)
golang-golang-x-mobile 0.0~git20250520.a1d9079%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,784 kB
  • sloc: objc: 1,512; java: 1,489; ansic: 1,159; xml: 365; asm: 34; sh: 14; makefile: 5
file content (53 lines) | stat: -rw-r--r-- 1,387 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Copyright 2014 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package clock

import "testing"

func TestLinear(t *testing.T) {
	t0 := Time(0)
	t1 := Time(6 * 60)
	now := Time(3 * 60)

	if c := Linear(t0, t1, now); c != 0.5 {
		t.Errorf("c=%.2f, want 0.5", c)
	}
}

func TestCubicBezier(t *testing.T) {
	t0 := Time(0)
	t1 := Time(1e6)

	tests := []struct {
		x0, y0, x1, y1 float32
		x, y           float32
	}{
		{0.00, 0.1, 0.4, 1.00, 0.0, 0.00},
		{0.00, 0.1, 0.4, 1.00, 0.1, 0.26},
		{0.00, 0.1, 0.4, 1.00, 0.5, 0.79},
		{0.00, 0.1, 0.4, 1.00, 0.9, 0.99},
		{0.00, 0.1, 0.4, 1.00, 1.0, 1.00},
		{0.36, 0.2, 0.3, 0.85, 0.0, 0.0},
		{0.36, 0.2, 0.3, 0.85, 0.3059, 0.3952},
		{0.36, 0.2, 0.3, 0.85, 0.4493, 0.6408},
		{0.36, 0.2, 0.3, 0.85, 0.8116, 0.9410},
		{0.00, 0.0, 1.0, 1.00, 0.1, 0.1},
		{0.00, 0.0, 1.0, 1.00, 0.5, 0.5},
		{0.00, 0.0, 1.0, 1.00, 0.9, 0.9},
		{0.42, 0.0, 1.0, 1.00, 0.0, 0.0},
	}

	for _, test := range tests {
		cb := CubicBezier(test.x0, test.y0, test.x1, test.y1)
		now := t0 + Time(float32(t1-t0)*test.x)
		y := cb(t0, t1, now)

		const epsilon = 0.01
		diff := y - test.y
		if diff < -epsilon || +epsilon < diff {
			t.Errorf("CubicBezier(%.2f,%.2f,%.2f,%.2f): for x=%.2f got y=%.2f, want %.2f", test.x0, test.y0, test.x1, test.y1, test.x, y, test.y)
		}
	}
}