tween_test.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. package clock
  5. import "testing"
  6. func TestLinear(t *testing.T) {
  7. t0 := Time(0)
  8. t1 := Time(6 * 60)
  9. now := Time(3 * 60)
  10. if c := Linear(t0, t1, now); c != 0.5 {
  11. t.Errorf("c=%.2f, want 0.5", c)
  12. }
  13. }
  14. func TestCubicBezier(t *testing.T) {
  15. t0 := Time(0)
  16. t1 := Time(1e6)
  17. tests := []struct {
  18. x0, y0, x1, y1 float32
  19. x, y float32
  20. }{
  21. {0.00, 0.1, 0.4, 1.00, 0.0, 0.00},
  22. {0.00, 0.1, 0.4, 1.00, 0.1, 0.26},
  23. {0.00, 0.1, 0.4, 1.00, 0.5, 0.79},
  24. {0.00, 0.1, 0.4, 1.00, 0.9, 0.99},
  25. {0.00, 0.1, 0.4, 1.00, 1.0, 1.00},
  26. {0.36, 0.2, 0.3, 0.85, 0.0, 0.0},
  27. {0.36, 0.2, 0.3, 0.85, 0.3059, 0.3952},
  28. {0.36, 0.2, 0.3, 0.85, 0.4493, 0.6408},
  29. {0.36, 0.2, 0.3, 0.85, 0.8116, 0.9410},
  30. {0.00, 0.0, 1.0, 1.00, 0.1, 0.1},
  31. {0.00, 0.0, 1.0, 1.00, 0.5, 0.5},
  32. {0.00, 0.0, 1.0, 1.00, 0.9, 0.9},
  33. {0.42, 0.0, 1.0, 1.00, 0.0, 0.0},
  34. }
  35. for _, test := range tests {
  36. cb := CubicBezier(test.x0, test.y0, test.x1, test.y1)
  37. now := t0 + Time(float32(t1-t0)*test.x)
  38. y := cb(t0, t1, now)
  39. const epsilon = 0.01
  40. diff := y - test.y
  41. if diff < -epsilon || +epsilon < diff {
  42. 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)
  43. }
  44. }
  45. }