1import sys 2from PyObjCTools.TestSupport import * 3 4import objc 5import struct 6 7NSObject = objc.lookUpClass('NSObject') 8 9class OC_TestConveniences(NSObject): 10 def initWithHashValue_(self, hashValue): 11 self = super(OC_TestConveniences, self).init() 12 self.hashValue = hashValue 13 return self 14 15 def hash(self): 16 return self.hashValue 17 18class TestConveniences(TestCase): 19 20 def testHash(self): 21 for hashValue in (0, sys.maxsize, sys.maxsize + 1, 0xFFFFFFFF): 22 expect = struct.unpack('l', struct.pack('L', hashValue))[0] 23 # Python can't hash to -1. Surprise! :) 24 if expect == -1: 25 expect = -2 26 o = OC_TestConveniences.alloc().initWithHashValue_(hashValue) 27 self.assertEqual(o.hash(), hashValue) 28 self.assertEqual(hash(o), expect, 'o.hash() == 0x%X | %r != %r' % (o.hash(), hash(o), expect)) 29 30if __name__ == '__main__': 31 main() 32