-
Notifications
You must be signed in to change notification settings - Fork 524
/
Copy pathCommon.Tests.cs
1221 lines (1092 loc) · 44.5 KB
/
Common.Tests.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
#define MarshalCharAsManagedSByte // Tests need to behave differently when char is mapped to sbyte
using System;
using System.Reflection;
using CommonTest;
using NUnit.Framework;
using Enum = CommonTest.Enum;
[TestFixture]
public class CommonTests
{
[Test]
public unsafe void TestCodeGeneration()
{
#pragma warning disable 0168 // warning CS0168: The variable `foo' is declared but never used
#pragma warning disable 0219 // warning CS0219: The variable `foo' is assigned but its value is never used
using (var changedAccessOfInheritedProperty = new ChangedAccessOfInheritedProperty())
{
Assert.That(changedAccessOfInheritedProperty.Property, Is.EqualTo(2));
}
Foo.NestedAbstract a;
Foo.RenamedEmptyEnum.EmptyEnum1.GetHashCode();
using (var foo = new Foo(5))
{
Assert.That(foo.B, Is.EqualTo(5));
Bar bar = foo;
Assert.IsTrue(Bar.Item.Item1 == bar);
foo.FieldOfTypedefPrimitivePointer = null;
using (var hasOverloadsWithDifferentPointerKindsToSameType =
new HasOverloadsWithDifferentPointerKindsToSameType())
{
hasOverloadsWithDifferentPointerKindsToSameType.Overload(foo, 0);
using (var foo2 = new Foo2())
hasOverloadsWithDifferentPointerKindsToSameType.Overload(foo2, 0);
}
}
using (var overridesNonDirectVirtual = new OverridesNonDirectVirtual())
{
using (var foo = new Foo())
{
Assert.That(overridesNonDirectVirtual.RetInt(foo), Is.EqualTo(3));
Assert.That(foo.FooPtr, Is.EqualTo(1));
}
}
using (var derivedFromTemplateInstantiationWithVirtual = new DerivedFromTemplateInstantiationWithVirtual())
{
}
using (var hasProtectedEnum = new HasProtectedEnum())
{
}
using (var hasPropertyNamedAsParent = new HasPropertyNamedAsParent())
{
hasPropertyNamedAsParent.hasPropertyNamedAsParent.GetHashCode();
}
EnumWithUnderscores.lOWER_BEFORE_CAPITAL.GetHashCode();
EnumWithUnderscores.UnderscoreAtEnd.GetHashCode();
EnumWithUnderscores.CAPITALS_More.GetHashCode();
EnumWithUnderscores.UsesDigits1_0.GetHashCode();
ItemsDifferByCase.Case_a.GetHashCode();
ItemsDifferByCase.CaseA.GetHashCode();
Enum.NAME_A.GetHashCode();
Enum.NAME__A.GetHashCode();
new AmbiguousParamNames(0, 0).Dispose();
Common.SMallFollowedByCapital();
Common.IntegerOverload(0);
Common.IntegerOverload((uint) 0);
Common.TakeVoidStarStar(null);
Common.OverloadPointer(IntPtr.Zero, 1);
using (new DerivedFromSecondaryBaseWithIgnoredVirtualMethod()) { }
#pragma warning restore 0168
#pragma warning restore 0219
}
[Test]
public void TestHello()
{
using (var hello = new Hello())
{
hello.PrintHello("Hello world");
Assert.That(hello.Add(1, 1), Is.EqualTo(2));
Assert.That(hello.Add(5, 5), Is.EqualTo(10));
Assert.IsTrue(hello.Test1(3, 3.0f));
Assert.IsFalse(hello.Test1(2, 3.0f));
var foo = new Foo { A = 4, B = 7 };
Assert.That(hello.AddFoo(foo), Is.EqualTo(11));
Assert.That(hello.AddFooPtr(foo), Is.EqualTo(11));
Assert.That(hello.AddFooPtr(foo), Is.EqualTo(11));
Assert.That(hello.AddFooRef(foo), Is.EqualTo(11));
unsafe
{
var pointer = foo.SomePointer;
var pointerPointer = foo.SomePointerPointer;
for (int i = 0; i < 4; i++)
{
Assert.AreEqual(i, pointer[i]);
Assert.AreEqual(i, (*pointerPointer)[i]);
}
}
var bar = new Bar { A = 4, B = 7 };
Assert.That(hello.AddBar(bar), Is.EqualTo(11));
Assert.That(bar.RetItem1(), Is.EqualTo(Bar.Item.Item1));
using (var retFoo = hello.RetFoo(7, 2.0f))
{
Assert.That(retFoo.A, Is.EqualTo(7));
Assert.That(retFoo.B, Is.EqualTo(2.0));
}
using (var foo2 = new Foo2 { A = 4, B = 2, C = 3 })
{
Assert.That(hello.AddFoo(foo2), Is.EqualTo(6));
Assert.That(hello.AddFoo2(foo2), Is.EqualTo(9));
}
var bar2 = new Bar2 { A = 4, B = 7, C = 3 };
Assert.That(hello.AddBar2(bar2), Is.EqualTo(14));
Assert.That(hello.RetEnum(Enum.A), Is.EqualTo(0));
Assert.That(hello.RetEnum(Enum.B), Is.EqualTo(2));
Assert.That(hello.RetEnum(Enum.C), Is.EqualTo(5));
//Assert.That(hello.RetEnum(Enum.D), Is.EqualTo(-2147483648));
Assert.That(hello.RetEnum(Enum.E), Is.EqualTo(1));
Assert.That(hello.RetEnum(Enum.F), Is.EqualTo(-9));
}
}
[Test]
public void TestPrimitiveConstCharStringInOut()
{
using (var hello = new Hello())
{
hello.StringOut(out string str);
Assert.That(str, Is.EqualTo("HelloStringOut"));
hello.StringOutRef(out str);
Assert.That(str, Is.EqualTo("HelloStringOutRef"));
str = "Hello";
hello.StringInOut(ref str);
Assert.That(str, Is.EqualTo("StringInOut"));
str = "Hello";
hello.StringInOutRef(ref str);
Assert.That(str, Is.EqualTo("StringInOutRef"));
hello.StringTypedef(str);
}
}
[Test]
public void TestPrimitiveOutParameters()
{
using (var hello = new Hello())
{
Assert.That(hello.TestPrimitiveOut(out float f), Is.True);
Assert.That(f, Is.EqualTo(10.0f));
}
}
[Test]
public void TestPrimitiveOutRefParameters()
{
using (var hello = new Hello())
{
Assert.That(hello.TestPrimitiveOutRef(out float f), Is.True);
Assert.That(f, Is.EqualTo(10.0f));
}
}
public void TestPrimitiveInOutParameters()
{
using (var hello = new Hello())
{
int i = 10;
Assert.That(hello.TestPrimitiveInOut(ref i), Is.True);
Assert.That(i, Is.EqualTo(20));
}
}
[Test]
public void TestPrimitiveInOutRefParameters()
{
using (var hello = new Hello())
{
int i = 10;
Assert.That(hello.TestPrimitiveInOutRef(ref i), Is.True);
Assert.That(i, Is.EqualTo(20));
}
}
[Test]
public void TestEnumOut()
{
using (var hello = new Hello())
{
hello.EnumOut((int) Enum.C, out Enum e);
Assert.That(e, Is.EqualTo(Enum.C));
}
}
[Test]
public void TestEnumOutRef()
{
using (var hello = new Hello())
{
hello.EnumOutRef((int) Enum.C, out Enum e);
Assert.That(e, Is.EqualTo(Enum.C));
}
}
[Test]
public void TestEnumInOut()
{
using (var hello = new Hello())
{
var e = Enum.E;
hello.EnumInOut(ref e);
Assert.That(e, Is.EqualTo(Enum.F));
}
}
[Test]
public void TestEnumInOutRef()
{
using (var hello = new Hello())
{
var e = Enum.E;
hello.EnumInOut(ref e);
Assert.That(e, Is.EqualTo(Enum.F));
}
}
[Test]
public void TestNullRef()
{
using (var hello = new Hello())
{
Assert.That(hello.RetNull(), Is.Null);
}
}
[Test]
public void TestAmbiguous()
{
using (var def = new DefaultParameters())
{
def.Foo(1, 2);
def.Bar();
}
using (Foo foo = new Foo())
{
Common.HasPointerParam(foo, 0);
Common.HasPointerParam(foo);
}
}
[Test]
public void TestLeftShiftOperator()
{
using (var foo2 = new Foo2 { C = 2 })
{
Foo2 result = foo2 << 3;
foo2.TestKeywordParam(IntPtr.Zero, Bar.Item.Item1, 1);
Assert.That(result.C, Is.EqualTo(16));
}
}
[Test]
public void TestAbstractReturnType()
{
using (var returnsAbstractFoo = new ReturnsAbstractFoo())
{
var abstractFoo = returnsAbstractFoo.Foo;
Assert.AreEqual(abstractFoo.PureFunction(1), 5);
Assert.AreEqual(abstractFoo.PureFunction1, 10);
var ok = false;
Assert.AreEqual(abstractFoo.PureFunction2(ref ok), 15);
}
}
[Test]
public void TestANSI()
{
using (var foo = new Foo())
{
Assert.That(foo.ANSI, Is.EqualTo("ANSI"));
}
}
[Test]
public void TestMoveOperatorToClass()
{
// Unary operator
using (var unary = new TestMoveOperatorToClass() { A = 4, B = 7 })
{
var unaryMinus = -unary;
Assert.That(unaryMinus.A, Is.EqualTo(-unary.A));
Assert.That(unaryMinus.B, Is.EqualTo(-unary.B));
}
// Binary operator
using (var bin = new TestMoveOperatorToClass { A = 4, B = 7 })
{
using (var bin1 = new TestMoveOperatorToClass { A = 5, B = 10 })
{
var binSum = bin + bin1;
Assert.That(binSum.A, Is.EqualTo(bin.A + bin1.A));
Assert.That(binSum.B, Is.EqualTo(bin.B + bin1.B));
}
}
// Multiple argument operator
using (var multiArg = new TestMoveOperatorToClass { A = 4, B = 7 })
{
var multiArgStar = multiArg * 2;
Assert.That(multiArgStar, Is.EqualTo(8));
}
}
[Test]
public void TestMoveFunctionToClass()
{
using (var common = new Common())
Assert.That(Common.Test(common), Is.EqualTo(5));
}
[Test]
public void TestMethodWithFixedInstance()
{
var bar = new Bar2 { A = 1, B = 2, C = 3 };
using (Foo2 foo = bar.NeedFixedInstance)
{
Assert.AreEqual(foo.A, 1);
Assert.AreEqual(foo.B, 2);
Assert.AreEqual(foo.C, 3);
}
}
[Test]
public void TestConversionOperator()
{
var bar = new Bar2 { A = 1, B = 2, C = 3 };
using (Foo2 foo = bar)
{
Assert.That(1, Is.EqualTo(foo.A));
Assert.That(2, Is.EqualTo(foo.B));
Assert.That(3, Is.EqualTo(foo.C));
}
using (var bar2Nested = new Bar2.Nested())
{
int bar2NestedInt = bar2Nested;
Assert.That(bar2NestedInt, Is.EqualTo(300));
}
int bar2Int = new Bar2();
Assert.That(bar2Int, Is.EqualTo(500));
}
[Test]
public void TestDelegates()
{
using (var delegates = new TestDelegates())
{
var doubleSum = delegates.A(2) + delegates.B(2);
Assert.AreEqual(8, doubleSum);
var stdcall = delegates.StdCall(i => i);
Assert.AreEqual(1, stdcall);
var cdecl = delegates.CDecl(i => i);
Assert.AreEqual(1, cdecl);
var emptydelegeate = delegates.MarshalNullDelegate;
Assert.AreEqual(emptydelegeate, null);
}
}
[Test]
public void TestUnion()
{
Hello.NestedPublic nestedPublic = new Hello.NestedPublic();
nestedPublic.J = 5;
Assert.That(nestedPublic.L, Is.EqualTo(5));
Assert.That(nestedPublic.G, Is.Not.EqualTo(0));
}
[Test]
public void TestNestedAnonymousTypes()
{
using (TestNestedTypes testNestedTypes = new TestNestedTypes())
{
testNestedTypes.ToVerifyCorrectLayoutBefore = 5;
Assert.That(testNestedTypes.ToVerifyCorrectLayoutBefore, Is.EqualTo(5));
testNestedTypes.I = 10;
Assert.That(testNestedTypes.I, Is.EqualTo(10));
#if MarshalCharAsManagedSByte
testNestedTypes.C = Convert.ToSByte('D');
Assert.That(Convert.ToChar(testNestedTypes.C), Is.EqualTo('D'));
#else
testNestedTypes.C = 'D';
Assert.That(testNestedTypes.C, Is.EqualTo('D'));
#endif
testNestedTypes.ToVerifyCorrectLayoutAfter = 15;
Assert.That(testNestedTypes.ToVerifyCorrectLayoutAfter, Is.EqualTo(15));
}
}
[Test]
public void TestPropertyChains()
{
var bar2 = new Bar2();
bar2.PointerToStruct = new Bar { A = 15 };
Assert.That(bar2.PointerToStruct.A, Is.EqualTo(15));
}
[Test]
public void TestStaticClasses()
{
Type staticClassType = typeof(TestStaticClass);
// Only static class can be both abstract and sealed
Assert.IsTrue(staticClassType.IsAbstract && staticClassType.IsSealed);
Assert.That(TestStaticClass.Add(1, 2), Is.EqualTo(3));
Assert.That(TestStaticClass.OneTwoThree, Is.EqualTo(123));
Assert.That(TestStaticClassDerived.Foo, Is.EqualTo(0));
TestNotStaticClass.StaticFunction.GetHashCode();
}
[Test]
public void TestCopyConstructor()
{
using (Foo foo = new Foo { A = 5, B = 5.5f })
{
Assert.That(foo.Init, Is.EqualTo(20));
using (var copyFoo = new Foo(foo))
{
Assert.That(foo.A, Is.EqualTo(copyFoo.A));
Assert.That(foo.B, Is.EqualTo(copyFoo.B));
}
}
using (var testCopyConstructorRef = new TestCopyConstructorRef { A = 10, B = 5 })
{
using (var copyBar = new TestCopyConstructorRef(testCopyConstructorRef))
{
Assert.That(testCopyConstructorRef.A, Is.EqualTo(copyBar.A));
Assert.That(testCopyConstructorRef.B, Is.EqualTo(copyBar.B));
}
}
using (var original = new HasCopyAndMoveConstructor(5))
{
using (var copy = new HasCopyAndMoveConstructor(original))
{
Assert.That(copy.Field, Is.EqualTo(original.Field));
}
}
}
[Test]
public void TestCharMarshalling()
{
using (Foo2 foo2 = new Foo2())
{
#if MarshalCharAsManagedSByte
for (Int32 c = sbyte.MinValue; c <= sbyte.MaxValue; c++)
{
sbyte cSbyte = Convert.ToSByte(c);
sbyte charMarshalled = foo2.TestCharMarshalling(cSbyte);
Int32 charBackConverted = Convert.ToInt32(charMarshalled);
Assert.That(charBackConverted, Is.EqualTo(c));
}
#else
for (char c = char.MinValue; c <= sbyte.MaxValue; c++)
Assert.That(foo2.TestCharMarshalling(c), Is.EqualTo(c));
Assert.Catch<OverflowException>(() => foo2.TestCharMarshalling('ж'));
#endif
}
}
[Test]
public void TestIndexers()
{
using (var indexedProperties = new TestIndexedProperties())
{
Assert.AreEqual(1, indexedProperties[0]);
Assert.AreEqual(1, indexedProperties["foo"]);
indexedProperties[0] = 2;
Assert.AreEqual(2, indexedProperties[0]);
indexedProperties[0f] = 3;
Assert.AreEqual(3, indexedProperties[0f]);
var properties = indexedProperties[(byte) 0];
Assert.AreEqual(0, properties.Field);
var newProperties = new TestProperties();
newProperties.Field = 4;
indexedProperties[(byte) 0] = newProperties;
Assert.AreEqual(4, indexedProperties[(byte) 0].Field);
newProperties = indexedProperties[(short) 0];
Assert.AreEqual(4, newProperties.Field);
newProperties.Field = 5;
Assert.AreEqual(5, indexedProperties[(byte) 0].Field);
newProperties.get();
newProperties.set(0);
newProperties.Get();
newProperties.Set(0);
var bar = new Bar { A = 5 };
indexedProperties[0u] = bar;
Assert.That(bar.A, Is.EqualTo(indexedProperties[0u].A));
indexedProperties[(ushort) 0] = bar;
Assert.That(bar.A, Is.EqualTo(indexedProperties[(ushort) 0].A));
using (var foo = new Foo())
{
var bar2 = new Bar { A = 10 };
indexedProperties[foo] = bar2;
Assert.That(bar2.A, Is.EqualTo(indexedProperties[foo].A));
}
}
}
[Test]
public void TestOperators()
{
using (var @class = new ClassWithOverloadedOperators())
{
#if MarshalCharAsManagedSByte
sbyte @char = @class;
#else
char @char = @class;
#endif
Assert.That(@char, Is.EqualTo(1));
short @short = @class;
Assert.That(@short, Is.EqualTo(3));
}
using (var @class = new ClassWithOverloadedOperators())
{
int classInt = @class;
Assert.That(classInt, Is.EqualTo(2));
}
}
[Test]
public void TestFunctions()
{
var ret = Common.Function;
Assert.That(ret, Is.EqualTo(5));
Common.FuncWithTypeAlias(0);
}
[Test]
public void TestProperties()
{
// Test field property
using (var prop = new TestProperties())
{
Assert.That(prop.Field, Is.EqualTo(0));
prop.Field = 10;
Assert.That(prop.Field, Is.EqualTo(10));
// Test getter/setter property
prop.Field = 20;
Assert.That(prop.FieldValue, Is.EqualTo(20));
prop.FieldValue = 10;
Assert.That(prop.FieldValue, Is.EqualTo(10));
prop.GetterAndSetterWithTheSameName = 25;
Assert.That(prop.GetterAndSetterWithTheSameName, Is.EqualTo(25));
prop.SetterReturnsBoolean = 35;
Assert.That(prop.SetterReturnsBoolean, Is.EqualTo(35));
Assert.That(prop.SetSetterReturnsBoolean(35), Is.False);
Assert.That(prop.SetSetterReturnsBoolean(40), Is.True);
prop.VirtualSetterReturnsBoolean = 45;
Assert.That(prop.VirtualSetterReturnsBoolean, Is.EqualTo(45));
Assert.That(prop.SetVirtualSetterReturnsBoolean(45), Is.False);
Assert.That(prop.SetVirtualSetterReturnsBoolean(50), Is.True);
Assert.That(prop.nestedEnum(), Is.EqualTo(5));
Assert.That(prop.nestedEnum(55), Is.EqualTo(55));
Assert.That(prop.Get32Bit, Is.EqualTo(10));
Assert.That(prop.ConstRefField, Is.EqualTo(prop.Field));
Assert.That(prop.IsEmpty, Is.EqualTo(prop.Empty));
Assert.That(prop.VirtualGetter, Is.EqualTo(15));
Assert.That(prop.StartWithVerb, Is.EqualTo(25));
prop.StartWithVerb = 5;
#if MarshalCharAsManagedSByte
sbyte cSByte = Convert.ToSByte('a');
Assert.That(prop.Contains(cSByte), Is.EqualTo(prop.Contains("a")));
#else
Assert.That(prop.Contains('a'), Is.EqualTo(prop.Contains("a")));
#endif
Assert.That(prop.conflict, Is.EqualTo(CommonTest.TestProperties.Conflict.Value1));
prop.conflict = CommonTest.TestProperties.Conflict.Value2;
Assert.That(prop.conflict, Is.EqualTo(CommonTest.TestProperties.Conflict.Value2));
prop.Callback = x => 4 * x;
Assert.That(prop.Callback(5), Is.EqualTo(20));
Assert.That(prop.ArchiveName, Is.EqualTo(20));
}
using (var prop = new HasOverridenSetter())
{
Assert.That(prop.VirtualGetter, Is.EqualTo(20));
prop.SetVirtualGetter(5);
}
}
[Test]
public void TestVariable()
{
// Test field property
using (var @var = new TestVariables())
{
@var.SetValue(10);
}
Assert.That(TestVariables.VALUE, Is.EqualTo(10));
}
[Test]
public void TestWideStrings()
{
using (var ws = new TestWideStrings())
{
var s = ws.WidePointer;
Assert.That(ws.WidePointer, Is.EqualTo("Hello"));
Assert.That(ws.WideNullPointer, Is.EqualTo(null));
}
}
[Test]
public unsafe void TestArraysPointers()
{
var values = MyEnum.A;
using (var arrays = new TestArraysPointers(ref values, 1))
{
Assert.That(arrays.Value, Is.EqualTo(MyEnum.A));
}
}
[Test]
public unsafe void TestGetterSetterToProperties()
{
using (var @class = new TestGetterSetterToProperties())
{
Assert.That(@class.Width, Is.EqualTo(640));
Assert.That(@class.Height, Is.EqualTo(480));
}
}
[Test]
public unsafe void TestSingleArgumentCtorToCastOperator()
{
using (var classA = new ClassA(10))
{
ClassB classB = classA;
Assert.AreEqual(classA.Value, classB.Value);
ClassC classC = (ClassC) classB;
Assert.AreEqual(classB.Value, classC.Value);
}
}
[Test]
public unsafe void TestFieldRef()
{
using (var classD = new ClassD(10))
{
var fieldRef = classD.Field;
fieldRef.Value = 20;
Assert.AreEqual(20, classD.Field.Value);
}
}
[Test]
public unsafe void TestDecltype()
{
int ret = Common.TestDecltype;
Assert.AreEqual(0, ret);
}
[Test]
public unsafe void TestNullPtrType()
{
void* ret = Common.TestNullPtrTypeRet;
Assert.AreEqual(IntPtr.Zero, new IntPtr(ret));
}
[Test]
public void TestCtorByValue()
{
var bar = new Bar { A = 4, B = 5.5f };
using (var foo2 = new Foo2 { C = 4, ValueTypeField = bar })
{
var result = foo2 << 2;
Assert.AreEqual(foo2.C << 2, result.C);
Assert.AreEqual(bar.A << 2, result.ValueTypeField.A);
Assert.AreEqual(bar.B, result.ValueTypeField.B);
}
}
[Test]
public void TestMarshalUnattributedDelegate()
{
using (TestDelegates testDelegates = new TestDelegates())
{
testDelegates.MarshalUnattributedDelegate(i => i);
}
}
[Test]
public void TestPassAnonymousDelegate()
{
using (var testDelegates = new TestDelegates())
{
int value = testDelegates.MarshalAnonymousDelegate(i => i * 2);
Assert.AreEqual(2, value);
int value5 = testDelegates.MarshalAnonymousDelegate5(i => i * 2);
Assert.AreEqual(4, value5);
int value6 = testDelegates.MarshalAnonymousDelegate6(i => i * 2);
Assert.AreEqual(6, value6);
}
}
[Test]
public void TestGetAnonymousDelegate()
{
using (var testDelegates = new TestDelegates())
{
var @delegate = testDelegates.MarshalAnonymousDelegate4;
int value = @delegate.Invoke(1);
Assert.AreEqual(2, value);
}
}
[Test]
public void TestFixedArrays()
{
using (var foo = new Foo())
{
var array = new[] { 1, 2, 3 };
foo.FixedArray = array;
for (int i = 0; i < foo.FixedArray.Length; i++)
Assert.That(array[i], Is.EqualTo(foo.FixedArray[i]));
}
}
[Test]
public void TestInternalCtorAmbiguity()
{
using (var invokesInternalCtorAmbiguity = new InvokesInternalCtorAmbiguity())
{
invokesInternalCtorAmbiguity.InvokeInternalCtor();
}
}
[Test]
public void TestEqualityOperator()
{
#pragma warning disable 1718 // Comparison made to same variable; did you mean to compare something else?
using (var foo = new Foo { A = 5, B = 5.5f })
{
Assert.IsTrue(foo == foo);
using (var notEqual = new Foo { A = 5, B = 5.6f })
{
Assert.IsTrue(notEqual != foo);
}
Assert.IsTrue(foo != null);
}
var bar = new Bar { A = 5, B = 5.5f };
Assert.IsTrue(bar == bar);
Assert.IsFalse(new Bar { A = 5, B = 5.6f } == bar);
#if !__MonoCS__
using (var differentConstOverloads = new DifferentConstOverloads())
{
DifferentConstOverloads other = null;
Assert.IsTrue(differentConstOverloads != other);
}
#endif
#pragma warning restore 1718
}
[Test]
public void TestFriendOperator()
{
using (HasFriend h1 = 5)
{
using (HasFriend h2 = 10)
{
Assert.AreEqual(15, (h1 + h2).M);
Assert.AreEqual(-5, (h1 - h2).M);
}
}
}
[Test]
public void TestOperatorOverloads()
{
var differentConstOverloads = new DifferentConstOverloads();
var differentConstOverloads1 = new DifferentConstOverloads();
Assert.IsTrue(differentConstOverloads == differentConstOverloads1);
// HACK: don't replace with a using because it triggers a bug in the Mono compiler
// https://travis-ci.org/github/mono/CppSharp/jobs/674224017#L997
differentConstOverloads1.Dispose();
Assert.IsTrue(differentConstOverloads == 5);
Assert.IsFalse(differentConstOverloads == 4);
Assert.IsTrue(differentConstOverloads == "abcde");
Assert.IsFalse(differentConstOverloads == "abcd");
differentConstOverloads.Dispose();
}
[Test]
public void TestPacking()
{
foreach (var testPacking in new TestPacking[] { new TestPacking1(), new TestPacking2(),
new TestPacking4(), new TestPacking8() })
{
testPacking.I1 = 2;
testPacking.I2 = 4;
testPacking.B = true;
Assert.That(testPacking.I1, Is.EqualTo(2));
Assert.That(testPacking.I2, Is.EqualTo(4));
Assert.That(testPacking.B, Is.EqualTo(true));
testPacking.Dispose();
}
}
[Test]
public void TestRenamingVariableNamedAfterKeyword()
{
Assert.AreEqual(10, Foo.Unsafe);
}
[Test]
public void TestMarshallingEmptyType()
{
using (ReturnsEmpty returnsEmpty = new ReturnsEmpty())
{
returnsEmpty.Empty.Dispose();
}
}
[Test]
public void TestOutTypeClassesPassTry()
{
RefTypeClassPassTry refTypeClassPassTry;
Common.FuncTryRefTypeOut(out refTypeClassPassTry);
Common.FuncTryRefTypePtrOut(out refTypeClassPassTry);
ValueTypeClassPassTry valueTypeClassPassTry;
Common.FuncTryValTypeOut(out valueTypeClassPassTry);
Common.FuncTryValTypePtrOut(out valueTypeClassPassTry);
}
[Test]
public void TestVirtualReturningClassWithCharField()
{
using (var hasVirtualReturningHasProblematicFields = new HasVirtualReturningHasProblematicFields())
{
var hasProblematicFields = hasVirtualReturningHasProblematicFields.ReturnsProblematicFields;
Assert.That(hasProblematicFields.B, Is.EqualTo(false));
hasProblematicFields.B = true;
Assert.That(hasProblematicFields.B, Is.EqualTo(true));
Assert.That(hasProblematicFields.C, Is.EqualTo(char.MinValue));
#if MarshalCharAsManagedSByte
hasProblematicFields.C = Convert.ToSByte('a');
Assert.That(Convert.ToChar(hasProblematicFields.C), Is.EqualTo('a'));
#else
hasProblematicFields.C = 'a';
Assert.That(hasProblematicFields.C, Is.EqualTo('a'));
#endif
}
}
[Test]
public void TestDisposingCustomDerivedFromVirtual()
{
using (new CustomDerivedFromVirtual())
{
}
}
[Test]
public void TestIncompleteCharArray()
{
Assert.That(Foo.CharArray, Is.EqualTo("abc"));
}
[Test]
public void TestPassingNullToRef()
{
using (var foo = new Foo())
{
Assert.Catch<ArgumentNullException>(() => foo.TakesRef(null));
}
}
[Test]
public void TestPassingNullToValue()
{
Assert.Catch<ArgumentNullException>(() => new Bar((Foo) null));
}
[Test]
public void TestNonTrivialDtorInvocation()
{
using (var nonTrivialDtor = new NonTrivialDtor())
{
}
Assert.IsTrue(NonTrivialDtor.dtorCalled);
}
[Test]
public void TestFixedCharArray()
{
using (var foo = new Foo())
{
#if MarshalCharAsManagedSByte
foo.FixedCharArray = new sbyte[] { Convert.ToSByte('a'), Convert.ToSByte('b'), Convert.ToSByte('c') };
Assert.That(foo.FixedCharArray[0], Is.EqualTo('a'));
Assert.That(foo.FixedCharArray[1], Is.EqualTo('b'));
Assert.That(foo.FixedCharArray[2], Is.EqualTo('c'));
#else
foo.FixedCharArray = new char[] { 'a', 'b', 'c' };
Assert.That(foo.FixedCharArray[0], Is.EqualTo('a'));
Assert.That(foo.FixedCharArray[1], Is.EqualTo('b'));
Assert.That(foo.FixedCharArray[2], Is.EqualTo('c'));
#endif
}
}
[Test]
public void TestStaticFields()
{
Assert.That(Foo.ReadWrite, Is.EqualTo(15));
Foo.ReadWrite = 25;
Assert.That(Foo.ReadWrite, Is.EqualTo(25));
}
[Test]
public void TestStdString()
{
// when C++ memory is deleted, it's only marked as free but not immediadely freed
// this can hide memory bugs while marshalling
// so let's use a long string to increase the chance of a crash right away
const string t = @"This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string.
This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string.
This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string.
This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string.
This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string.
This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string.";
using (var hasStdString = new HasStdString())
{
Assert.That(hasStdString.TestStdString(t), Is.EqualTo(t + "_test"));
hasStdString.S = t;
Assert.That(hasStdString.S, Is.EqualTo(t));
Assert.That(hasStdString.StdString, Is.EqualTo(t));
Assert.That(hasStdString.StdString, Is.EqualTo(t));
}
}
[Test]
public void TestStdStringPassedByValue()
{
// when C++ memory is deleted, it's only marked as free but not immediadely freed
// this can hide memory bugs while marshalling
// so let's use a long string to increase the chance of a crash right away
const string t = @"This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string.
This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string.
This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string.
This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string.
This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string.
This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string.";
using (var hasStdString = new HasStdString())
{
Assert.That(hasStdString.TestStdStringPassedByValue(t), Is.EqualTo(t + "_test"));
hasStdString.S = t;
Assert.That(hasStdString.S, Is.EqualTo(t));
Assert.That(hasStdString.StdString, Is.EqualTo(t));
Assert.That(hasStdString.StdString, Is.EqualTo(t));
}
}
public void TestNullStdString()
{
using (var hasStdString = new HasStdString())