-
Notifications
You must be signed in to change notification settings - Fork 1
/
BigIntUidTests.cs
170 lines (119 loc) · 4.51 KB
/
BigIntUidTests.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
namespace BlueMine.Data.Tests
{
[System.Diagnostics.DebuggerDisplay("Rand: {nameof(m_rand)}")]
internal class BigIntUidTests
{
private class UidContainer
{
public string Number;
public UInt128 UI;
public System.Guid UID;
public string NumberFromUid;
public UidContainer(UInt128 ui)
{
this.UI = ui;
this.Number = ui.ToString();
this.UID = ui.ToGuid();
this.NumberFromUid = UInt128.FromGuid(this.UID).ToString();
} // End Constructor
} // End Class UidContainer
public static void Test2()
{
// UInt128.DivMod(5, 3);
UInt128 five = new UInt128(5);
UInt128 three = new UInt128(3);
UInt128 product = five % three;
UInt128 big = new UInt128("001125967647482471318463346559328037643");
System.Guid bigUID = big.ToGuid();
System.Console.WriteLine("", five, three, product, bigUID);
UInt128 a = new UInt128(256) + 3;
// a = UInt128.Square(a);
System.Console.WriteLine(a);
string g = new UInt128(1).ToGuid().ToString();
//g = UInt128.MinValue.ToGuid().ToString();
g = new UInt128(16777216 - 1).ToGuid().ToString();
System.Console.WriteLine(g);
} // End Sub Test
public static void Test()
{
BigRandom rand = new BigRandom();
System.Collections.Generic.List<UidContainer> ls =
new System.Collections.Generic.List<UidContainer>();
for (int i = 0; i < 100; ++i)
{
UInt128 ui = rand.NextUInt128();
ls.Add(new UidContainer(ui));
} // Next i
ls.Sort(Compare);
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.AppendLine(";WITH CTE AS ( ");
int padLen = UInt128.MaxValue.ToString().Length;
for (int i = 0; i < ls.Count; ++i)
{
if (i == 0)
sb.Append(" ");
else
sb.Append(" UNION ALL ");
sb.Append("SELECT CAST('");
sb.Append(ls[i].UID.ToString());
sb.Append("' AS uniqueidentifier) AS uid, '");
sb.Append(ls[i].UI.ToString().PadLeft(padLen, '0'));
sb.Append("' AS num, '");
sb.Append(ls[i].NumberFromUid.PadLeft(padLen, '0'));
sb.Append("' AS num2 ");
sb.AppendLine(System.Environment.NewLine);
} // Next i
sb.AppendLine(@"
)
SELECT
*
,ROW_NUMBER() OVER(ORDER BY uid) AS rnUid
,ROW_NUMBER() OVER(ORDER BY num) AS rnNum
FROM CTE
ORDER BY uid
-- ORDER BY num
");
string s = sb.ToString();
sb.Clear();
sb = null;
System.Console.WriteLine(ls);
System.Console.WriteLine(s);
} // End Sub Test
private static int Compare(UidContainer a, UidContainer b)
{
System.Guid x = a.UID;
System.Guid y = b.UID;
return Compare(x, y);
} // End Function Compare
private static int Compare(System.Guid x, System.Guid y)
{
const int NUM_BYTES_IN_GUID = 16;
byte byte1, byte2;
byte[] xBytes = new byte[NUM_BYTES_IN_GUID];
byte[] yBytes = new byte[NUM_BYTES_IN_GUID];
x.ToByteArray().CopyTo(xBytes, 0);
y.ToByteArray().CopyTo(yBytes, 0);
int[] byteOrder = new int[16] // 16 Bytes = 128 Bit
{10, 11, 12, 13, 14, 15, 8, 9, 6, 7, 4, 5, 0, 1, 2, 3};
// Swap to the correct order to be compared
for (int i = 0; i < NUM_BYTES_IN_GUID; i++)
{
byte1 = xBytes[byteOrder[i]];
byte2 = yBytes[byteOrder[i]];
if (byte1 != byte2)
{
int num = (byte1 < byte2) ? -1 : 1;
xBytes = null;
yBytes = null;
byteOrder = null;
return num;
} // End if (byte1 != byte2)
} // Next i
xBytes = null;
yBytes = null;
byteOrder = null;
return 0;
} // End Function Compare
} // End Class BigIntUidTests
} // End Namespace BlueMine.Data.Tests