|
| 1 | +using System; |
| 2 | +using System.Linq; |
| 3 | +using Xunit; |
| 4 | + |
| 5 | +namespace MPack.Tests |
| 6 | +{ |
| 7 | + public class EqualityTests |
| 8 | + { |
| 9 | + [Fact] |
| 10 | + public void TestDictionaryEquality() |
| 11 | + { |
| 12 | + var dictionary = new MDict |
| 13 | + { |
| 14 | + { |
| 15 | + "array1", MToken.From(new[] |
| 16 | + { |
| 17 | + "array1_value1", // implicitly converted string |
| 18 | + MToken.From("array1_value2"), |
| 19 | + }) |
| 20 | + }, |
| 21 | + {"bool1", MToken.From(true)}, //boolean |
| 22 | + {"double1", MToken.From(50.5)}, //single-precision float |
| 23 | + {"double2", MToken.From(15.2)}, |
| 24 | + {"int1", 50505}, // implicitly converted integer |
| 25 | + {"int2", MToken.From(50)} // integer |
| 26 | + }; |
| 27 | + |
| 28 | + var dictionary2 = new MDict |
| 29 | + { |
| 30 | + { |
| 31 | + "array1", MToken.From(new[] |
| 32 | + { |
| 33 | + "array1_value1", // implicitly converted string |
| 34 | + MToken.From("array1_value2"), |
| 35 | + }) |
| 36 | + }, |
| 37 | + {"bool1", MToken.From(true)}, //boolean |
| 38 | + {"double1", MToken.From(50.5)}, //single-precision float |
| 39 | + {"double2", MToken.From(15.2)}, |
| 40 | + {"int1", 50505}, // implicitly converted integer |
| 41 | + {"int2", MToken.From(50)} // integer |
| 42 | + }; |
| 43 | + |
| 44 | + var dictionary3 = new MDict |
| 45 | + { |
| 46 | + { |
| 47 | + "array1", MToken.From(new[] |
| 48 | + { |
| 49 | + "array1_ value1", // implicitly converted string |
| 50 | + MToken.From("array1_value2"), |
| 51 | + }) |
| 52 | + }, |
| 53 | + {"bool1", MToken.From(true)}, //boolean |
| 54 | + {"double1", MToken.From(50.5)}, //single-precision float |
| 55 | + {"double2", MToken.From(15.2)}, |
| 56 | + {"int1", 50505}, // implicitly converted integer |
| 57 | + {"int2", MToken.From(50)} // integer |
| 58 | + }; |
| 59 | + |
| 60 | + Assert.Equal(dictionary, dictionary2); |
| 61 | + Assert.NotEqual(dictionary, dictionary3); |
| 62 | + Assert.NotEqual(dictionary, MToken.From(50.5)); |
| 63 | + } |
| 64 | + |
| 65 | + [Fact] |
| 66 | + public void TestNumberEquality() |
| 67 | + { |
| 68 | + Assert.Equal(MToken.From(10), MToken.From(10)); |
| 69 | + Assert.NotEqual(MToken.From(10), MToken.From(-10)); |
| 70 | + Assert.NotEqual(MToken.From(10), -10); |
| 71 | + Assert.Equal(MToken.From(10), 10); |
| 72 | + Assert.Equal(10, MToken.From(10)); |
| 73 | + Assert.Equal((long)10, MToken.From(10)); |
| 74 | + Assert.Equal(10, MToken.From((long)10)); |
| 75 | + Assert.Equal(MToken.From((int)10), MToken.From((double)10)); |
| 76 | + Assert.Equal(MToken.From((decimal)10.12345), MToken.From((double)10.12345)); |
| 77 | + Assert.NotEqual(MToken.From((decimal)10.12345), MToken.From((double)10.1245)); |
| 78 | + Assert.Equal(MToken.From((object)10.12345d), MToken.From((object)10.12345m)); |
| 79 | + } |
| 80 | + |
| 81 | + [Fact] |
| 82 | + public void TestStringEquality() |
| 83 | + { |
| 84 | + Assert.Equal(MToken.From("hello!"), MToken.From("hello!")); |
| 85 | + Assert.NotEqual(MToken.From(10), MToken.From("hello!")); |
| 86 | + } |
| 87 | + |
| 88 | + [Fact] |
| 89 | + public void TestStringCompare() |
| 90 | + { |
| 91 | + var arr = new MToken[] |
| 92 | + { |
| 93 | + "a", |
| 94 | + 9, |
| 95 | + "z", |
| 96 | + "!", |
| 97 | + 1, |
| 98 | + "b", |
| 99 | + }; |
| 100 | + |
| 101 | + Array.Sort(arr); |
| 102 | + Assert.Equal(1, arr[0]); |
| 103 | + Assert.Equal(9, arr[1]); |
| 104 | + Assert.Equal("!", arr[2]); |
| 105 | + Assert.Equal("a", arr[3]); |
| 106 | + Assert.Equal("b", arr[4]); |
| 107 | + Assert.Equal("z", arr[5]); |
| 108 | + } |
| 109 | + |
| 110 | + [Fact] |
| 111 | + public void TestBoolNullEquality() |
| 112 | + { |
| 113 | + Assert.Equal(MToken.From(true), MToken.From(true)); |
| 114 | + Assert.NotEqual(MToken.From(true), MToken.From(false)); |
| 115 | + Assert.NotEqual(MToken.From(true), MToken.From(null)); |
| 116 | + Assert.Equal(MToken.From(false), MToken.From(false)); |
| 117 | + Assert.NotEqual(MToken.From(false), MToken.From(null)); |
| 118 | + Assert.Equal(MToken.From(null), MToken.From(null)); |
| 119 | + Assert.NotEqual(MToken.From(null), MToken.From(10)); |
| 120 | + Assert.NotEqual(MToken.From(null), MToken.From("asd")); |
| 121 | + } |
| 122 | + |
| 123 | + [Fact] |
| 124 | + public void TestArrayEquality() |
| 125 | + { |
| 126 | + var tests = new object[] |
| 127 | + { |
| 128 | + (float) 0, |
| 129 | + (float) 50505, |
| 130 | + 1234.92d, |
| 131 | + 800, |
| 132 | + (object)99656, |
| 133 | + "hello!", |
| 134 | + new[] {1, 2, 3, 4, 5}, |
| 135 | + }; |
| 136 | + |
| 137 | + var m1 = tests.Select(f => MToken.From(f)).ToArray(); |
| 138 | + var m2 = tests.Select(f => MToken.From(f)).ToArray(); |
| 139 | + tests[1] = 0; |
| 140 | + var m3 = tests.Select(f => MToken.From(f)).ToArray(); |
| 141 | + |
| 142 | + Assert.Equal(m1, m2); |
| 143 | + Assert.NotEqual(m1, m3); |
| 144 | + } |
| 145 | + } |
| 146 | +} |
0 commit comments