github.com/c-darwin/mobile@v0.0.0-20160313183840-ff625c46f7c9/exp/f32/affine_test.go (about)

     1  package f32
     2  
     3  import (
     4  	"math"
     5  	"testing"
     6  )
     7  
     8  var xyTests = []struct {
     9  	x, y float32
    10  }{
    11  	{0, 0},
    12  	{1, 1},
    13  	{2, 3},
    14  	{6.5, 4.3},
    15  }
    16  
    17  var a = Affine{
    18  	{3, 4, 5},
    19  	{6, 7, 8},
    20  }
    21  
    22  func TestInverse(t *testing.T) {
    23  	wantInv := Affine{
    24  		{-2.33333, 1.33333, 1},
    25  		{2, -1, -2},
    26  	}
    27  	var gotInv Affine
    28  	gotInv.Inverse(&a)
    29  	if !gotInv.Eq(&wantInv, 0.01) {
    30  		t.Errorf("Inverse: got %s want %s", gotInv, wantInv)
    31  	}
    32  
    33  	var wantId, gotId Affine
    34  	wantId.Identity()
    35  	gotId.Mul(&a, &wantInv)
    36  	if !gotId.Eq(&wantId, 0.01) {
    37  		t.Errorf("Identity #0: got %s want %s", gotId, wantId)
    38  	}
    39  	gotId.Mul(&wantInv, &a)
    40  	if !gotId.Eq(&wantId, 0.01) {
    41  		t.Errorf("Identity #1: got %s want %s", gotId, wantId)
    42  	}
    43  }
    44  
    45  func TestAffineScale(t *testing.T) {
    46  	for _, test := range xyTests {
    47  		want := a
    48  		want.Mul(&want, &Affine{{test.x, 0, 0}, {0, test.y, 0}})
    49  		got := a
    50  		got.Scale(&got, test.x, test.y)
    51  
    52  		if !got.Eq(&want, 0.01) {
    53  			t.Errorf("(%.2f, %.2f): got %s want %s", test.x, test.y, got, want)
    54  		}
    55  	}
    56  }
    57  
    58  func TestAffineTranslate(t *testing.T) {
    59  	for _, test := range xyTests {
    60  		want := a
    61  		want.Mul(&want, &Affine{{1, 0, test.x}, {0, 1, test.y}})
    62  		got := a
    63  		got.Translate(&got, test.x, test.y)
    64  
    65  		if !got.Eq(&want, 0.01) {
    66  			t.Errorf("(%.2f, %.2f): got %s want %s", test.x, test.y, got, want)
    67  		}
    68  	}
    69  
    70  }
    71  
    72  func TestAffineRotate(t *testing.T) {
    73  	want := Affine{
    74  		{-4.000, 3.000, 5.000},
    75  		{-7.000, 6.000, 8.000},
    76  	}
    77  	got := a
    78  	got.Rotate(&got, math.Pi/2)
    79  	if !got.Eq(&want, 0.01) {
    80  		t.Errorf("rotate π: got %s want %s", got, want)
    81  	}
    82  
    83  	want = a
    84  	got = a
    85  	got.Rotate(&got, 2*math.Pi)
    86  	if !got.Eq(&want, 0.01) {
    87  		t.Errorf("rotate 2π: got %s want %s", got, want)
    88  	}
    89  
    90  	got = a
    91  	got.Rotate(&got, math.Pi)
    92  	got.Rotate(&got, math.Pi)
    93  	if !got.Eq(&want, 0.01) {
    94  		t.Errorf("rotate π then π: got %s want %s", got, want)
    95  	}
    96  
    97  	got = a
    98  	got.Rotate(&got, math.Pi/3)
    99  	got.Rotate(&got, -math.Pi/3)
   100  	if !got.Eq(&want, 0.01) {
   101  		t.Errorf("rotate π/3 then -π/3: got %s want %s", got, want)
   102  	}
   103  }
   104  
   105  func TestAffineScaleTranslate(t *testing.T) {
   106  	mulVec := func(m *Affine, v [2]float32) (mv [2]float32) {
   107  		mv[0] = m[0][0]*v[0] + m[0][1]*v[1] + m[0][2]
   108  		mv[1] = m[1][0]*v[0] + m[1][1]*v[1] + m[1][2]
   109  		return mv
   110  	}
   111  	v := [2]float32{1, 10}
   112  
   113  	var sThenT Affine
   114  	sThenT.Identity()
   115  	sThenT.Scale(&sThenT, 13, 17)
   116  	sThenT.Translate(&sThenT, 101, 151)
   117  	wantSTT := [2]float32{
   118  		13 * (101 + 1),
   119  		17 * (151 + 10),
   120  	}
   121  	if got := mulVec(&sThenT, v); got != wantSTT {
   122  		t.Errorf("S then T: got %v, want %v", got, wantSTT)
   123  	}
   124  
   125  	var tThenS Affine
   126  	tThenS.Identity()
   127  	tThenS.Translate(&tThenS, 101, 151)
   128  	tThenS.Scale(&tThenS, 13, 17)
   129  	wantTTS := [2]float32{
   130  		101 + (13 * 1),
   131  		151 + (17 * 10),
   132  	}
   133  	if got := mulVec(&tThenS, v); got != wantTTS {
   134  		t.Errorf("T then S: got %v, want %v", got, wantTTS)
   135  	}
   136  }