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