github.com/acrespo/mobile@v0.0.0-20190107162257-dc0771356504/exp/sprite/clock/tween_test.go (about)

     1  // Copyright 2014 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package clock
     6  
     7  import "testing"
     8  
     9  func TestLinear(t *testing.T) {
    10  	t0 := Time(0)
    11  	t1 := Time(6 * 60)
    12  	now := Time(3 * 60)
    13  
    14  	if c := Linear(t0, t1, now); c != 0.5 {
    15  		t.Errorf("c=%.2f, want 0.5", c)
    16  	}
    17  }
    18  
    19  func TestCubicBezier(t *testing.T) {
    20  	t0 := Time(0)
    21  	t1 := Time(1e6)
    22  
    23  	tests := []struct {
    24  		x0, y0, x1, y1 float32
    25  		x, y           float32
    26  	}{
    27  		{0.00, 0.1, 0.4, 1.00, 0.0, 0.00},
    28  		{0.00, 0.1, 0.4, 1.00, 0.1, 0.26},
    29  		{0.00, 0.1, 0.4, 1.00, 0.5, 0.79},
    30  		{0.00, 0.1, 0.4, 1.00, 0.9, 0.99},
    31  		{0.00, 0.1, 0.4, 1.00, 1.0, 1.00},
    32  		{0.36, 0.2, 0.3, 0.85, 0.0, 0.0},
    33  		{0.36, 0.2, 0.3, 0.85, 0.3059, 0.3952},
    34  		{0.36, 0.2, 0.3, 0.85, 0.4493, 0.6408},
    35  		{0.36, 0.2, 0.3, 0.85, 0.8116, 0.9410},
    36  		{0.00, 0.0, 1.0, 1.00, 0.1, 0.1},
    37  		{0.00, 0.0, 1.0, 1.00, 0.5, 0.5},
    38  		{0.00, 0.0, 1.0, 1.00, 0.9, 0.9},
    39  		{0.42, 0.0, 1.0, 1.00, 0.0, 0.0},
    40  	}
    41  
    42  	for _, test := range tests {
    43  		cb := CubicBezier(test.x0, test.y0, test.x1, test.y1)
    44  		now := t0 + Time(float32(t1-t0)*test.x)
    45  		y := cb(t0, t1, now)
    46  
    47  		const epsilon = 0.01
    48  		diff := y - test.y
    49  		if diff < -epsilon || +epsilon < diff {
    50  			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)
    51  		}
    52  	}
    53  }