-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
Matrix3x2Tests.cs
1085 lines (883 loc) · 39.4 KB
/
Matrix3x2Tests.cs
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Runtime.InteropServices;
using Xunit;
namespace System.Numerics.Tests
{
public sealed class Matrix3x2Tests
{
static Matrix3x2 GenerateIncrementalMatrixNumber(float value = 0.0f)
{
Matrix3x2 a = new Matrix3x2();
a.M11 = value + 1.0f;
a.M12 = value + 2.0f;
a.M21 = value + 3.0f;
a.M22 = value + 4.0f;
a.M31 = value + 5.0f;
a.M32 = value + 6.0f;
return a;
}
static Matrix3x2 GenerateTestMatrix()
{
Matrix3x2 m = Matrix3x2.CreateRotation(MathHelper.ToRadians(30.0f));
m.Translation = new Vector2(111.0f, 222.0f);
return m;
}
[Theory]
[InlineData(0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f)]
[InlineData(1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f)]
[InlineData(3.1434343f, 1.1234123f, 0.1234123f, -0.1234123f, 3.1434343f, 1.1234123f)]
[InlineData(1.0000001f, 0.0000001f, 2.0000001f, 0.0000002f, 1.0000001f, 0.0000001f)]
public void Matrix3x2IndexerGetTest(float m11, float m12, float m21, float m22, float m31, float m32)
{
var matrix = new Matrix3x2(m11, m12, m21, m22, m31, m32);
Assert.Equal(m11, matrix[0, 0]);
Assert.Equal(m12, matrix[0, 1]);
Assert.Equal(m21, matrix[1, 0]);
Assert.Equal(m22, matrix[1, 1]);
Assert.Equal(m31, matrix[2, 0]);
Assert.Equal(m32, matrix[2, 1]);
}
[Theory]
[InlineData(0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f)]
[InlineData(1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f)]
[InlineData(3.1434343f, 1.1234123f, 0.1234123f, -0.1234123f, 3.1434343f, 1.1234123f)]
[InlineData(1.0000001f, 0.0000001f, 2.0000001f, 0.0000002f, 1.0000001f, 0.0000001f)]
public void Matrix3x2IndexerSetTest(float m11, float m12, float m21, float m22, float m31, float m32)
{
var matrix = new Matrix3x2(0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f);
matrix[0, 0] = m11;
matrix[0, 1] = m12;
matrix[1, 0] = m21;
matrix[1, 1] = m22;
matrix[2, 0] = m31;
matrix[2, 1] = m32;
Assert.Equal(m11, matrix[0, 0]);
Assert.Equal(m12, matrix[0, 1]);
Assert.Equal(m21, matrix[1, 0]);
Assert.Equal(m22, matrix[1, 1]);
Assert.Equal(m31, matrix[2, 0]);
Assert.Equal(m32, matrix[2, 1]);
}
// A test for Identity
[Fact]
public void Matrix3x2IdentityTest()
{
Matrix3x2 val = new Matrix3x2();
val.M11 = val.M22 = 1.0f;
Assert.True(MathHelper.Equal(val, Matrix3x2.Identity), "Matrix3x2.Indentity was not set correctly.");
}
// A test for Determinant
[Fact]
public void Matrix3x2DeterminantTest()
{
Matrix3x2 target = Matrix3x2.CreateRotation(MathHelper.ToRadians(30.0f));
float val = 1.0f;
float det = target.GetDeterminant();
Assert.True(MathHelper.Equal(val, det), "Matrix3x2.Determinant was not set correctly.");
}
// A test for Determinant
// Determinant test |A| = 1 / |A'|
[Fact]
public void Matrix3x2DeterminantTest1()
{
Matrix3x2 a = new Matrix3x2();
a.M11 = 5.0f;
a.M12 = 2.0f;
a.M21 = 12.0f;
a.M22 = 6.8f;
a.M31 = 6.5f;
a.M32 = 1.0f;
Matrix3x2 i;
Assert.True(Matrix3x2.Invert(a, out i));
float detA = a.GetDeterminant();
float detI = i.GetDeterminant();
float t = 1.0f / detI;
// only accurate to 3 precision
Assert.True(System.Math.Abs(detA - t) < 1e-3, "Matrix3x2.Determinant was not set correctly.");
// sanity check against 4x4 version
Assert.Equal(new Matrix4x4(a).GetDeterminant(), detA);
Assert.Equal(new Matrix4x4(i).GetDeterminant(), detI);
}
// A test for Invert (Matrix3x2)
[Fact]
public void Matrix3x2InvertTest()
{
Matrix3x2 mtx = Matrix3x2.CreateRotation(MathHelper.ToRadians(30.0f));
Matrix3x2 expected = new Matrix3x2();
expected.M11 = 0.8660254f;
expected.M12 = -0.5f;
expected.M21 = 0.5f;
expected.M22 = 0.8660254f;
expected.M31 = 0;
expected.M32 = 0;
Matrix3x2 actual;
Assert.True(Matrix3x2.Invert(mtx, out actual));
Assert.True(MathHelper.Equal(expected, actual), "Matrix3x2.Invert did not return the expected value.");
Matrix3x2 i = mtx * actual;
Assert.True(MathHelper.Equal(i, Matrix3x2.Identity), "Matrix3x2.Invert did not return the expected value.");
}
// A test for Invert (Matrix3x2)
[Fact]
public void Matrix3x2InvertIdentityTest()
{
Matrix3x2 mtx = Matrix3x2.Identity;
Matrix3x2 actual;
Assert.True(Matrix3x2.Invert(mtx, out actual));
Assert.True(MathHelper.Equal(actual, Matrix3x2.Identity));
}
// A test for Invert (Matrix3x2)
[Fact]
public void Matrix3x2InvertTranslationTest()
{
Matrix3x2 mtx = Matrix3x2.CreateTranslation(23, 42);
Matrix3x2 actual;
Assert.True(Matrix3x2.Invert(mtx, out actual));
Matrix3x2 i = mtx * actual;
Assert.True(MathHelper.Equal(i, Matrix3x2.Identity));
}
// A test for Invert (Matrix3x2)
[Fact]
public void Matrix3x2InvertRotationTest()
{
Matrix3x2 mtx = Matrix3x2.CreateRotation(2);
Matrix3x2 actual;
Assert.True(Matrix3x2.Invert(mtx, out actual));
Matrix3x2 i = mtx * actual;
Assert.True(MathHelper.Equal(i, Matrix3x2.Identity));
}
// A test for Invert (Matrix3x2)
[Fact]
public void Matrix3x2InvertScaleTest()
{
Matrix3x2 mtx = Matrix3x2.CreateScale(23, -42);
Matrix3x2 actual;
Assert.True(Matrix3x2.Invert(mtx, out actual));
Matrix3x2 i = mtx * actual;
Assert.True(MathHelper.Equal(i, Matrix3x2.Identity));
}
// A test for Invert (Matrix3x2)
[Fact]
public void Matrix3x2InvertAffineTest()
{
Matrix3x2 mtx = Matrix3x2.CreateRotation(2) *
Matrix3x2.CreateScale(23, -42) *
Matrix3x2.CreateTranslation(17, 53);
Matrix3x2 actual;
Assert.True(Matrix3x2.Invert(mtx, out actual));
Matrix3x2 i = mtx * actual;
Assert.True(MathHelper.Equal(i, Matrix3x2.Identity));
}
// A test for CreateRotation (float)
[Fact]
public void Matrix3x2CreateRotationTest()
{
float radians = MathHelper.ToRadians(50.0f);
Matrix3x2 expected = new Matrix3x2();
expected.M11 = 0.642787635f;
expected.M12 = 0.766044438f;
expected.M21 = -0.766044438f;
expected.M22 = 0.642787635f;
Matrix3x2 actual;
actual = Matrix3x2.CreateRotation(radians);
Assert.True(MathHelper.Equal(expected, actual), "Matrix3x2.CreateRotation did not return the expected value.");
}
// A test for CreateRotation (float, Vector2f)
[Fact]
public void Matrix3x2CreateRotationCenterTest()
{
float radians = MathHelper.ToRadians(30.0f);
Vector2 center = new Vector2(23, 42);
Matrix3x2 rotateAroundZero = Matrix3x2.CreateRotation(radians, Vector2.Zero);
Matrix3x2 rotateAroundZeroExpected = Matrix3x2.CreateRotation(radians);
Assert.True(MathHelper.Equal(rotateAroundZero, rotateAroundZeroExpected));
Matrix3x2 rotateAroundCenter = Matrix3x2.CreateRotation(radians, center);
Matrix3x2 rotateAroundCenterExpected = Matrix3x2.CreateTranslation(-center) * Matrix3x2.CreateRotation(radians) * Matrix3x2.CreateTranslation(center);
Assert.True(MathHelper.Equal(rotateAroundCenter, rotateAroundCenterExpected));
}
// A test for CreateRotation (float)
[Fact]
public void Matrix3x2CreateRotationRightAngleTest()
{
// 90 degree rotations must be exact!
Matrix3x2 actual = Matrix3x2.CreateRotation(0);
Assert.Equal(new Matrix3x2(1, 0, 0, 1, 0, 0), actual);
actual = Matrix3x2.CreateRotation(MathHelper.Pi / 2);
Assert.Equal(new Matrix3x2(0, 1, -1, 0, 0, 0), actual);
actual = Matrix3x2.CreateRotation(MathHelper.Pi);
Assert.Equal(new Matrix3x2(-1, 0, 0, -1, 0, 0), actual);
actual = Matrix3x2.CreateRotation(MathHelper.Pi * 3 / 2);
Assert.Equal(new Matrix3x2(0, -1, 1, 0, 0, 0), actual);
actual = Matrix3x2.CreateRotation(MathHelper.Pi * 2);
Assert.Equal(new Matrix3x2(1, 0, 0, 1, 0, 0), actual);
actual = Matrix3x2.CreateRotation(MathHelper.Pi * 5 / 2);
Assert.Equal(new Matrix3x2(0, 1, -1, 0, 0, 0), actual);
actual = Matrix3x2.CreateRotation(-MathHelper.Pi / 2);
Assert.Equal(new Matrix3x2(0, -1, 1, 0, 0, 0), actual);
// But merely close-to-90 rotations should not be excessively clamped.
float delta = MathHelper.ToRadians(0.01f);
actual = Matrix3x2.CreateRotation(MathHelper.Pi + delta);
Assert.False(MathHelper.Equal(new Matrix3x2(-1, 0, 0, -1, 0, 0), actual));
actual = Matrix3x2.CreateRotation(MathHelper.Pi - delta);
Assert.False(MathHelper.Equal(new Matrix3x2(-1, 0, 0, -1, 0, 0), actual));
}
// A test for CreateRotation (float, Vector2f)
[Fact]
public void Matrix3x2CreateRotationRightAngleCenterTest()
{
Vector2 center = new Vector2(3, 7);
// 90 degree rotations must be exact!
Matrix3x2 actual = Matrix3x2.CreateRotation(0, center);
Assert.Equal(new Matrix3x2(1, 0, 0, 1, 0, 0), actual);
actual = Matrix3x2.CreateRotation(MathHelper.Pi / 2, center);
Assert.Equal(new Matrix3x2(0, 1, -1, 0, 10, 4), actual);
actual = Matrix3x2.CreateRotation(MathHelper.Pi, center);
Assert.Equal(new Matrix3x2(-1, 0, 0, -1, 6, 14), actual);
actual = Matrix3x2.CreateRotation(MathHelper.Pi * 3 / 2, center);
Assert.Equal(new Matrix3x2(0, -1, 1, 0, -4, 10), actual);
actual = Matrix3x2.CreateRotation(MathHelper.Pi * 2, center);
Assert.Equal(new Matrix3x2(1, 0, 0, 1, 0, 0), actual);
actual = Matrix3x2.CreateRotation(MathHelper.Pi * 5 / 2, center);
Assert.Equal(new Matrix3x2(0, 1, -1, 0, 10, 4), actual);
actual = Matrix3x2.CreateRotation(-MathHelper.Pi / 2, center);
Assert.Equal(new Matrix3x2(0, -1, 1, 0, -4, 10), actual);
// But merely close-to-90 rotations should not be excessively clamped.
float delta = MathHelper.ToRadians(0.01f);
actual = Matrix3x2.CreateRotation(MathHelper.Pi + delta, center);
Assert.False(MathHelper.Equal(new Matrix3x2(-1, 0, 0, -1, 6, 14), actual));
actual = Matrix3x2.CreateRotation(MathHelper.Pi - delta, center);
Assert.False(MathHelper.Equal(new Matrix3x2(-1, 0, 0, -1, 6, 14), actual));
}
// A test for Invert (Matrix3x2)
// Non invertible matrix - determinant is zero - singular matrix
[Fact]
public void Matrix3x2InvertTest1()
{
Matrix3x2 a = new Matrix3x2();
a.M11 = 0.0f;
a.M12 = 2.0f;
a.M21 = 0.0f;
a.M22 = 4.0f;
a.M31 = 5.0f;
a.M32 = 6.0f;
float detA = a.GetDeterminant();
Assert.True(MathHelper.Equal(detA, 0.0f), "Matrix3x2.Invert did not return the expected value.");
Matrix3x2 actual;
Assert.False(Matrix3x2.Invert(a, out actual));
// all the elements in Actual is NaN
Assert.True(
float.IsNaN(actual.M11) && float.IsNaN(actual.M12) &&
float.IsNaN(actual.M21) && float.IsNaN(actual.M22) &&
float.IsNaN(actual.M31) && float.IsNaN(actual.M32)
, "Matrix3x2.Invert did not return the expected value.");
}
// A test for Lerp (Matrix3x2, Matrix3x2, float)
[Fact]
public void Matrix3x2LerpTest()
{
Matrix3x2 a = new Matrix3x2();
a.M11 = 11.0f;
a.M12 = 12.0f;
a.M21 = 21.0f;
a.M22 = 22.0f;
a.M31 = 31.0f;
a.M32 = 32.0f;
Matrix3x2 b = GenerateIncrementalMatrixNumber();
float t = 0.5f;
Matrix3x2 expected = new Matrix3x2();
expected.M11 = a.M11 + (b.M11 - a.M11) * t;
expected.M12 = a.M12 + (b.M12 - a.M12) * t;
expected.M21 = a.M21 + (b.M21 - a.M21) * t;
expected.M22 = a.M22 + (b.M22 - a.M22) * t;
expected.M31 = a.M31 + (b.M31 - a.M31) * t;
expected.M32 = a.M32 + (b.M32 - a.M32) * t;
Matrix3x2 actual;
actual = Matrix3x2.Lerp(a, b, t);
Assert.True(MathHelper.Equal(expected, actual), "Matrix3x2.Lerp did not return the expected value.");
}
// A test for operator - (Matrix3x2)
[Fact]
public void Matrix3x2UnaryNegationTest()
{
Matrix3x2 a = GenerateIncrementalMatrixNumber();
Matrix3x2 expected = new Matrix3x2();
expected.M11 = -1.0f;
expected.M12 = -2.0f;
expected.M21 = -3.0f;
expected.M22 = -4.0f;
expected.M31 = -5.0f;
expected.M32 = -6.0f;
Matrix3x2 actual = -a;
Assert.True(MathHelper.Equal(expected, actual), "Matrix3x2.operator - did not return the expected value.");
}
// A test for operator - (Matrix3x2, Matrix3x2)
[Fact]
public void Matrix3x2SubtractionTest()
{
Matrix3x2 a = GenerateIncrementalMatrixNumber();
Matrix3x2 b = GenerateIncrementalMatrixNumber(-3.0f);
Matrix3x2 expected = new Matrix3x2();
expected.M11 = a.M11 - b.M11;
expected.M12 = a.M12 - b.M12;
expected.M21 = a.M21 - b.M21;
expected.M22 = a.M22 - b.M22;
expected.M31 = a.M31 - b.M31;
expected.M32 = a.M32 - b.M32;
Matrix3x2 actual = a - b;
Assert.True(MathHelper.Equal(expected, actual), "Matrix3x2.operator - did not return the expected value.");
}
// A test for operator * (Matrix3x2, Matrix3x2)
[Fact]
public void Matrix3x2MultiplyTest1()
{
Matrix3x2 a = GenerateIncrementalMatrixNumber();
Matrix3x2 b = GenerateIncrementalMatrixNumber(-3.0f);
Matrix3x2 expected = new Matrix3x2();
expected.M11 = a.M11 * b.M11 + a.M12 * b.M21;
expected.M12 = a.M11 * b.M12 + a.M12 * b.M22;
expected.M21 = a.M21 * b.M11 + a.M22 * b.M21;
expected.M22 = a.M21 * b.M12 + a.M22 * b.M22;
expected.M31 = a.M31 * b.M11 + a.M32 * b.M21 + b.M31;
expected.M32 = a.M31 * b.M12 + a.M32 * b.M22 + b.M32;
Matrix3x2 actual = a * b;
Assert.True(MathHelper.Equal(expected, actual), "Matrix3x2.operator * did not return the expected value.");
// Sanity check by comparison with 4x4 multiply.
a = Matrix3x2.CreateRotation(MathHelper.ToRadians(30)) * Matrix3x2.CreateTranslation(23, 42);
b = Matrix3x2.CreateScale(3, 7) * Matrix3x2.CreateTranslation(666, -1);
actual = a * b;
Matrix4x4 a44 = new Matrix4x4(a);
Matrix4x4 b44 = new Matrix4x4(b);
Matrix4x4 expected44 = a44 * b44;
Matrix4x4 actual44 = new Matrix4x4(actual);
Assert.True(MathHelper.Equal(expected44, actual44), "Matrix3x2.operator * did not return the expected value.");
}
// A test for operator * (Matrix3x2, Matrix3x2)
// Multiply with identity matrix
[Fact]
public void Matrix3x2MultiplyTest4()
{
Matrix3x2 a = new Matrix3x2();
a.M11 = 1.0f;
a.M12 = 2.0f;
a.M21 = 5.0f;
a.M22 = -6.0f;
a.M31 = 9.0f;
a.M32 = 10.0f;
Matrix3x2 b = new Matrix3x2();
b = Matrix3x2.Identity;
Matrix3x2 expected = a;
Matrix3x2 actual = a * b;
Assert.True(MathHelper.Equal(expected, actual), "Matrix3x2.operator * did not return the expected value.");
}
// A test for operator + (Matrix3x2, Matrix3x2)
[Fact]
public void Matrix3x2AdditionTest()
{
Matrix3x2 a = GenerateIncrementalMatrixNumber();
Matrix3x2 b = GenerateIncrementalMatrixNumber(-3.0f);
Matrix3x2 expected = new Matrix3x2();
expected.M11 = a.M11 + b.M11;
expected.M12 = a.M12 + b.M12;
expected.M21 = a.M21 + b.M21;
expected.M22 = a.M22 + b.M22;
expected.M31 = a.M31 + b.M31;
expected.M32 = a.M32 + b.M32;
Matrix3x2 actual;
actual = a + b;
Assert.True(MathHelper.Equal(expected, actual), "Matrix3x2.operator + did not return the expected value.");
}
// A test for ToString ()
[Fact]
public void Matrix3x2ToStringTest()
{
Matrix3x2 a = new Matrix3x2();
a.M11 = 11.0f;
a.M12 = -12.0f;
a.M21 = 21.0f;
a.M22 = 22.0f;
a.M31 = 31.0f;
a.M32 = 32.0f;
string expected = "{ {M11:11 M12:-12} " +
"{M21:21 M22:22} " +
"{M31:31 M32:32} }";
string actual;
actual = a.ToString();
Assert.Equal(expected, actual);
}
// A test for Add (Matrix3x2, Matrix3x2)
[Fact]
public void Matrix3x2AddTest()
{
Matrix3x2 a = GenerateIncrementalMatrixNumber();
Matrix3x2 b = GenerateIncrementalMatrixNumber(-3.0f);
Matrix3x2 expected = new Matrix3x2();
expected.M11 = a.M11 + b.M11;
expected.M12 = a.M12 + b.M12;
expected.M21 = a.M21 + b.M21;
expected.M22 = a.M22 + b.M22;
expected.M31 = a.M31 + b.M31;
expected.M32 = a.M32 + b.M32;
Matrix3x2 actual;
actual = Matrix3x2.Add(a, b);
Assert.Equal(expected, actual);
}
// A test for Equals (object)
[Fact]
public void Matrix3x2EqualsTest()
{
Matrix3x2 a = GenerateIncrementalMatrixNumber();
Matrix3x2 b = GenerateIncrementalMatrixNumber();
// case 1: compare between same values
object obj = b;
bool expected = true;
bool actual = a.Equals(obj);
Assert.Equal(expected, actual);
// case 2: compare between different values
b.M11 = 11.0f;
obj = b;
expected = false;
actual = a.Equals(obj);
Assert.Equal(expected, actual);
// case 3: compare between different types.
obj = new Vector4();
expected = false;
actual = a.Equals(obj);
Assert.Equal(expected, actual);
// case 3: compare against null.
obj = null;
expected = false;
actual = a.Equals(obj);
Assert.Equal(expected, actual);
}
// A test for GetHashCode ()
[Fact]
public void Matrix3x2GetHashCodeTest()
{
Matrix3x2 target = GenerateIncrementalMatrixNumber();
int expected = HashCode.Combine(
new Vector2(target.M11, target.M12),
new Vector2(target.M21, target.M22),
new Vector2(target.M31, target.M32)
);
int actual = target.GetHashCode();
Assert.Equal(expected, actual);
}
// A test for Multiply (Matrix3x2, Matrix3x2)
[Fact]
public void Matrix3x2MultiplyTest3()
{
Matrix3x2 a = GenerateIncrementalMatrixNumber();
Matrix3x2 b = GenerateIncrementalMatrixNumber(-3.0f);
Matrix3x2 expected = new Matrix3x2();
expected.M11 = a.M11 * b.M11 + a.M12 * b.M21;
expected.M12 = a.M11 * b.M12 + a.M12 * b.M22;
expected.M21 = a.M21 * b.M11 + a.M22 * b.M21;
expected.M22 = a.M21 * b.M12 + a.M22 * b.M22;
expected.M31 = a.M31 * b.M11 + a.M32 * b.M21 + b.M31;
expected.M32 = a.M31 * b.M12 + a.M32 * b.M22 + b.M32;
Matrix3x2 actual;
actual = Matrix3x2.Multiply(a, b);
Assert.Equal(expected, actual);
// Sanity check by comparison with 4x4 multiply.
a = Matrix3x2.CreateRotation(MathHelper.ToRadians(30)) * Matrix3x2.CreateTranslation(23, 42);
b = Matrix3x2.CreateScale(3, 7) * Matrix3x2.CreateTranslation(666, -1);
actual = Matrix3x2.Multiply(a, b);
Matrix4x4 a44 = new Matrix4x4(a);
Matrix4x4 b44 = new Matrix4x4(b);
Matrix4x4 expected44 = Matrix4x4.Multiply(a44, b44);
Matrix4x4 actual44 = new Matrix4x4(actual);
Assert.True(MathHelper.Equal(expected44, actual44), "Matrix3x2.Multiply did not return the expected value.");
}
// A test for Multiply (Matrix3x2, float)
[Fact]
public void Matrix3x2MultiplyTest5()
{
Matrix3x2 a = GenerateIncrementalMatrixNumber();
Matrix3x2 expected = new Matrix3x2(3, 6, 9, 12, 15, 18);
Matrix3x2 actual = Matrix3x2.Multiply(a, 3);
Assert.Equal(expected, actual);
}
// A test for Multiply (Matrix3x2, float)
[Fact]
public void Matrix3x2MultiplyTest6()
{
Matrix3x2 a = GenerateIncrementalMatrixNumber();
Matrix3x2 expected = new Matrix3x2(3, 6, 9, 12, 15, 18);
Matrix3x2 actual = a * 3;
Assert.Equal(expected, actual);
}
// A test for Negate (Matrix3x2)
[Fact]
public void Matrix3x2NegateTest()
{
Matrix3x2 m = GenerateIncrementalMatrixNumber();
Matrix3x2 expected = new Matrix3x2();
expected.M11 = -1.0f;
expected.M12 = -2.0f;
expected.M21 = -3.0f;
expected.M22 = -4.0f;
expected.M31 = -5.0f;
expected.M32 = -6.0f;
Matrix3x2 actual;
actual = Matrix3x2.Negate(m);
Assert.Equal(expected, actual);
}
// A test for operator != (Matrix3x2, Matrix3x2)
[Fact]
public void Matrix3x2InequalityTest()
{
Matrix3x2 a = GenerateIncrementalMatrixNumber();
Matrix3x2 b = GenerateIncrementalMatrixNumber();
// case 1: compare between same values
bool expected = false;
bool actual = a != b;
Assert.Equal(expected, actual);
// case 2: compare between different values
b.M11 = 11.0f;
expected = true;
actual = a != b;
Assert.Equal(expected, actual);
}
// A test for operator == (Matrix3x2, Matrix3x2)
[Fact]
public void Matrix3x2EqualityTest()
{
Matrix3x2 a = GenerateIncrementalMatrixNumber();
Matrix3x2 b = GenerateIncrementalMatrixNumber();
// case 1: compare between same values
bool expected = true;
bool actual = a == b;
Assert.Equal(expected, actual);
// case 2: compare between different values
b.M11 = 11.0f;
expected = false;
actual = a == b;
Assert.Equal(expected, actual);
}
// A test for Subtract (Matrix3x2, Matrix3x2)
[Fact]
public void Matrix3x2SubtractTest()
{
Matrix3x2 a = GenerateIncrementalMatrixNumber();
Matrix3x2 b = GenerateIncrementalMatrixNumber(-3.0f);
Matrix3x2 expected = new Matrix3x2();
expected.M11 = a.M11 - b.M11;
expected.M12 = a.M12 - b.M12;
expected.M21 = a.M21 - b.M21;
expected.M22 = a.M22 - b.M22;
expected.M31 = a.M31 - b.M31;
expected.M32 = a.M32 - b.M32;
Matrix3x2 actual;
actual = Matrix3x2.Subtract(a, b);
Assert.Equal(expected, actual);
}
// A test for CreateScale (Vector2f)
[Fact]
public void Matrix3x2CreateScaleTest1()
{
Vector2 scales = new Vector2(2.0f, 3.0f);
Matrix3x2 expected = new Matrix3x2(
2.0f, 0.0f,
0.0f, 3.0f,
0.0f, 0.0f);
Matrix3x2 actual = Matrix3x2.CreateScale(scales);
Assert.Equal(expected, actual);
}
// A test for CreateScale (Vector2f, Vector2f)
[Fact]
public void Matrix3x2CreateScaleCenterTest1()
{
Vector2 scale = new Vector2(3, 4);
Vector2 center = new Vector2(23, 42);
Matrix3x2 scaleAroundZero = Matrix3x2.CreateScale(scale, Vector2.Zero);
Matrix3x2 scaleAroundZeroExpected = Matrix3x2.CreateScale(scale);
Assert.True(MathHelper.Equal(scaleAroundZero, scaleAroundZeroExpected));
Matrix3x2 scaleAroundCenter = Matrix3x2.CreateScale(scale, center);
Matrix3x2 scaleAroundCenterExpected = Matrix3x2.CreateTranslation(-center) * Matrix3x2.CreateScale(scale) * Matrix3x2.CreateTranslation(center);
Assert.True(MathHelper.Equal(scaleAroundCenter, scaleAroundCenterExpected));
}
// A test for CreateScale (float)
[Fact]
public void Matrix3x2CreateScaleTest2()
{
float scale = 2.0f;
Matrix3x2 expected = new Matrix3x2(
2.0f, 0.0f,
0.0f, 2.0f,
0.0f, 0.0f);
Matrix3x2 actual = Matrix3x2.CreateScale(scale);
Assert.Equal(expected, actual);
}
// A test for CreateScale (float, Vector2f)
[Fact]
public void Matrix3x2CreateScaleCenterTest2()
{
float scale = 5;
Vector2 center = new Vector2(23, 42);
Matrix3x2 scaleAroundZero = Matrix3x2.CreateScale(scale, Vector2.Zero);
Matrix3x2 scaleAroundZeroExpected = Matrix3x2.CreateScale(scale);
Assert.True(MathHelper.Equal(scaleAroundZero, scaleAroundZeroExpected));
Matrix3x2 scaleAroundCenter = Matrix3x2.CreateScale(scale, center);
Matrix3x2 scaleAroundCenterExpected = Matrix3x2.CreateTranslation(-center) * Matrix3x2.CreateScale(scale) * Matrix3x2.CreateTranslation(center);
Assert.True(MathHelper.Equal(scaleAroundCenter, scaleAroundCenterExpected));
}
// A test for CreateScale (float, float)
[Fact]
public void Matrix3x2CreateScaleTest3()
{
float xScale = 2.0f;
float yScale = 3.0f;
Matrix3x2 expected = new Matrix3x2(
2.0f, 0.0f,
0.0f, 3.0f,
0.0f, 0.0f);
Matrix3x2 actual = Matrix3x2.CreateScale(xScale, yScale);
Assert.Equal(expected, actual);
}
// A test for CreateScale (float, float, Vector2f)
[Fact]
public void Matrix3x2CreateScaleCenterTest3()
{
Vector2 scale = new Vector2(3, 4);
Vector2 center = new Vector2(23, 42);
Matrix3x2 scaleAroundZero = Matrix3x2.CreateScale(scale.X, scale.Y, Vector2.Zero);
Matrix3x2 scaleAroundZeroExpected = Matrix3x2.CreateScale(scale.X, scale.Y);
Assert.True(MathHelper.Equal(scaleAroundZero, scaleAroundZeroExpected));
Matrix3x2 scaleAroundCenter = Matrix3x2.CreateScale(scale.X, scale.Y, center);
Matrix3x2 scaleAroundCenterExpected = Matrix3x2.CreateTranslation(-center) * Matrix3x2.CreateScale(scale.X, scale.Y) * Matrix3x2.CreateTranslation(center);
Assert.True(MathHelper.Equal(scaleAroundCenter, scaleAroundCenterExpected));
}
// A test for CreateTranslation (Vector2f)
[Fact]
public void Matrix3x2CreateTranslationTest1()
{
Vector2 position = new Vector2(2.0f, 3.0f);
Matrix3x2 expected = new Matrix3x2(
1.0f, 0.0f,
0.0f, 1.0f,
2.0f, 3.0f);
Matrix3x2 actual = Matrix3x2.CreateTranslation(position);
Assert.Equal(expected, actual);
}
// A test for CreateTranslation (float, float)
[Fact]
public void Matrix3x2CreateTranslationTest2()
{
float xPosition = 2.0f;
float yPosition = 3.0f;
Matrix3x2 expected = new Matrix3x2(
1.0f, 0.0f,
0.0f, 1.0f,
2.0f, 3.0f);
Matrix3x2 actual = Matrix3x2.CreateTranslation(xPosition, yPosition);
Assert.Equal(expected, actual);
}
// A test for Translation
[Fact]
public void Matrix3x2TranslationTest()
{
Matrix3x2 a = GenerateTestMatrix();
Matrix3x2 b = a;
// Transformed vector that has same semantics of property must be same.
Vector2 val = new Vector2(a.M31, a.M32);
Assert.Equal(val, a.Translation);
// Set value and get value must be same.
val = new Vector2(1.0f, 2.0f);
a.Translation = val;
Assert.Equal(val, a.Translation);
// Make sure it only modifies expected value of matrix.
Assert.True(
a.M11 == b.M11 && a.M12 == b.M12 &&
a.M21 == b.M21 && a.M22 == b.M22 &&
a.M31 != b.M31 && a.M32 != b.M32,
"Matrix3x2.Translation modified unexpected value of matrix.");
}
// A test for Equals (Matrix3x2)
[Fact]
public void Matrix3x2EqualsTest1()
{
Matrix3x2 a = GenerateIncrementalMatrixNumber();
Matrix3x2 b = GenerateIncrementalMatrixNumber();
// case 1: compare between same values
bool expected = true;
bool actual = a.Equals(b);
Assert.Equal(expected, actual);
// case 2: compare between different values
b.M11 = 11.0f;
expected = false;
actual = a.Equals(b);
Assert.Equal(expected, actual);
}
// A test for CreateSkew (float, float)
[Fact]
public void Matrix3x2CreateSkewIdentityTest()
{
Matrix3x2 expected = Matrix3x2.Identity;
Matrix3x2 actual = Matrix3x2.CreateSkew(0, 0);
Assert.Equal(expected, actual);
}
// A test for CreateSkew (float, float)
[Fact]
public void Matrix3x2CreateSkewXTest()
{
Matrix3x2 expected = new Matrix3x2(1, 0, -0.414213562373095f, 1, 0, 0);
Matrix3x2 actual = Matrix3x2.CreateSkew(-MathHelper.Pi / 8, 0);
Assert.True(MathHelper.Equal(expected, actual));
expected = new Matrix3x2(1, 0, 0.414213562373095f, 1, 0, 0);
actual = Matrix3x2.CreateSkew(MathHelper.Pi / 8, 0);
Assert.True(MathHelper.Equal(expected, actual));
Vector2 result = Vector2.Transform(new Vector2(0, 0), actual);
Assert.True(MathHelper.Equal(new Vector2(0, 0), result));
result = Vector2.Transform(new Vector2(0, 1), actual);
Assert.True(MathHelper.Equal(new Vector2(0.414213568f, 1), result));
result = Vector2.Transform(new Vector2(0, -1), actual);
Assert.True(MathHelper.Equal(new Vector2(-0.414213568f, -1), result));
result = Vector2.Transform(new Vector2(3, 10), actual);
Assert.True(MathHelper.Equal(new Vector2(7.14213568f, 10), result));
}
// A test for CreateSkew (float, float)
[Fact]
public void Matrix3x2CreateSkewYTest()
{
Matrix3x2 expected = new Matrix3x2(1, -0.414213562373095f, 0, 1, 0, 0);
Matrix3x2 actual = Matrix3x2.CreateSkew(0, -MathHelper.Pi / 8);
Assert.True(MathHelper.Equal(expected, actual));
expected = new Matrix3x2(1, 0.414213562373095f, 0, 1, 0, 0);
actual = Matrix3x2.CreateSkew(0, MathHelper.Pi / 8);
Assert.True(MathHelper.Equal(expected, actual));
Vector2 result = Vector2.Transform(new Vector2(0, 0), actual);
Assert.True(MathHelper.Equal(new Vector2(0, 0), result));
result = Vector2.Transform(new Vector2(1, 0), actual);
Assert.True(MathHelper.Equal(new Vector2(1, 0.414213568f), result));
result = Vector2.Transform(new Vector2(-1, 0), actual);
Assert.True(MathHelper.Equal(new Vector2(-1, -0.414213568f), result));
result = Vector2.Transform(new Vector2(10, 3), actual);
Assert.True(MathHelper.Equal(new Vector2(10, 7.14213568f), result));
}
// A test for CreateSkew (float, float)
[Fact]
public void Matrix3x2CreateSkewXYTest()
{
Matrix3x2 expected = new Matrix3x2(1, -0.414213562373095f, 1, 1, 0, 0);
Matrix3x2 actual = Matrix3x2.CreateSkew(MathHelper.Pi / 4, -MathHelper.Pi / 8);
Assert.True(MathHelper.Equal(expected, actual));
Vector2 result = Vector2.Transform(new Vector2(0, 0), actual);
Assert.True(MathHelper.Equal(new Vector2(0, 0), result));
result = Vector2.Transform(new Vector2(1, 0), actual);
Assert.True(MathHelper.Equal(new Vector2(1, -0.414213562373095f), result));
result = Vector2.Transform(new Vector2(0, 1), actual);
Assert.True(MathHelper.Equal(new Vector2(1, 1), result));
result = Vector2.Transform(new Vector2(1, 1), actual);
Assert.True(MathHelper.Equal(new Vector2(2, 0.585786437626905f), result));
}
// A test for CreateSkew (float, float, Vector2f)
[Fact]
public void Matrix3x2CreateSkewCenterTest()
{
float skewX = 1, skewY = 2;
Vector2 center = new Vector2(23, 42);
Matrix3x2 skewAroundZero = Matrix3x2.CreateSkew(skewX, skewY, Vector2.Zero);
Matrix3x2 skewAroundZeroExpected = Matrix3x2.CreateSkew(skewX, skewY);
Assert.True(MathHelper.Equal(skewAroundZero, skewAroundZeroExpected));
Matrix3x2 skewAroundCenter = Matrix3x2.CreateSkew(skewX, skewY, center);
Matrix3x2 skewAroundCenterExpected = Matrix3x2.CreateTranslation(-center) * Matrix3x2.CreateSkew(skewX, skewY) * Matrix3x2.CreateTranslation(center);
Assert.True(MathHelper.Equal(skewAroundCenter, skewAroundCenterExpected));
}
// A test for IsIdentity
[Fact]
public void Matrix3x2IsIdentityTest()
{
Assert.True(Matrix3x2.Identity.IsIdentity);
Assert.True(new Matrix3x2(1, 0, 0, 1, 0, 0).IsIdentity);
Assert.False(new Matrix3x2(0, 0, 0, 1, 0, 0).IsIdentity);
Assert.False(new Matrix3x2(1, 1, 0, 1, 0, 0).IsIdentity);
Assert.False(new Matrix3x2(1, 0, 1, 1, 0, 0).IsIdentity);
Assert.False(new Matrix3x2(1, 0, 0, 0, 0, 0).IsIdentity);
Assert.False(new Matrix3x2(1, 0, 0, 1, 1, 0).IsIdentity);
Assert.False(new Matrix3x2(1, 0, 0, 1, 0, 1).IsIdentity);
}
// A test for Matrix3x2 comparison involving NaN values
[Fact]
public void Matrix3x2EqualsNaNTest()
{
Matrix3x2 a = new Matrix3x2(float.NaN, 0, 0, 0, 0, 0);
Matrix3x2 b = new Matrix3x2(0, float.NaN, 0, 0, 0, 0);
Matrix3x2 c = new Matrix3x2(0, 0, float.NaN, 0, 0, 0);
Matrix3x2 d = new Matrix3x2(0, 0, 0, float.NaN, 0, 0);
Matrix3x2 e = new Matrix3x2(0, 0, 0, 0, float.NaN, 0);
Matrix3x2 f = new Matrix3x2(0, 0, 0, 0, 0, float.NaN);
Assert.False(a == new Matrix3x2());
Assert.False(b == new Matrix3x2());
Assert.False(c == new Matrix3x2());