1from CoreFoundation import * 2 3from PyObjCTools.TestSupport import * 4 5try: 6 long 7except NameError: 8 long = int 9 10 11class TestCFBinaryHeap (TestCase): 12 def testTypes(self): 13 self.assertIsCFType(CFBinaryHeapRef) 14 15 def testCreation(self): 16 heap = CFBinaryHeapCreate( 17 None, 0) 18 self.assert_(isinstance(heap, CFBinaryHeapRef)) 19 20 CFBinaryHeapAddValue(heap, "hello") 21 CFBinaryHeapAddValue(heap, "world") 22 CFBinaryHeapAddValue(heap, "aapjes") 23 24 self.assertTrue(CFBinaryHeapContainsValue(heap, "hello")) 25 self.assertFalse(CFBinaryHeapContainsValue(heap, "niemand")) 26 27 def testApply(self): 28 def compare(l, r, info): 29 return cmp(l, r) 30 31 heap = CFBinaryHeapCreate( 32 None, 0) 33 self.assert_(isinstance(heap, CFBinaryHeapRef)) 34 35 CFBinaryHeapAddValue(heap, "hello") 36 CFBinaryHeapAddValue(heap, "world") 37 CFBinaryHeapAddValue(heap, "aapjes") 38 39 items = [] 40 contexts = [] 41 42 def function(item, context): 43 items.append(item * 2) 44 contexts.append(context) 45 46 CFBinaryHeapApplyFunction(heap, function, None) 47 self.assertEqual(contexts, [None, None, None]) 48 self.assertEqual(items, ["aapjesaapjes", "hellohello", "worldworld"]) 49 50 ctx = [''] 51 def function(item, context): 52 context[0] += item 53 CFBinaryHeapApplyFunction(heap, function, ctx) 54 self.assertEqual(ctx[0], 'aapjeshelloworld') 55 56 57 def testTypeID(self): 58 v = CFBinaryHeapGetTypeID() 59 self.assertIsInstance(v, (int, long)) 60 def testCopy(self): 61 heap = CFBinaryHeapCreate( 62 None, 0) 63 self.assert_(isinstance(heap, CFBinaryHeapRef)) 64 65 CFBinaryHeapAddValue(heap, "hello") 66 CFBinaryHeapAddValue(heap, "world") 67 CFBinaryHeapAddValue(heap, "aapjes") 68 69 heap2 = CFBinaryHeapCreateCopy(None, 0, heap) 70 self.assert_(isinstance(heap2, CFBinaryHeapRef)) 71 72 def testInspect(self): 73 from Foundation import NSString, NSLog 74 heap = CFBinaryHeapCreate( 75 None, 0) 76 self.assert_(isinstance(heap, CFBinaryHeapRef)) 77 78 79 CFBinaryHeapAddValue(heap, "hello") 80 CFBinaryHeapAddValue(heap, "world") 81 CFBinaryHeapAddValue(heap, "aapjes") 82 83 #ok, min = CFBinaryHeapGetMinimumIfPresent(heap, None) 84 #self.assertTrue(ok) 85 #self.assertEqual(min, "aapjes") 86 #self.fail() 87 88 count = CFBinaryHeapGetCount(heap) 89 self.assertEqual(count, 3) 90 91 count = CFBinaryHeapGetCountOfValue(heap, "hello") 92 self.assertEqual(count, 1) 93 count = CFBinaryHeapGetCountOfValue(heap, "fobar") 94 self.assertEqual(count, 0) 95 96 self.assertTrue(CFBinaryHeapContainsValue(heap, "hello")) 97 self.assertFalse(CFBinaryHeapContainsValue(heap, "foobar")) 98 99 min = CFBinaryHeapGetMinimum(heap) 100 self.assertEqual(min, "aapjes") 101 102 count = CFBinaryHeapGetCount(heap) 103 self.assertEqual(count, 3) 104 105 #ok, min = CFBinaryHeapGetMinimumIfPresent(heap, None) 106 #self.assertTrue(ok) 107 #self.assertEqual(min, "aapjes") 108 #self.fail() 109 110 values = CFBinaryHeapGetValues(heap) 111 self.assertEqual(values, ("aapjes", "hello", "world")) 112 113 CFBinaryHeapRemoveMinimumValue(heap) 114 values = CFBinaryHeapGetValues(heap) 115 self.assertEqual(values, ("hello", "world")) 116 117 CFBinaryHeapRemoveAllValues(heap) 118 values = CFBinaryHeapGetValues(heap) 119 self.assertEqual(values, ()) 120 121 def testFunctions(self): 122 self.assertArgHasType(CFBinaryHeapGetCountOfValue, 1, b'@') 123 self.assertArgHasType(CFBinaryHeapContainsValue, 1, b'@') 124 self.assertResultHasType(CFBinaryHeapGetMinimum, b'@') 125 self.assertResultHasType(CFBinaryHeapGetMinimumIfPresent, objc._C_NSBOOL) 126 self.assertArgHasType(CFBinaryHeapGetMinimumIfPresent, 1, b'o^@') 127 self.assertArgIsFunction(CFBinaryHeapApplyFunction, 1, b'v@@', False) 128 self.assertArgHasType(CFBinaryHeapApplyFunction, 2, b'@') 129 self.assertArgHasType(CFBinaryHeapAddValue, 1, b'@') 130 131if __name__ == "__main__": 132 main() 133