File: libbytesize_unittest.py

package info (click to toggle)
libbytesize 2.11-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 784 kB
  • sloc: python: 1,850; ansic: 994; sh: 666; makefile: 258
file content (770 lines) | stat: -rwxr-xr-x 25,850 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/python3
# -*- coding: utf-8 -*-

import locale
import unittest
import sys
import ctypes

from locale_utils import get_avail_locales, missing_locales, requires_locales

from bytesize import KiB, GiB, ROUND_UP, ROUND_DOWN, ROUND_HALF_UP, OverflowError

# SizeStruct is part of the 'private' API and needs to be imported differently
# when running from locally build tree and when using installed library
try:
    from bytesize import SizeStruct
except ImportError:
    from bytesize.bytesize import SizeStruct

DEFAULT_LOCALE = "en_US.utf8"

class SizeTestCase(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        unittest.TestCase.setUpClass()
        cls.avail_locales = get_avail_locales()

    def setUp(self):
        missing = missing_locales({DEFAULT_LOCALE}, self.avail_locales)
        if missing:
            self.skipTest("requires missing locales: %s" % missing)
        locale.setlocale(locale.LC_ALL, DEFAULT_LOCALE)
        self.addCleanup(self._clean_up)

    def _clean_up(self):
        locale.setlocale(locale.LC_ALL, DEFAULT_LOCALE)

    def testNew(self):
        actual = SizeStruct.new().get_bytes()
        expected = (0, 0)
        self.assertEqual(actual, expected)

    @requires_locales({'cs_CZ.UTF-8', 'ps_AF.UTF-8', 'en_US.UTF-8'})
    def testNewFromStr(self):
        actual = SizeStruct.new_from_str('0 B').get_bytes()
        expected = (0, 0)
        self.assertEqual(actual, expected)

        actual = SizeStruct.new_from_str('1 KiB').get_bytes()
        expected = (1024, 1)
        self.assertEqual(actual, expected)

        actual = SizeStruct.new_from_str('1KB').get_bytes()
        expected = (1000, 1)
        self.assertEqual(actual, expected)

        actual = SizeStruct.new_from_str('1 MiB').get_bytes()
        expected = (1048576, 1)
        self.assertEqual(actual, expected)

        actual = SizeStruct.new_from_str('   1 MiB').get_bytes()
        expected = (1048576, 1)
        self.assertEqual(actual, expected)

        actual = SizeStruct.new_from_str('1 MiB    ').get_bytes()
        expected = (1048576, 1)
        self.assertEqual(actual, expected)

        actual = SizeStruct.new_from_str('    1 MiB   ').get_bytes()
        expected = (1048576, 1)
        self.assertEqual(actual, expected)

        actual = SizeStruct.new_from_str('-1.5 GiB').get_bytes()
        expected = (1610612736, -1)
        self.assertEqual(actual, expected)

        actual = SizeStruct.new_from_str('+1.5 GiB').get_bytes()
        expected = (1610612736, 1)
        self.assertEqual(actual, expected)

        locale.setlocale(locale.LC_ALL,'cs_CZ.UTF-8')

        actual = SizeStruct.new_from_str('1,5 KiB').get_bytes()
        expected = (1536, 1)
        self.assertEqual(actual, expected)

        actual = SizeStruct.new_from_str('-1,5 KiB').get_bytes()
        expected = (1536, -1)
        self.assertEqual(actual, expected)

        actual = SizeStruct.new_from_str('1.5 KiB').get_bytes()
        expected = (1536, 1)
        self.assertEqual(actual, expected)

        actual = SizeStruct.new_from_str('-1.5 KiB').get_bytes()
        expected = (1536, -1)
        self.assertEqual(actual, expected)

        actual = SizeStruct.new_from_str('1e-1 KB').get_bytes()
        expected = (100, 1)
        self.assertEqual(actual, expected)

        actual = SizeStruct.new_from_str('-1e-1 KB').get_bytes()
        expected = (100, -1)
        self.assertEqual(actual, expected)

        # this persian locale uses a two-byte unicode character for the radix
        locale.setlocale(locale.LC_ALL, 'ps_AF.UTF-8')

        actual = SizeStruct.new_from_str('1٫5 KiB').get_bytes()
        expected = (1536, 1)
        self.assertEqual(actual, expected)

        actual = SizeStruct.new_from_str('-1٫5 KiB').get_bytes()
        expected = (1536, -1)
        self.assertEqual(actual, expected)

        actual = SizeStruct.new_from_str('1.5 KiB').get_bytes()
        expected = (1536, 1)
        self.assertEqual(actual, expected)

        actual = SizeStruct.new_from_str('-1.5 KiB').get_bytes()
        expected = (1536, -1)
        self.assertEqual(actual, expected)

        locale.setlocale(locale.LC_ALL, DEFAULT_LOCALE)

    #enddef

    def testNewFromBytes(self):
        actual = SizeStruct.new_from_bytes(0, 0).get_bytes()
        expected = (0, 0)
        self.assertEqual(actual, expected)

        actual = SizeStruct.new_from_bytes(10, 1).get_bytes()
        expected = (10, 1)
        self.assertEqual(actual, expected)

        actual = SizeStruct.new_from_bytes(1024, -1).get_bytes()
        expected = (1024, -1)
        self.assertEqual(actual, expected)

        # now let's try something bigger than MAXUINT32
        actual = SizeStruct.new_from_bytes(5718360*1024, 1).get_bytes()
        expected = (5718360*1024, 1)
        self.assertEqual(actual, expected)

    #enddef

    def testNewFromSizeStruct(self):
        tempSizeStruct = SizeStruct.new_from_bytes(17, 1)
        actual = SizeStruct.new_from_size(tempSizeStruct).get_bytes()
        expected = (17, 1)
        self.assertEqual(actual, expected)
    #enddef

    def testAdd(self):
        x = SizeStruct.new_from_bytes(8, 1)
        y = SizeStruct.new_from_bytes(16, 1)
        actual = x.add(y).get_bytes()
        expected = (24, 1)
        self.assertEqual(actual, expected)

        x = SizeStruct.new_from_bytes(8, -1)
        y = SizeStruct.new_from_bytes(16, 1)
        actual = x.add(y).get_bytes()
        expected = (8, 1)
        self.assertEqual(actual, expected)

        x = SizeStruct.new_from_bytes(8, -1)
        y = SizeStruct.new_from_bytes(16, -1)
        actual = x.add(y).get_bytes()
        expected = (24, -1)
        self.assertEqual(actual, expected)

        x = SizeStruct.new_from_bytes(0, 0)
        y = SizeStruct.new_from_bytes(16, -1)
        actual = x.add(y).get_bytes()
        expected = (16, -1)
        self.assertEqual(actual, expected)

        x = SizeStruct.new_from_bytes(0, 0)
        y = SizeStruct.new_from_bytes(0, 0)
        actual = x.add(y).get_bytes()
        expected = (0, 0)
        self.assertEqual(actual, expected)
    #enddef


    def testAddBytes(self):
        x = SizeStruct.new_from_bytes(8, 1)
        actual = x.add_bytes(16).get_bytes()
        expected = (24, 1)
        self.assertEqual(actual, expected)

        x = SizeStruct.new_from_bytes(8, -1)
        actual = x.add_bytes(8).get_bytes()
        expected = (0, 0)
        self.assertEqual(actual, expected)

        x = SizeStruct.new_from_bytes(8, -1)
        actual = x.add_bytes(0).get_bytes()
        expected = (8, -1)
        self.assertEqual(actual, expected)

        # try some big value (bigger than ULONG_MAX on 32bit arches)
        x = SizeStruct.new_from_bytes(0, 0)
        actual = x.add_bytes(2**36).get_bytes()
        expected = (2**36, 1)
        self.assertEqual(actual, expected)
    #enddef

    def testSub(self):
        x = SizeStruct.new_from_bytes(8, 1)
        y = SizeStruct.new_from_bytes(16, 1)
        actual = x.sub(y).get_bytes()
        expected = (8, -1)
        self.assertEqual(actual, expected)

        x = SizeStruct.new_from_bytes(8, -1)
        y = SizeStruct.new_from_bytes(16, 1)
        actual = x.sub(y).get_bytes()
        expected = (24, -1)
        self.assertEqual(actual, expected)

        x = SizeStruct.new_from_bytes(8, -1)
        y = SizeStruct.new_from_bytes(16, -1)
        actual = x.sub(y).get_bytes()
        expected = (8, 1)
        self.assertEqual(actual, expected)

        x = SizeStruct.new_from_bytes(0, 0)
        y = SizeStruct.new_from_bytes(16, -1)
        actual = x.sub(y).get_bytes()
        expected = (16, 1)
        self.assertEqual(actual, expected)

        x = SizeStruct.new_from_bytes(0, 0)
        y = SizeStruct.new_from_bytes(0, 0)
        actual = x.sub(y).get_bytes()
        expected = (0, 0)
        self.assertEqual(actual, expected)
    #enddef

    def testSubBytes(self):
        x = SizeStruct.new_from_bytes(8, 1)
        actual = x.sub_bytes(16).get_bytes()
        expected = (8, -1)
        self.assertEqual(actual, expected)

        x = SizeStruct.new_from_bytes(8, 1)
        actual = x.sub_bytes(8).get_bytes()
        expected = (0, 0)
        self.assertEqual(actual, expected)

        x = SizeStruct.new_from_bytes(8, -1)
        actual = x.sub_bytes(0).get_bytes()
        expected = (8, -1)
        self.assertEqual(actual, expected)

        # try some big value (bigger than ULONG_MAX on 32bit arches)
        x = SizeStruct.new_from_bytes(2**36 + 10, 1)
        actual = x.sub_bytes(2**36).get_bytes()
        expected = (10, 1)
        self.assertEqual(actual, expected)
    #enddef

    def testCmp(self):
        x = SizeStruct.new_from_str("1 KiB")
        y = SizeStruct.new_from_str("-1 KiB")

        # params: SizeStruct x, SizeStruct y, bool abs
        # result > 0
        cmpResult = SizeStruct.cmp(x, y, False)
        self.assertGreater(cmpResult, 0)

        # result < 0
        cmpResult = SizeStruct.cmp(y, x, False)
        self.assertLess(cmpResult, 0)

        # result == 0
        cmpResult = SizeStruct.cmp(y, x, True)
        self.assertEqual(cmpResult, 0)
    #enddef

    def testCmpBytes(self):
        x = SizeStruct.new_from_str("1 KiB")

        # result > 0
        y = 1023
        cmpResult = SizeStruct.cmp_bytes(x, y, False)
        self.assertGreater(cmpResult, 0)

        # result < 0
        y = 1025
        cmpResult = SizeStruct.cmp_bytes(x, y, False)
        self.assertLess(cmpResult, 0)

        # result == 0
        y = 1024
        cmpResult = SizeStruct.cmp_bytes(x, y, False)
        self.assertEqual(cmpResult, 0)

        # test with abs == True
        x = SizeStruct.new_from_str("-1 KiB")

        # result > 0
        y = 1023
        cmpResult = SizeStruct.cmp_bytes(x, y, True)
        self.assertGreater(cmpResult, 0)

        # result < 0
        y = 1025
        cmpResult = SizeStruct.cmp_bytes(x, y, True)
        self.assertLess(cmpResult, 0)

        # result == 0
        y = 1024
        cmpResult = SizeStruct.cmp_bytes(x, y, True)
        self.assertEqual(cmpResult, 0)
    #enddef

    def testConvertTo(self):
        x = SizeStruct.new_from_str("1 KiB")
        x.convert_to(KiB)
    #enddef

    def testDiv(self):
        x = SizeStruct.new_from_str("1 KiB")
        y = SizeStruct.new_from_str("-0.1 KiB")
        divResult = x.div(y)
        self.assertEqual(divResult, (10, -1))

        x = SizeStruct.new_from_str("1 MiB")
        y = SizeStruct.new_from_str("1 KiB")
        divResult = x.div(y)
        self.assertEqual(divResult, (1024, 1))

        x = SizeStruct.new_from_str("1 GB")
        y = SizeStruct.new_from_str("0.7 GB")
        divResult = x.div(y)
        self.assertEqual(divResult, (1, 1))

        x = SizeStruct.new_from_str("-1 KiB")
        y = SizeStruct.new_from_str("0.1 KiB")
        divResult = x.div(y)
        self.assertEqual(divResult, (10, -1))
    #enddef

    def testDivInt(self):
        x = SizeStruct.new_from_str("1 MiB")
        y = 1024
        divResult = x.div_int(y).get_bytes()
        self.assertEqual(divResult, (1024, 1))

        x = SizeStruct.new_from_str("-1 MiB")
        y = 1077
        divResult = x.div_int(y).get_bytes()
        self.assertEqual(divResult, (973, -1))

        try:
            x = SizeStruct.new_from_bytes(2 * 2**36, 1)
            y = 2**36
            res = x.div_int(y).get_bytes()
            self.assertEqual(res, (2, 1))
        except OverflowError:
            # ULONG_MAX is the real limit for division, if it's smaller than
            # UINT64_MAX, an error is expected, otherwise it is a bug
            if ctypes.sizeof(ctypes.c_ulong) == 4:
                pass
    #enddef

    def testGetBytesStr(self):
        strSizeStruct = SizeStruct.new_from_str("-1 KiB").get_bytes_str()
        self.assertEqual(strSizeStruct, "-1024")
    #enddef

    @requires_locales({'cs_CZ.UTF-8'})
    def testHumanReadable(self):
        strSizeStruct = SizeStruct.new_from_str("12 KiB").human_readable(KiB, 2, False)
        self.assertEqual(strSizeStruct, "12 KiB")

        strSizeStruct = SizeStruct.new_from_str("1 KB").human_readable(KiB, 2, False)
        self.assertEqual(strSizeStruct, "0.98 KiB")

        locale.setlocale(locale.LC_ALL, 'cs_CZ.UTF-8')
        strSizeStruct = SizeStruct.new_from_str("1 KB").human_readable(KiB, 2, True)
        self.assertEqual(strSizeStruct, "0,98 KiB")
        locale.setlocale(locale.LC_ALL, DEFAULT_LOCALE);

        strSizeStruct = SizeStruct.new_from_str("100 GiB").human_readable(KiB, 2, False)
        self.assertEqual(strSizeStruct, "100 GiB")

        strSizeStruct = SizeStruct.new_from_str("100.00 GiB").human_readable(KiB, 2, False)
        self.assertEqual(strSizeStruct, "100 GiB")

        strSizeStruct = SizeStruct.new_from_str("100 GiB").human_readable(KiB, 0, False)
        self.assertEqual(strSizeStruct, "100 GiB")

        # test that the result of human_readable() can be parsed back
        strSizeStruct = SizeStruct.new_from_str("100 GiB").human_readable(GiB, 0, False)
        self.assertEqual(SizeStruct.new_from_str(strSizeStruct).get_bytes(), (100 * 1024**3, 1))

        # even if translated
        strSizeStruct = SizeStruct.new_from_str("100 GiB").human_readable(GiB, 0, True)
        self.assertEqual(SizeStruct.new_from_str(strSizeStruct).get_bytes(), (100 * 1024**3, 1))
    #enddef

    def testSgn(self):
        sgn = SizeStruct.new_from_str("12 KiB").sgn()
        self.assertEqual(sgn, 1)

        sgn = SizeStruct.new_from_str("0 MB").sgn()
        self.assertEqual(sgn, 0)

        sgn = SizeStruct.new_from_str("-12 GiB").sgn()
        self.assertEqual(sgn, -1)
    #enddef

    def testTrueDiv(self):
        x = SizeStruct.new_from_str("1024 B")
        y = SizeStruct.new_from_str("-102.4 B") # rounds to whole bytes
        divResult = float(x.true_div(y)[:15].replace(locale.nl_langinfo(locale.RADIXCHAR), ".")) # just some number to cover accurancy and not cross max float range
        self.assertAlmostEqual(divResult, 1024.0/-102.0)

        x = SizeStruct.new_from_str("1 MiB")
        y = SizeStruct.new_from_str("1 KiB")
        divResult = float(x.true_div(y)[:15].replace(locale.nl_langinfo(locale.RADIXCHAR), ".")) # just some number to cover accurancy and not cross max float range
        self.assertAlmostEqual(divResult, 1024.0)
    #enddef

    def testMod(self):
        x = SizeStruct.new_from_str("1024 B")
        y = SizeStruct.new_from_str("1000 B")
        actual = x.mod(y).get_bytes()
        expected = (24, 1)
        self.assertEqual(actual, expected)

        # when modding the signs are ignored

        x = SizeStruct.new_from_str("1024 B")
        y = SizeStruct.new_from_str("-1000 B")
        actual = x.mod(y).get_bytes()
        expected = (24, 1)
        self.assertEqual(actual, expected)

        x = SizeStruct.new_from_str("-1024 B")
        y = SizeStruct.new_from_str("1000 B")
        actual = x.mod(y).get_bytes()
        expected = (24, 1)
        self.assertEqual(actual, expected)

        x = SizeStruct.new_from_str("-1024 B")
        y = SizeStruct.new_from_str("-1000 B")
        actual = x.mod(y).get_bytes()
        expected = (24, 1)
        self.assertEqual(actual, expected)

        x = SizeStruct.new_from_str("1024 B")
        y = SizeStruct.new_from_str("1024 B")
        actual = x.mod(y).get_bytes()
        expected = (0, 0)
        self.assertEqual(actual, expected)
    #enddef

    def testMulFloatStr(self):
        x = SizeStruct.new_from_str("8 B")
        actual = x.mul_float_str("1.51").get_bytes()
        self.assertEqual(actual, (12, 1))

        x = SizeStruct.new_from_str("-8 B")
        actual = x.mul_float_str("1.51").get_bytes()
        self.assertEqual(actual, (12, -1))

        x = SizeStruct.new_from_str("8 B")
        actual = x.mul_float_str("-1.51").get_bytes()
        self.assertEqual(actual, (12, -1))
    #enddef

    def testMulInt(self):
        x = SizeStruct.new_from_str("8 B")
        y = 2
        actual = x.mul_int(y).get_bytes()
        expected = (16, 1)
        self.assertEqual(actual, expected)

        x = SizeStruct.new_from_str("0 B")
        y = 1
        actual = x.mul_int(y).get_bytes()
        expected = (0, 0)
        self.assertEqual(actual, expected)

        x = SizeStruct.new_from_str("10 B")
        y = 0
        actual = x.mul_int(y).get_bytes()
        expected = (0, 0)
        self.assertEqual(actual, expected)

        x = SizeStruct.new_from_bytes(4, 1)
        y = 2**36
        actual = x.mul_int(y).get_bytes()
        expected = (4 * 2**36, 1)
        self.assertEqual(actual, expected)
    #enddef

    def testRoundToNearest(self):
        x = SizeStruct.new_from_str("1500 B")
        roundTo = SizeStruct.new_from_str("1 KiB")
        actual = x.round_to_nearest(roundTo, ROUND_UP).get_bytes()
        expected = (2048, 1)
        self.assertEqual(actual, expected)

        x = SizeStruct.new_from_str("1500 B")
        roundTo = SizeStruct.new_from_str("1 KiB")
        actual = x.round_to_nearest(roundTo, ROUND_DOWN).get_bytes()
        expected = (1024, 1)
        self.assertEqual(actual, expected)

        x = SizeStruct.new_from_str("1500 B")
        roundTo = SizeStruct.new_from_str("10 KiB")
        actual = x.round_to_nearest(roundTo, ROUND_DOWN).get_bytes()
        expected = (0, 0)
        self.assertEqual(actual, expected)

        x = SizeStruct.new_from_str("1024 B")
        roundTo = SizeStruct.new_from_str("1 KiB")
        actual = x.round_to_nearest(roundTo, ROUND_DOWN).get_bytes()
        expected = (1024, 1)
        self.assertEqual(actual, expected)
        actual = x.round_to_nearest(roundTo, ROUND_UP).get_bytes()
        expected = (1024, 1)
        self.assertEqual(actual, expected)
        actual = x.round_to_nearest(roundTo, ROUND_HALF_UP).get_bytes()
        expected = (1024, 1)
        self.assertEqual(actual, expected)

        x = SizeStruct.new_from_str("1023 B")
        actual = x.round_to_nearest(roundTo, ROUND_DOWN).get_bytes()
        expected = (0, 0)
        self.assertEqual(actual, expected)
        actual = x.round_to_nearest(roundTo, ROUND_UP).get_bytes()
        expected = (1024, 1)
        self.assertEqual(actual, expected)
        actual = x.round_to_nearest(roundTo, ROUND_HALF_UP).get_bytes()
        expected = (1024, 1)
        self.assertEqual(actual, expected)

        x = SizeStruct.new_from_str("1025 B")
        actual = x.round_to_nearest(roundTo, ROUND_DOWN).get_bytes()
        expected = (1024, 1)
        self.assertEqual(actual, expected)
        actual = x.round_to_nearest(roundTo, ROUND_UP).get_bytes()
        expected = (2048, 1)
        self.assertEqual(actual, expected)
        actual = x.round_to_nearest(roundTo, ROUND_HALF_UP).get_bytes()
        expected = (1024, 1)
        self.assertEqual(actual, expected)

        x = SizeStruct.new_from_str("1535 B")
        actual = x.round_to_nearest(roundTo, ROUND_DOWN).get_bytes()
        expected = (1024, 1)
        self.assertEqual(actual, expected)
        actual = x.round_to_nearest(roundTo, ROUND_UP).get_bytes()
        expected = (2048, 1)
        self.assertEqual(actual, expected)
        actual = x.round_to_nearest(roundTo, ROUND_HALF_UP).get_bytes()
        expected = (1024, 1)
        self.assertEqual(actual, expected)

        x = SizeStruct.new_from_str("1536 B")
        actual = x.round_to_nearest(roundTo, ROUND_DOWN).get_bytes()
        expected = (1024, 1)
        self.assertEqual(actual, expected)
        actual = x.round_to_nearest(roundTo, ROUND_UP).get_bytes()
        expected = (2048, 1)
        self.assertEqual(actual, expected)
        actual = x.round_to_nearest(roundTo, ROUND_HALF_UP).get_bytes()
        expected = (2048, 1)
        self.assertEqual(actual, expected)

        # now check something bigger
        x = SizeStruct.new_from_str("575 GiB")
        roundTo = SizeStruct.new_from_str("128 GiB")
        actual = x.round_to_nearest(roundTo, ROUND_HALF_UP).get_bytes_str()
        expected = SizeStruct.new_from_str("512 GiB").get_bytes_str()
        self.assertEqual(actual, expected)

        x = SizeStruct.new_from_str("576 GiB")
        roundTo = SizeStruct.new_from_str("128 GiB")
        actual = x.round_to_nearest(roundTo, ROUND_HALF_UP).get_bytes_str()
        expected = SizeStruct.new_from_str("640 GiB").get_bytes_str()
        self.assertEqual(actual, expected)
    #enddef

    def testGrow(self):
        x = SizeStruct.new_from_bytes(16, 1)
        y = SizeStruct.new_from_bytes(8, 1)
        x.grow(y)
        actual = x.get_bytes()
        expected = (24, 1)
        self.assertEqual(actual, expected)
    #enddef

    def testGrowBytes(self):
        x = SizeStruct.new_from_bytes(16, 1)
        x.grow_bytes(8)
        actual = x.get_bytes()
        expected = (24, 1)
        self.assertEqual(actual, expected)

        x = SizeStruct.new_from_bytes(16, 1)
        x.grow_bytes(2**36)
        actual = x.get_bytes()
        expected = (16 + 2**36, 1)
        self.assertEqual(actual, expected)
    #enddef

    def testGrowMulFloatStr(self):
        x = SizeStruct.new_from_str("8 B")
        x.grow_mul_float_str("1.51")
        actual = x.get_bytes()
        self.assertEqual(actual, (12, 1))

        x = SizeStruct.new_from_str("-8 B")
        x.grow_mul_float_str("1.51")
        actual = x.get_bytes()
        self.assertEqual(actual, (12, -1))

        x = SizeStruct.new_from_str("8 B")
        x.grow_mul_float_str("-1.51")
        actual = x.get_bytes()
        self.assertEqual(actual, (12, -1))
    #enddef

    def testGrowMulInt(self):
        x = SizeStruct.new_from_str("8 B")
        x.grow_mul_int(2)
        actual = x.get_bytes()
        expected = (16, 1)
        self.assertEqual(actual, expected)

        x = SizeStruct.new_from_str("0 B")
        x.grow_mul_int(1)
        actual = x.get_bytes()
        expected = (0, 0)
        self.assertEqual(actual, expected)

        x = SizeStruct.new_from_str("10 B")
        x.grow_mul_int(0)
        actual = x.get_bytes()
        expected = (0, 0)
        self.assertEqual(actual, expected)

        x = SizeStruct.new_from_bytes(4, 1)
        x.grow_mul_int(2**36)
        actual = x.get_bytes()
        expected = (4 * 2**36, 1)
        self.assertEqual(actual, expected)
    #enddef

    def testShrink(self):
        x = SizeStruct.new_from_bytes(16, 1)
        y = SizeStruct.new_from_bytes(8, 1)
        x.shrink(y)
        actual = x.get_bytes()
        expected = (8, 1)
        self.assertEqual(actual, expected)

        x = SizeStruct.new_from_bytes(8, 1)
        y = SizeStruct.new_from_bytes(16, 1)
        x.shrink(y)
        actual = x.get_bytes()
        expected = (8, -1)
        self.assertEqual(actual, expected)
    #enddef

    def testShrinkBytes(self):
        x = SizeStruct.new_from_str("8 B")
        x.shrink_bytes(2)
        actual = x.get_bytes()
        expected = (6, 1)
        self.assertEqual(actual, expected)

        x = SizeStruct.new_from_str("8 B")
        x.shrink_bytes(16)
        actual = x.get_bytes()
        expected = (8, -1)
        self.assertEqual(actual, expected)

        x = SizeStruct.new_from_str("-8 B")
        x.shrink_bytes(8)
        actual = x.get_bytes()
        expected = (16, -1)
        self.assertEqual(actual, expected)

        x = SizeStruct.new_from_bytes(2 * 2**36, 1)
        x.shrink_bytes(2**36)
        actual = x.get_bytes()
        expected = (2**36, 1)
        self.assertEqual(actual, expected)
    #enddef

    def testShrinkDivInt(self):
        x = SizeStruct.new_from_str("100 B")
        y = 11
        x.shrink_div_int(y)
        actual = x.get_bytes()
        expected = (9, 1)
        self.assertEqual(actual, expected)

        x = SizeStruct.new_from_str("98 B")
        y = 11
        x.shrink_div_int(y)
        actual = x.get_bytes()
        expected = (8, 1)
        self.assertEqual(actual, expected)

        x = SizeStruct.new_from_bytes(2 * 2**36, 1)
        y = 2**36
        try:
            res = x.shrink_div_int(y).get_bytes()
            self.assertEqual(res, (2, 1))
        except OverflowError:
            # ULONG_MAX is the real limit for division, if it's smaller than
            # UINT64_MAX, an error is expected, otherwise it is a bug
            if ctypes.sizeof(ctypes.c_ulong) == 4:
                pass
    #enddef

    def testTrueDivInt(self):
        x = SizeStruct.new_from_str("1000 B")
        y = 100
        divResult = float(x.true_div_int(y)[:15]) # just some number to cover accuracy and not cross max float range
        self.assertAlmostEqual(divResult, 1000.0/100.0)

        x = SizeStruct.new_from_str("-1 MiB")
        y = 1024
        divResult = float(x.true_div_int(y)[:15]) # just some number to cover accuracy and not cross max float range
        self.assertAlmostEqual(divResult, -1024.0)

        x = SizeStruct.new_from_str("0 MiB")
        y = 1024
        divResult = float(x.true_div_int(y)[:15]) # just some number to cover accuracy and not cross max float range
        self.assertAlmostEqual(divResult, 0.0)

        x = SizeStruct.new_from_bytes(10 * 2**36, 1)
        y = 2**36
        try:
            res = float(x.true_div_int(y)[:15])
            self.assertAlmostEqual(res, 10.0)
        except OverflowError:
            # ULONG_MAX is the real limit for division, if it's smaller than
            # UINT64_MAX, an error is expected, otherwise it is a bug
            if ctypes.sizeof(ctypes.c_ulong) == 4:
                pass
    #enddef

#endclass


# script entry point
if __name__=='__main__':
    if len(sys.argv) > 1:
        DEFAULT_LOCALE = sys.argv[1]
        # the unittest module would try to intepret the argument too, let's
        # remove it
        sys.argv = [sys.argv[0]]
    unittest.main()
#endif