Source

pyobjc / pyobjc-core / PyObjCTest / test_number_proxy.py

Full commit
  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
"""
Tests for the proxy of Python numbers

NOTE: Decimal conversion is not tested, the required proxy is part of
the Foundation bindings :-(
"""
from __future__ import unicode_literals
import sys, os
from PyObjCTools.TestSupport import *
from PyObjCTest.fnd import NSNumber, NSNumberFormatter
from PyObjCTest.pythonnumber import OC_TestNumber
import objc


if sys.version_info[0] == 3:
    unicode = str
    long = int

OC_PythonNumber = objc.lookUpClass("OC_PythonNumber")
try:
    NSCFNumber = objc.lookUpClass("__NSCFNumber")
except objc.error:
    NSCFNumber = objc.lookUpClass("NSCFNumber")


NSOrderedAscending = -1
NSOrderedSame = 0
NSOrderedDescending = 1

class TestNSNumber (TestCase):
    # These testcases check the behaviour of NSNumber, these
    # are mostly here to verify that NSNumbers behave as
    # we expect them to.

    def testClass(self):
        for m in ('numberWithInt_', 'numberWithFloat_', 'numberWithDouble_', 'numberWithShort_'):
            v = getattr(NSNumber, m)(0)
            self.assertIsInstance(v, NSNumber)
            self.assertIsNotInstance(v, OC_PythonNumber)
            self.assertIs(OC_TestNumber.numberClass_(v), NSCFNumber)

    def testDecimal(self):
        NSDecimalNumber = objc.lookUpClass("NSDecimalNumber")
        v = NSDecimalNumber.numberWithInt_(10)
        self.assertIsInstance(v, NSDecimalNumber)

        from objc._pythonify import numberWrapper
        o = numberWrapper(v)
        self.assertIs(o, v)

    def testLongValue(self):
        v = NSNumber.numberWithUnsignedLongLong_(2 ** 63 + 5000)
        self.assertIsInstance(v, long)

        if os_release() <= '10.5':
            self.assertEqual(v.description(), str(-2**63+5000))
        else:
            self.assertEqual(v.description(), str(2**63+5000))

        self.assertIsNot(type(v), long)

        self.assertRaises(AttributeError, setattr, v, 'x', 42)

    def testEdgeCases(self):
        from objc._pythonify import numberWrapper

        n = objc.lookUpClass('NSObject').alloc().init()

        with filterWarnings("error", RuntimeWarning):
            self.assertRaises(RuntimeWarning, numberWrapper, n)

        with filterWarnings("ignore", RuntimeWarning):
            self.assertIs(numberWrapper(n), n)


        # Fake number class, to ensure that all of
        # numberWrapper can be tested with a 64-bit runtime
        class Number (objc.lookUpClass("NSObject")):
            def objCType(self):
                return objc._C_INT

            def longValue(self):
                return 42

        n = Number.alloc().init()
        v = numberWrapper(n)
        self.assertEqual(v, 42)
        self.assertIs(v.__pyobjc_object__, n)

    def testPickling(self):
        v = {
            'long': NSNumber.numberWithUnsignedLongLong_(2 ** 63 + 5000),
            'int':  NSNumber.numberWithInt_(42),
            'float': NSNumber.numberWithDouble_(2.0),
        }
        import pickle
        data = pickle.dumps(v)

        w = pickle.loads(data)
        if os_release() <= '10.5':
            self.assertEqual(w, {
                'long': -2**63 + 5000,
                'int': 42,
                'float': 2.0,
            })
        else:
            self.assertEqual(w, {
                'long': 2**63 + 5000,
                'int': 42,
                'float': 2.0,
            })

        for o in v.values():
            self.assertTrue(hasattr(o, '__pyobjc_object__'))

        for o in w.values():
            self.assertFalse(hasattr(o, '__pyobjc_object__'))

    def testShortConversions(self):
        v = NSNumber.numberWithShort_(42)

        self.assertEqual(v.stringValue(), '42')

        self.assertEqual(OC_TestNumber.numberAsBOOL_(v), 1)
        self.assertEqual(OC_TestNumber.numberAsChar_(v), 42)
        self.assertEqual(OC_TestNumber.numberAsShort_(v), 42)
        self.assertEqual(OC_TestNumber.numberAsInt_(v), 42)
        self.assertEqual(OC_TestNumber.numberAsLong_(v), 42)
        self.assertEqual(OC_TestNumber.numberAsLongLong_(v), 42)
        self.assertEqual(OC_TestNumber.numberAsUnsignedChar_(v), 42)
        self.assertEqual(OC_TestNumber.numberAsUnsignedShort_(v), 42)
        self.assertEqual(OC_TestNumber.numberAsUnsignedInt_(v), 42)
        self.assertEqual(OC_TestNumber.numberAsUnsignedLong_(v), 42)
        self.assertEqual(OC_TestNumber.numberAsUnsignedLongLong_(v), 42)
        self.assertEqual(OC_TestNumber.numberAsFloat_(v), 42.0)
        self.assertEqual(OC_TestNumber.numberAsDouble_(v), 42.0)


    def testIntConversions(self):
        v = NSNumber.numberWithInt_(42)

        self.assertEqual(v.stringValue(), '42')

        self.assertEqual(OC_TestNumber.numberAsBOOL_(v), 1)
        self.assertEqual(OC_TestNumber.numberAsChar_(v), 42)
        self.assertEqual(OC_TestNumber.numberAsShort_(v), 42)
        self.assertEqual(OC_TestNumber.numberAsInt_(v), 42)
        self.assertEqual(OC_TestNumber.numberAsLong_(v), 42)
        self.assertEqual(OC_TestNumber.numberAsLongLong_(v), 42)
        self.assertEqual(OC_TestNumber.numberAsUnsignedChar_(v), 42)
        self.assertEqual(OC_TestNumber.numberAsUnsignedShort_(v), 42)
        self.assertEqual(OC_TestNumber.numberAsUnsignedInt_(v), 42)
        self.assertEqual(OC_TestNumber.numberAsUnsignedLong_(v), 42)
        self.assertEqual(OC_TestNumber.numberAsUnsignedLongLong_(v), 42)
        self.assertEqual(OC_TestNumber.numberAsFloat_(v), 42.0)
        self.assertEqual(OC_TestNumber.numberAsDouble_(v), 42.0)

        # Negative values
        v = NSNumber.numberWithInt_(-42)

        self.assertEqual(v.stringValue(), '-42')

        self.assertEqual(OC_TestNumber.numberAsBOOL_(v), 1)
        self.assertEqual(OC_TestNumber.numberAsChar_(v), -42)
        self.assertEqual(OC_TestNumber.numberAsShort_(v), -42)
        self.assertEqual(OC_TestNumber.numberAsInt_(v), -42)
        self.assertEqual(OC_TestNumber.numberAsLong_(v), -42)
        self.assertEqual(OC_TestNumber.numberAsLongLong_(v), -42)
        self.assertEqual(OC_TestNumber.numberAsUnsignedChar_(v), 214)
        self.assertEqual(OC_TestNumber.numberAsUnsignedShort_(v), 65494)
        self.assertEqual(OC_TestNumber.numberAsUnsignedInt_(v), 4294967254)

        if sys.maxsize == (2 ** 31) -1:
            self.assertEqual(OC_TestNumber.numberAsUnsignedLong_(v), 4294967254)
        else:
            self.assertEqual(OC_TestNumber.numberAsUnsignedLong_(v), 18446744073709551574)

        self.assertEqual(OC_TestNumber.numberAsUnsignedLongLong_(v), 18446744073709551574)
        self.assertEqual(OC_TestNumber.numberAsFloat_(v), -42.0)
        self.assertEqual(OC_TestNumber.numberAsDouble_(v), -42.0)

        # Overflow
        v = NSNumber.numberWithInt_(892455)

        self.assertEqual(OC_TestNumber.numberAsBOOL_(v), 1)
        self.assertEqual(OC_TestNumber.numberAsChar_(v), 39)
        self.assertEqual(OC_TestNumber.numberAsShort_(v), -25049)
        self.assertEqual(OC_TestNumber.numberAsUnsignedChar_(v), 39)
        self.assertEqual(OC_TestNumber.numberAsUnsignedShort_(v), 40487)

    def testDoubleConversions(self):
        v = NSNumber.numberWithDouble_(75.5)
        self.assertEqual(v.stringValue(), '75.5')

        self.assertEqual(OC_TestNumber.numberAsBOOL_(v), 1)
        self.assertEqual(OC_TestNumber.numberAsChar_(v), 75)
        self.assertEqual(OC_TestNumber.numberAsShort_(v), 75)
        self.assertEqual(OC_TestNumber.numberAsInt_(v), 75)
        self.assertEqual(OC_TestNumber.numberAsLong_(v), 75)
        self.assertEqual(OC_TestNumber.numberAsLongLong_(v), 75)
        self.assertEqual(OC_TestNumber.numberAsUnsignedChar_(v), 75)
        self.assertEqual(OC_TestNumber.numberAsUnsignedShort_(v), 75)
        self.assertEqual(OC_TestNumber.numberAsUnsignedInt_(v), 75)
        self.assertEqual(OC_TestNumber.numberAsUnsignedLong_(v), 75)
        self.assertEqual(OC_TestNumber.numberAsUnsignedLongLong_(v), 75)
        self.assertEqual(OC_TestNumber.numberAsFloat_(v), 75.5)
        self.assertEqual(OC_TestNumber.numberAsDouble_(v), 75.5)

        # Negative values
        v = NSNumber.numberWithDouble_(-127.6)
        self.assertEqual(v.stringValue(), '-127.6')

        self.assertEqual(OC_TestNumber.numberAsBOOL_(v), 1)
        self.assertEqual(OC_TestNumber.numberAsChar_(v), -127)
        self.assertEqual(OC_TestNumber.numberAsShort_(v), -127)
        self.assertEqual(OC_TestNumber.numberAsInt_(v), -127)
        self.assertEqual(OC_TestNumber.numberAsLong_(v), -127)
        self.assertEqual(OC_TestNumber.numberAsLongLong_(v), -127)
        self.assertEqual(OC_TestNumber.numberAsUnsignedChar_(v), 129)
        self.assertEqual(OC_TestNumber.numberAsUnsignedShort_(v), 65409)
        self.assertEqual(OC_TestNumber.numberAsUnsignedInt_(v), 4294967169)

        if sys.maxsize == (2 ** 31) -1:
            self.assertEqual(OC_TestNumber.numberAsUnsignedLong_(v), 4294967169)
        else:
            self.assertEqual(OC_TestNumber.numberAsUnsignedLong_(v), 18446744073709551488)

        # The first entry in the tuple below is incorrect, that happens to be what
        # is returned by NSNumber on some platforms (in particular, any Python where
        # the python framework itself is linked against the 10.4 SDK)
        #
        #   double v = -127.6;
        #   unsigned long long lv = v;
        #   printf("%llu\n", lv);
        #

        self.assertIn(
                OC_TestNumber.numberAsUnsignedLongLong_(v),
                    (18446744073709551489, 18446744073709551488))

        self.assertEqual(OC_TestNumber.numberAsDouble_(v), -127.6)

        # Overflow
        v = NSNumber.numberWithDouble_(float(2**64 + 99))

        self.assertEqual(OC_TestNumber.numberAsBOOL_(v), 1)

        if sys.byteorder == 'big':
            self.assertEqual(OC_TestNumber.numberAsChar_(v), -1)
            self.assertEqual(OC_TestNumber.numberAsShort_(v), -1)
            self.assertEqual(OC_TestNumber.numberAsUnsignedChar_(v), 255)
            self.assertEqual(OC_TestNumber.numberAsUnsignedShort_(v), 65535)
        else:
            self.assertEqual(OC_TestNumber.numberAsChar_(v), 0)
            self.assertEqual(OC_TestNumber.numberAsShort_(v), 0)
            self.assertEqual(OC_TestNumber.numberAsUnsignedChar_(v), 0)
            self.assertEqual(OC_TestNumber.numberAsUnsignedShort_(v), 0)

    def testCompare(self):
        self.assertEqual(OC_TestNumber.compareA_andB_(NSNumber.numberWithLong_(0), NSNumber.numberWithLong_(1)), NSOrderedAscending)
        self.assertEqual(OC_TestNumber.compareA_andB_(NSNumber.numberWithLong_(0), NSNumber.numberWithUnsignedLongLong_(2**40)), NSOrderedAscending)
        self.assertEqual(OC_TestNumber.compareA_andB_(NSNumber.numberWithLong_(0), NSNumber.numberWithDouble_(42.0)), NSOrderedAscending)

        self.assertEqual(OC_TestNumber.compareA_andB_(NSNumber.numberWithLong_(0), NSNumber.numberWithLong_(-1)), NSOrderedDescending)
        self.assertEqual(OC_TestNumber.compareA_andB_(NSNumber.numberWithLong_(0), NSNumber.numberWithLongLong_(-2**60)), NSOrderedDescending)
        self.assertEqual(OC_TestNumber.compareA_andB_(NSNumber.numberWithLong_(0), NSNumber.numberWithDouble_(-42.0)), NSOrderedDescending)

        self.assertEqual(OC_TestNumber.compareA_andB_(NSNumber.numberWithLong_(0), NSNumber.numberWithLong_(0)), NSOrderedSame)
        self.assertEqual(OC_TestNumber.compareA_andB_(NSNumber.numberWithLong_(0), NSNumber.numberWithDouble_(0.0)), NSOrderedSame)
        self.assertEqual(OC_TestNumber.compareA_andB_(NSNumber.numberWithLong_(0), NSNumber.numberWithLongLong_(0)), NSOrderedSame)

    def testDescription(self):
        v = OC_TestNumber.numberDescription_(NSNumber.numberWithInt_(0))
        self.assertIsInstance(v, unicode)
        self.assertEqual(v, "0")

        v = OC_TestNumber.numberDescription_(NSNumber.numberWithLongLong_(2**60))
        self.assertIsInstance(v, unicode)
        self.assertEqual(v, unicode(str(2**60)))

        v = OC_TestNumber.numberDescription_(NSNumber.numberWithLongLong_(-2**60))
        self.assertIsInstance(v, unicode)
        self.assertEqual(v, unicode(str(-2**60)))

        v = OC_TestNumber.numberDescription_(NSNumber.numberWithDouble_(264.0))
        self.assertIsInstance(v, unicode)
        self.assertEqual(v, "264")


class TestPyNumber (TestCase):
    # Basic tests of the proxy methods

    def testClasses(self):
        # Ensure that python numbers are proxied using the right proxy type
        for v in (0, 1, 2**32+1, 2**64+1, 42.5):
            self.assertIs(OC_TestNumber.numberClass_(v), OC_PythonNumber)

        # The booleans True and False must be proxied as the corresponding
        # NSNumber constants, otherwise lowlevel Cocoa/CoreFoundation code
        # get's upset.
        try:
            boolClass = objc.lookUpClass('__NSCFBoolean')
        except objc.error:
            boolClass = objc.lookUpClass('NSCFBoolean')

        for v in (True, False):
            self.assertIs(OC_TestNumber.numberClass_(v), boolClass)
            self.assertIs(objc.repythonify(v), v)


    def testPythonIntConversions(self):
        # Conversions to other values. Note that values are converted
        # using C casts, without any exceptions when converting a
        # negative value to an unsigned one and without exceptions for
        # overflow.
        v = 42

        self.assertEqual(OC_TestNumber.numberAsBOOL_(v), 1)
        self.assertEqual(OC_TestNumber.numberAsChar_(v), 42)
        self.assertEqual(OC_TestNumber.numberAsShort_(v), 42)
        self.assertEqual(OC_TestNumber.numberAsInt_(v), 42)
        self.assertEqual(OC_TestNumber.numberAsLong_(v), 42)
        self.assertEqual(OC_TestNumber.numberAsLongLong_(v), 42)
        self.assertEqual(OC_TestNumber.numberAsUnsignedChar_(v), 42)
        self.assertEqual(OC_TestNumber.numberAsUnsignedShort_(v), 42)
        self.assertEqual(OC_TestNumber.numberAsUnsignedInt_(v), 42)
        self.assertEqual(OC_TestNumber.numberAsUnsignedLong_(v), 42)
        self.assertEqual(OC_TestNumber.numberAsUnsignedLongLong_(v), 42)
        self.assertEqual(OC_TestNumber.numberAsUnsignedLongLong_(v), 42)
        self.assertEqual(OC_TestNumber.numberAsFloat_(v), 42.0)
        self.assertEqual(OC_TestNumber.numberAsDouble_(v), 42.0)

        # Negative values
        v = -42

        self.assertEqual(OC_TestNumber.numberAsBOOL_(v), 1)
        self.assertEqual(OC_TestNumber.numberAsChar_(v), -42)
        self.assertEqual(OC_TestNumber.numberAsShort_(v), -42)
        self.assertEqual(OC_TestNumber.numberAsInt_(v), -42)
        self.assertEqual(OC_TestNumber.numberAsLong_(v), -42)
        self.assertEqual(OC_TestNumber.numberAsLongLong_(v), -42)
        self.assertEqual(OC_TestNumber.numberAsUnsignedChar_(v), 214)
        self.assertEqual(OC_TestNumber.numberAsUnsignedShort_(v), 65494)
        self.assertEqual(OC_TestNumber.numberAsUnsignedInt_(v), 4294967254)

        if sys.maxsize == (2 ** 31) -1:
            self.assertEqual(OC_TestNumber.numberAsUnsignedLong_(v), 4294967254)
        else:
            self.assertEqual(OC_TestNumber.numberAsUnsignedLong_(v), 18446744073709551574)

        self.assertEqual(OC_TestNumber.numberAsUnsignedLongLong_(v), 18446744073709551574)
        self.assertEqual(OC_TestNumber.numberAsFloat_(v), -42.0)
        self.assertEqual(OC_TestNumber.numberAsDouble_(v), -42.0)

        # Overflow
        v = 892455

        self.assertEqual(OC_TestNumber.numberAsBOOL_(v), 1)
        self.assertEqual(OC_TestNumber.numberAsChar_(v), 39)
        self.assertEqual(OC_TestNumber.numberAsShort_(v), -25049)
        self.assertEqual(OC_TestNumber.numberAsUnsignedChar_(v), 39)
        self.assertEqual(OC_TestNumber.numberAsUnsignedShort_(v), 40487)

    def testPythonLongConversions(self):
        if sys.version_info[0] == 2:
            v = long(42)
            self.assertIsInstance(v, long)
        else:
            v = 42

        self.assertEqual(OC_TestNumber.numberAsBOOL_(v), 1)
        self.assertEqual(OC_TestNumber.numberAsChar_(v), 42)
        self.assertEqual(OC_TestNumber.numberAsShort_(v), 42)
        self.assertEqual(OC_TestNumber.numberAsInt_(v), 42)
        self.assertEqual(OC_TestNumber.numberAsLong_(v), 42)
        self.assertEqual(OC_TestNumber.numberAsLongLong_(v), 42)
        self.assertEqual(OC_TestNumber.numberAsUnsignedChar_(v), 42)
        self.assertEqual(OC_TestNumber.numberAsUnsignedShort_(v), 42)
        self.assertEqual(OC_TestNumber.numberAsUnsignedInt_(v), 42)
        self.assertEqual(OC_TestNumber.numberAsUnsignedLong_(v), 42)
        self.assertEqual(OC_TestNumber.numberAsUnsignedLongLong_(v), 42)
        self.assertEqual(OC_TestNumber.numberAsUnsignedLongLong_(v), 42)
        self.assertEqual(OC_TestNumber.numberAsFloat_(v), 42.0)
        self.assertEqual(OC_TestNumber.numberAsDouble_(v), 42.0)

        # Negative values
        if sys.version_info[0] == 2:
            v = long(-42)
            self.assertIsInstance(v, long)
        else:
            v = -42

        self.assertEqual(OC_TestNumber.numberAsBOOL_(v), 1)
        self.assertEqual(OC_TestNumber.numberAsChar_(v), -42)
        self.assertEqual(OC_TestNumber.numberAsShort_(v), -42)
        self.assertEqual(OC_TestNumber.numberAsInt_(v), -42)
        self.assertEqual(OC_TestNumber.numberAsLong_(v), -42)
        self.assertEqual(OC_TestNumber.numberAsLongLong_(v), -42)
        self.assertEqual(OC_TestNumber.numberAsUnsignedChar_(v), 214)
        self.assertEqual(OC_TestNumber.numberAsUnsignedShort_(v), 65494)
        self.assertEqual(OC_TestNumber.numberAsUnsignedInt_(v), 4294967254)

        if sys.maxsize == (2 ** 31) -1:
            self.assertEqual(OC_TestNumber.numberAsUnsignedLong_(v), 4294967254)
        else:
            self.assertEqual(OC_TestNumber.numberAsUnsignedLong_(v), 18446744073709551574)

        self.assertEqual(OC_TestNumber.numberAsUnsignedLongLong_(v), 18446744073709551574)
        self.assertEqual(OC_TestNumber.numberAsFloat_(v), -42.0)
        self.assertEqual(OC_TestNumber.numberAsDouble_(v), -42.0)

        # Overflow
        if sys.version_info[0] == 2:
            v = long(892455)
            self.assertIsInstance(v, long)
        else:
            v = 892455

        self.assertEqual(OC_TestNumber.numberAsBOOL_(v), 1)
        self.assertEqual(OC_TestNumber.numberAsChar_(v), 39)
        self.assertEqual(OC_TestNumber.numberAsShort_(v), -25049)
        self.assertEqual(OC_TestNumber.numberAsUnsignedChar_(v), 39)
        self.assertEqual(OC_TestNumber.numberAsUnsignedShort_(v), 40487)

        # Very much overflow
        v = 2 ** 64 + 1
        self.assertEqual(OC_TestNumber.numberAsBOOL_(v), 1)
        self.assertEqual(OC_TestNumber.numberAsChar_(v), 1)
        self.assertEqual(OC_TestNumber.numberAsShort_(v), 1)
        self.assertEqual(OC_TestNumber.numberAsInt_(v), 1)
        self.assertEqual(OC_TestNumber.numberAsLong_(v), 1)
        self.assertEqual(OC_TestNumber.numberAsLongLong_(v), 1)
        self.assertEqual(OC_TestNumber.numberAsUnsignedChar_(v), 1)
        self.assertEqual(OC_TestNumber.numberAsUnsignedShort_(v), 1)
        self.assertEqual(OC_TestNumber.numberAsUnsignedInt_(v), 1)
        self.assertEqual(OC_TestNumber.numberAsUnsignedLong_(v), 1)
        self.assertEqual(OC_TestNumber.numberAsUnsignedLongLong_(v), 1)

    def testDoubleConversions(self):
        v = 75.5

        self.assertEqual(OC_TestNumber.numberAsBOOL_(v), 1)
        self.assertEqual(OC_TestNumber.numberAsChar_(v), 75)
        self.assertEqual(OC_TestNumber.numberAsShort_(v), 75)
        self.assertEqual(OC_TestNumber.numberAsInt_(v), 75)
        self.assertEqual(OC_TestNumber.numberAsLong_(v), 75)
        self.assertEqual(OC_TestNumber.numberAsLongLong_(v), 75)
        self.assertEqual(OC_TestNumber.numberAsUnsignedChar_(v), 75)
        self.assertEqual(OC_TestNumber.numberAsUnsignedShort_(v), 75)
        self.assertEqual(OC_TestNumber.numberAsUnsignedInt_(v), 75)
        self.assertEqual(OC_TestNumber.numberAsUnsignedLong_(v), 75)
        self.assertEqual(OC_TestNumber.numberAsUnsignedLongLong_(v), 75)
        self.assertEqual(OC_TestNumber.numberAsFloat_(v), 75.5)
        self.assertEqual(OC_TestNumber.numberAsDouble_(v), 75.5)

        # Negative values
        v = -127.6

        self.assertEqual(OC_TestNumber.numberAsBOOL_(v), 1)
        self.assertEqual(OC_TestNumber.numberAsChar_(v), -127)
        self.assertEqual(OC_TestNumber.numberAsShort_(v), -127)
        self.assertEqual(OC_TestNumber.numberAsInt_(v), -127)
        self.assertEqual(OC_TestNumber.numberAsLong_(v), -127)
        self.assertEqual(OC_TestNumber.numberAsLongLong_(v), -127)

        self.assertEqual(OC_TestNumber.numberAsUnsignedChar_(v), 129)
        self.assertEqual(OC_TestNumber.numberAsUnsignedShort_(v), 65409)
        self.assertEqual(OC_TestNumber.numberAsUnsignedInt_(v), 4294967169)

        if sys.maxsize == (2 ** 31) -1:
            self.assertEqual(OC_TestNumber.numberAsUnsignedLong_(v), 4294967169)
        else:
            self.assertEqual(OC_TestNumber.numberAsUnsignedLong_(v), 18446744073709551489)

        if sys.byteorder == 'big':
            self.assertEqual(OC_TestNumber.numberAsUnsignedLongLong_(v), 4294967169)
        else:
            self.assertEqual(OC_TestNumber.numberAsUnsignedLongLong_(v), 18446744073709551489)

        self.assertEqual(OC_TestNumber.numberAsDouble_(v), -127.6)

        # Overflow
        v = float(2**64 + 99)

        self.assertEqual(OC_TestNumber.numberAsBOOL_(v), 1)

        if sys.byteorder == 'big':
            self.assertEqual(OC_TestNumber.numberAsChar_(v), -1)
            self.assertEqual(OC_TestNumber.numberAsShort_(v), -1)
            self.assertEqual(OC_TestNumber.numberAsUnsignedChar_(v), 255)
            self.assertEqual(OC_TestNumber.numberAsUnsignedShort_(v), 65535)
        else:
            self.assertEqual(OC_TestNumber.numberAsChar_(v), 0)
            self.assertEqual(OC_TestNumber.numberAsShort_(v), 0)
            self.assertEqual(OC_TestNumber.numberAsUnsignedChar_(v), 0)
            self.assertEqual(OC_TestNumber.numberAsUnsignedShort_(v), 0)

    def testCompare(self):
        self.assertEqual(OC_TestNumber.compareA_andB_(0, 1), NSOrderedAscending)
        self.assertEqual(OC_TestNumber.compareA_andB_(0, 2**64), NSOrderedAscending)
        self.assertEqual(OC_TestNumber.compareA_andB_(0, 42.0), NSOrderedAscending)

        self.assertEqual(OC_TestNumber.compareA_andB_(0, -1), NSOrderedDescending)
        self.assertEqual(OC_TestNumber.compareA_andB_(0, -2**64), NSOrderedDescending)
        self.assertEqual(OC_TestNumber.compareA_andB_(0, -42.0), NSOrderedDescending)

        self.assertEqual(OC_TestNumber.compareA_andB_(0, 0), NSOrderedSame)
        self.assertEqual(OC_TestNumber.compareA_andB_(0, 0.0), NSOrderedSame)
        if sys.version_info[0] == 2:
            self.assertEqual(OC_TestNumber.compareA_andB_(0, long(0)), NSOrderedSame)

    def testNumberEqual(self):
        self.assertFalse(OC_TestNumber.number_isEqualTo_(0, 1))
        self.assertFalse(OC_TestNumber.number_isEqualTo_(0, 2**64))
        self.assertFalse(OC_TestNumber.number_isEqualTo_(0, 42.0))

        self.assertFalse(OC_TestNumber.number_isEqualTo_(0, -1))
        self.assertFalse(OC_TestNumber.number_isEqualTo_(0, -2**64))
        self.assertFalse(OC_TestNumber.number_isEqualTo_(0, -42.0))

        self.assertTrue(OC_TestNumber.number_isEqualTo_(0, 0))
        self.assertTrue(OC_TestNumber.number_isEqualTo_(0, 0.0))
        if sys.version_info[0] == 2:
            self.assertTrue(OC_TestNumber.number_isEqualTo_(0, long(0)))

    def testDescription(self):
        v = OC_TestNumber.numberDescription_(0)
        self.assertIsInstance(v, unicode)
        self.assertEqual(v, "0")

        v = OC_TestNumber.numberDescription_(2**64)
        self.assertIsInstance(v, unicode)
        self.assertEqual(v, unicode(repr(2**64)))

        v = OC_TestNumber.numberDescription_(-2**64)
        self.assertIsInstance(v, unicode)
        self.assertEqual(v, unicode(repr(-2**64)))

        v = OC_TestNumber.numberDescription_(264.0)
        self.assertIsInstance(v, unicode)
        self.assertEqual(v, "264.0")

        v = OC_TestNumber.numberDescription_(False)
        self.assertIsInstance(v, unicode)
        self.assertEqual(v, "0")

        v = OC_TestNumber.numberDescription_(True)
        self.assertIsInstance(v, unicode)
        self.assertEqual(v, "1")

class TestInteractions (TestCase):
    # Test interactions between Python and NSNumber numbers

    def testMixedCompare(self):
        # compare for:
        #   - python number to nsnumber
        #   - nsnumber to python number
        # For: (bool, int, long, float) vs (char, short, ...)
        methods = [
                'numberWithInt_',
                'numberWithChar_',
                'numberWithLong_',
                'numberWithDouble_',
            ]

        self.assertEqual(OC_TestNumber.compareA_andB_(42, 42), NSOrderedSame)
        for m in methods:
            self.assertEqual(OC_TestNumber.compareA_andB_(getattr(NSNumber, m)(42), 42), NSOrderedSame)
            self.assertEqual(OC_TestNumber.compareA_andB_(42, getattr(NSNumber, m)(42)), NSOrderedSame)

        self.assertEqual(OC_TestNumber.compareA_andB_(42, 99), NSOrderedAscending)
        for m in methods:
            self.assertEqual(OC_TestNumber.compareA_andB_(getattr(NSNumber, m)(42), 99), NSOrderedAscending)
            self.assertEqual(OC_TestNumber.compareA_andB_(42, getattr(NSNumber, m)(99)), NSOrderedAscending)

    def testMixedEquals(self):
        # isEqualToNumber for:
        #   - python number to nsnumber
        #   - nsnumber to python number
        # For: (bool, int, long, float) vs (char, short, ...)
        self.assertTrue(OC_TestNumber.number_isEqualTo_(0, NSNumber.numberWithInt_(0)))
        self.assertTrue(OC_TestNumber.number_isEqualTo_(0, NSNumber.numberWithLong_(0)))
        self.assertTrue(OC_TestNumber.number_isEqualTo_(0, NSNumber.numberWithFloat_(0)))
        self.assertTrue(OC_TestNumber.number_isEqualTo_(NSNumber.numberWithInt_(0), 0))
        self.assertTrue(OC_TestNumber.number_isEqualTo_(NSNumber.numberWithLong_(0), 0))
        self.assertTrue(OC_TestNumber.number_isEqualTo_(NSNumber.numberWithFloat_(0), 0))

        self.assertFalse(OC_TestNumber.number_isEqualTo_(42, NSNumber.numberWithInt_(0)))
        self.assertFalse(OC_TestNumber.number_isEqualTo_(42, NSNumber.numberWithLong_(0)))
        self.assertFalse(OC_TestNumber.number_isEqualTo_(42, NSNumber.numberWithFloat_(0)))
        self.assertFalse(OC_TestNumber.number_isEqualTo_(NSNumber.numberWithInt_(0), 42))
        self.assertFalse(OC_TestNumber.number_isEqualTo_(NSNumber.numberWithLong_(0), 42))
        self.assertFalse(OC_TestNumber.number_isEqualTo_(NSNumber.numberWithFloat_(0), 42))


class TestNumberFormatter (TestCase):
    # Test behaviour of an NSNumberFormatter, both with
    # Python numbers and NSNumbers
    def testFormatting(self):
        formatter = NSNumberFormatter.alloc().init()

        n = NSNumber.numberWithInt_(42)
        p = 42
        self.assertEqual(formatter.stringForObjectValue_(n), formatter.stringForObjectValue_(p))

        n = NSNumber.numberWithInt_(-42)
        p = -42
        self.assertEqual(formatter.stringForObjectValue_(n), formatter.stringForObjectValue_(p))


        n = NSNumber.numberWithDouble_(10.42)
        p = 10.42
        self.assertEqual(formatter.stringForObjectValue_(n), formatter.stringForObjectValue_(p))

if __name__ == "__main__":
    main()