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))
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), 4_294_967_254) if sys.maxsize == (2 ** 31) - 1: self.assertEqual(OC_TestNumber.numberAsUnsignedLong_(v), 4_294_967_254) else: self.assertEqual( OC_TestNumber.numberAsUnsignedLong_(v), 18_446_744_073_709_551_574 ) self.assertEqual( OC_TestNumber.numberAsUnsignedLongLong_(v), 18_446_744_073_709_551_574 ) self.assertEqual(OC_TestNumber.numberAsFloat_(v), -42.0) self.assertEqual(OC_TestNumber.numberAsDouble_(v), -42.0) # Overflow v = NSNumber.numberWithInt_(892_455) 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 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 testIntConversions(self): v = NSNumber.numberWithInt_(42) self.assertEquals(OC_TestNumber.numberAsBOOL_(v), 1) self.assertEquals(OC_TestNumber.numberAsChar_(v), 42) self.assertEquals(OC_TestNumber.numberAsShort_(v), 42) self.assertEquals(OC_TestNumber.numberAsInt_(v), 42) self.assertEquals(OC_TestNumber.numberAsLong_(v), 42) self.assertEquals(OC_TestNumber.numberAsLongLong_(v), 42) self.assertEquals(OC_TestNumber.numberAsUnsignedChar_(v), 42) self.assertEquals(OC_TestNumber.numberAsUnsignedShort_(v), 42) self.assertEquals(OC_TestNumber.numberAsUnsignedInt_(v), 42) self.assertEquals(OC_TestNumber.numberAsUnsignedLong_(v), 42) self.assertEquals(OC_TestNumber.numberAsUnsignedLongLong_(v), 42) self.assertEquals(OC_TestNumber.numberAsFloat_(v), 42.0) self.assertEquals(OC_TestNumber.numberAsDouble_(v), 42.0) # Negative values v = NSNumber.numberWithInt_(-42) self.assertEquals(OC_TestNumber.numberAsBOOL_(v), 1) self.assertEquals(OC_TestNumber.numberAsChar_(v), -42) self.assertEquals(OC_TestNumber.numberAsShort_(v), -42) self.assertEquals(OC_TestNumber.numberAsInt_(v), -42) self.assertEquals(OC_TestNumber.numberAsLong_(v), -42) self.assertEquals(OC_TestNumber.numberAsLongLong_(v), -42) self.assertEquals(OC_TestNumber.numberAsUnsignedChar_(v), 214) self.assertEquals(OC_TestNumber.numberAsUnsignedShort_(v), 65494) self.assertEquals(OC_TestNumber.numberAsUnsignedInt_(v), 4294967254) if sys.maxint == (2**31) - 1: self.assertEquals(OC_TestNumber.numberAsUnsignedLong_(v), 4294967254) else: self.assertEquals(OC_TestNumber.numberAsUnsignedLong_(v), 18446744073709551574) self.assertEquals(OC_TestNumber.numberAsUnsignedLongLong_(v), 18446744073709551574) self.assertEquals(OC_TestNumber.numberAsFloat_(v), -42.0) self.assertEquals(OC_TestNumber.numberAsDouble_(v), -42.0) # Overflow v = NSNumber.numberWithInt_(892455) self.assertEquals(OC_TestNumber.numberAsBOOL_(v), 1) self.assertEquals(OC_TestNumber.numberAsChar_(v), 39) self.assertEquals(OC_TestNumber.numberAsShort_(v), -25049) self.assertEquals(OC_TestNumber.numberAsUnsignedChar_(v), 39) self.assertEquals(OC_TestNumber.numberAsUnsignedShort_(v), 40487)
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))
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' and os_release() != '10.10': 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 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_level_key(os_release()) < os_level_key('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 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_level_key(os_release()) < os_level_key("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 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))
def testPickleInt(self): number_type = OC_PythonLong v = NSNumber.numberWithInt_(42) self.assertIsInstance(v, number_type) # First python pickle s = pickle.dumps(v) v2 = pickle.loads(s) self.assertEqual(v2, v) self.assertIsNotInstance(v2, number_type) self.assertIsInstance(v2, int)
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")
def testPickleInt(self): v = NSNumber.numberWithInt_(42) self.assert_(isinstance(v, OC_PythonInt)) # First python pickle s = pickle.dumps(v) v2 = pickle.loads(s) self.assertEquals(v2, v) self.assert_(not isinstance(v2, OC_PythonInt)) self.assert_(isinstance(v2, int)) # Then C pickle s = cPickle.dumps(v) v2 = cPickle.loads(s) self.assertEquals(v2, v) self.assert_(not isinstance(v2, OC_PythonInt)) self.assert_(isinstance(v2, int))
def testPickleInt(self): if sys.version_info[0] == 2: number_type = OC_PythonInt else: number_type = OC_PythonLong v = NSNumber.numberWithInt_(42) self.assertIsInstance(v, number_type) # First python pickle s = pickle.dumps(v) v2 = pickle.loads(s) self.assertEquals(v2, v) self.assertIsNotInstance(v2, number_type) self.assertIsInstance(v2, int) # Then C pickle s = cPickle.dumps(v) v2 = cPickle.loads(s) self.assertEquals(v2, v) self.assertIsNotInstance(v2, number_type) self.assertIsInstance(v2, int)