bin/projects/dbatools/dbatools.Tests/Utility/SizeTest.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
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Sqlcollaborative.Dbatools.Utility
{
    [TestClass]
    public class SizeTest
    {
        [TestMethod]
        public void TestDefaultContrustor()
        {
            var size = new Size();
            Assert.AreEqual(0, size.Byte);
            Assert.AreEqual("0 B", size.ToString());
            // Make sure the defaults come from the utility host.
            Assert.AreEqual(UtilityHost.SizeStyle, size.Style);
            Assert.AreEqual(UtilityHost.SizeDigits, size.Digits);
            // Make sure the default are what we explicitly what we expect them to be.
            Assert.AreEqual(SizeStyle.Dynamic, size.Style);
            Assert.AreEqual(2, size.Digits);
        }

        [TestMethod]
        public void TestGetHash()
        {
            var size = 10234453626262624;
            var byteSize = size ;
            Assert.AreEqual(size.GetHashCode(), byteSize.GetHashCode());
        }

        [TestMethod]
        public void TestCompareToObjOfSize()
        {
            var sizes = new Object[] { new Size(42), new Size(56), new Size(42) };
            Assert.AreEqual(-1, ((Size)sizes[0]).CompareTo(sizes[1]));
            Assert.AreEqual(0, ((Size)sizes[0]).CompareTo(sizes[2]));
            Assert.AreEqual(1, ((Size)sizes[1]).CompareTo(sizes[2]));
        }

        [TestMethod]
        public void TestCompareToObjOfInt()
        {
            var sizes = new[] { new Size(42), new Size(56), new Size(42) };
            Assert.AreEqual(-1, sizes[0].CompareTo(sizes[1].Byte));
            Assert.AreEqual(0, sizes[0].CompareTo(sizes[2].Byte));
            Assert.AreEqual(1, sizes[1].CompareTo(sizes[2].Byte));
        }

        [DataRow(42u, 56u)]
        [DataRow(10000u, 2500000u)]
        [DataRow(2u, 7u)]
        [TestMethod]
        public void TestCompareToObjOfUInt(uint a, uint b)
        {
            Assert.IsTrue(a < b, string.Format("Invalid test data, A ({0}) should be less than B ({1})", a, b));
            var sizes = new[] { (Size)a, (Size)b, (Size)a };
            Assert.AreEqual(-1, sizes[0].CompareTo(b));
            Assert.AreEqual(0, sizes[0].CompareTo(a));
            Assert.AreEqual(1, sizes[1].CompareTo(a));
        }

        [DataRow(42, 56)]
        [DataRow(10000, 2500000)]
        [DataRow(2, 7)]
        [TestMethod]
        public void TestCompareToObjOfDecimal(int a, int b)
        {
            Assert.IsTrue(a < b, string.Format("Invalid test data, A ({0}) should be less than B ({1})", a, b));
            var sizes = new[] { (Size)(decimal)a, (Size)(decimal)b, (Size)(decimal)a };
            Assert.AreEqual(-1, sizes[0].CompareTo((decimal)b));
            Assert.AreEqual(0, sizes[0].CompareTo((decimal)a));
            Assert.AreEqual(1, sizes[1].CompareTo((decimal)a));
        }

        [DataRow(42d, 56d)]
        [DataRow(10000d, 2500000d)]
        [DataRow(2d, 7d)]
        [TestMethod]
        public void TestCompareToObjOfDouble(double a, double b)
        {
            Assert.IsTrue(a < b, string.Format("Invalid test data, A ({0}) should be less than B ({1})", a, b));
            var sizes = new [] { (Size)a, (Size)b, (Size)a };
            Assert.AreEqual(-1, sizes[0].CompareTo(b));
            Assert.AreEqual(0, sizes[0].CompareTo(a));
            Assert.AreEqual(1, sizes[1].CompareTo(a));
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentException))]
        public void TestCompareToObjOfInvalid()
        {
            var size = new Size();
            try
            {
                size.CompareTo(Guid.Empty);
            }
            catch (ArgumentException ex)
            {
                Assert.AreEqual("Cannot compare a Sqlcollaborative.Dbatools.Utility.Size to a System.Guid", ex.Message);
                throw;
            }
        }


        [DataRow(42, 56)]
        [DataRow(10000, 2500000)]
        [DataRow(2, 7)]
        [TestMethod]
        public void TestCompareToSize(int a, int b)
        {
            Assert.IsTrue(a < b, string.Format("Invalid test data, A ({0}) should be less than B ({1})", a, b));
            var sizes = new[] { new Size(a), new Size(b), new Size(a)};
            Assert.AreEqual(-1, sizes[0].CompareTo(sizes[1]));
            Assert.AreEqual(0, sizes[0].CompareTo(sizes[2]));
            Assert.AreEqual(1, sizes[1].CompareTo(sizes[2]));
        }

        [TestMethod]
        public void TestEqualsTo()
        {
            var sizes = new[] { new Size(42), new Size(56), new Size(42)};
            Assert.IsFalse(sizes[0].Equals(sizes[1]));
            Assert.IsTrue(sizes[0].Equals(sizes[2]));
        }

        [TestMethod]
        public void TestDigitsToString()
        {
            var size = new Size(5607509301657);
            Assert.AreEqual("5.10 TB", size.ToString());
            size.Digits = 1;
            Assert.AreEqual("5.1 TB", size.ToString());
            size.Digits = 0;
            Assert.AreEqual("5 TB", size.ToString());
            size.Digits = -42;
            Assert.AreEqual("5 TB", size.ToString());
        }

        [TestMethod]
        public void TestStyleToString()
        {
            var size = new Size(5607509301657);
            Assert.AreEqual("5.10 TB", size.ToString());
            size.Style = SizeStyle.Gigabyte;
            Assert.AreEqual("5,222.40 GB", size.ToString());
            size.Digits = 1;
            Assert.AreEqual("5,222.4 GB", size.ToString());
            size.Digits = 0;
            Assert.AreEqual("5,222 GB", size.ToString());
            size.Style = SizeStyle.Megabyte;
            Assert.AreEqual("5,347,738 MB", size.ToString());
            size.Style = SizeStyle.Kilobyte;
            Assert.AreEqual("5,476,083,302 KB", size.ToString());
            size.Style = SizeStyle.Byte;
            Assert.AreEqual("5607509301657 B", size.ToString());
            size.Style = SizeStyle.Plain;
            Assert.AreEqual("5607509301657", size.ToString());
            // Because the first time we were using dynamic styling.
            size.Style = SizeStyle.Terabyte;
            Assert.AreEqual("5 TB", size.ToString());
        }

        [TestMethod]
        public void TestTerabytes()
        {
            var size = new Size(5607509301657);
            Assert.AreEqual("5.10 TB", size.ToString());
        }

        [TestMethod]
        public void TestGigabytes()
        {
            var size = new Size(72 * (long)Math.Pow(1024, 3));
            Assert.AreEqual("72.00 GB", size.ToString());
        }

        [TestMethod]
        public void TestMegabytes()
        {
            var size = new Size(49 * (long)Math.Pow(1024, 2));
            Assert.AreEqual("49.00 MB", size.ToString());
        }

        [TestMethod]
        public void TestKilobytes()
        {
            var size = new Size(52 * (long)Math.Pow(1024, 1));
            Assert.AreEqual("52.00 KB", size.ToString());
        }

        [TestMethod]
        public void TestBytes()
        {
            var size = new Size(526);
            Assert.AreEqual("526 B", size.ToString());
            size.Digits = 34;
            Assert.AreEqual("526 B", size.ToString());
        }

        [TestMethod]
        public void TestUnlimited()
        {
            var size = new Size(-1);
            Assert.AreEqual("Unlimited", size.ToString());
        }

        [TestMethod]
        public void TestZeroBytes()
        {
            var size = new Size(0);
            Assert.AreEqual("0 B", size.ToString());
        }

        [TestMethod]
        public void TestNegativeBytes()
        {
            var size = new Size(-2);
            Assert.AreEqual("", size.ToString());
        }

        [TestMethod]
        public void TestLargeDecimal()
        {
            Size size = 1000000000000000m;
            Assert.AreEqual(909, size.Terabyte, 0.9);
            Assert.AreEqual(953674316, size.Megabyte, 0.9);
            Assert.AreEqual("909.49 TB", size.ToString());
            decimal reverse = size;
            Assert.AreEqual(1000000000000000m, reverse);
        }

        [TestMethod]
        public void TestLargeDouble()
        {
            Size size = 1000000000000000d;
            Assert.AreEqual(909, size.Terabyte, 0.9);
            Assert.AreEqual("909.49 TB", size.ToString());
            double reverse = size;
            Assert.AreEqual(1000000000000000d, reverse);
        }

        [TestMethod]
        public void TestInt32Cast()
        {
            int iSize = 1000000000;
            Size size = iSize;
            Assert.AreEqual(iSize, size.Byte);
            Assert.AreEqual(953, size.Megabyte, 0.9);
            Assert.AreEqual("953.67 MB", size.ToString());
            int reverse = size;
            Assert.AreEqual(1000000000, reverse);
        }

        [DataRow(1)]
        [DataRow(50)]
        [DataRow(230)]
        [DataRow(53687091200)] // 50 GB
        [DataRow(1000000000000000)]
        [TestMethod]
        public void TestGetHashCode(long iSize)
        {
            var size = new Size(iSize);
            Assert.AreEqual(iSize.GetHashCode(), size.GetHashCode());
        }

        [DataRow(2, 2, 4)]
        [DataRow(1024, 1024, 2048)]
        [DataRow(1024, -1024, 0)]
        [TestMethod]
        public void TestAddition(long a, long b, long result)
        {
            Assert.AreEqual(new Size(result), new Size(a) + new Size(b));
        }

        [DataRow(2, 2, 0)]
        [DataRow(1024, 1024, 0)]
        [DataRow(1024, -1024, 2048)]
        [TestMethod]
        public void TestSubtraction(long a, long b, long result)
        {
            Assert.AreEqual(new Size(result), new Size(a) - new Size(b));
        }

        [DataRow(2, 2, 4)]
        [DataRow(2, 3, 6)]
        [DataRow(1024, 1024, 1048576)]
        [DataRow(1024, -1024, -1048576)]
        [TestMethod]
        public void TestMultiplication(long a, long b, long result)
        {
            Assert.AreEqual(new Size(result), new Size(a) * new Size(b));
        }

        [DataRow(2, 2, 1)]
        [DataRow(2, 3, 0)]
        [DataRow(1024, 1024, 1)]
        [DataRow(1024, -1024, -1)]
        [TestMethod]
        public void TestDivision(long a, long b, long result)
        {
            Assert.AreEqual(new Size(result), new Size(a) / new Size(b));
        }
    }
}