Commits

Enrico Franchi  committed d87793b

Added test proving that non-fibonacci tree can't be merged.

  • Participants
  • Parent commits 6fe7ce1

Comments (0)

Files changed (2)

File src/fibomodule.c

 static PyObject*
 Fibonacci_Tree_Repr(PyObject* tree)
 {
-	PyObject *aslist, *result, *fmt;
-	int i;
+    PyObject *aslist, *result, *fmt;
+    int i;
 
-	i = Py_ReprEnter(tree);
-	if (i != 0) {
-		if (i < 0)
-			return NULL;
-		return PyString_FromString("[...]");
-	}
+    i = Py_ReprEnter(tree);
+    if (i != 0) {
+        if (i < 0)
+            return NULL;
+        return PyString_FromString("[...]");
+    }
 
-	aslist = PySequence_List(tree);
-	if (aslist == NULL) {
-		Py_ReprLeave(tree);
-		return NULL;
-	}
+    aslist = PySequence_List(tree);
+    if (aslist == NULL) {
+        Py_ReprLeave(tree);
+        return NULL;
+    }
 
-	fmt = PyString_FromString("FibonacciHeap(%r)");
-	if (fmt == NULL) {
-		Py_DECREF(aslist);
-		Py_ReprLeave(tree);
-		return NULL;
-	}
-	result = PyString_Format(fmt, aslist);
-	Py_DECREF(fmt);
-	Py_DECREF(aslist);
-	Py_ReprLeave(tree);
-	return result;
+    fmt = PyString_FromString("FibonacciHeap(%r)");
+    if (fmt == NULL) {
+        Py_DECREF(aslist);
+        Py_ReprLeave(tree);
+        return NULL;
+    }
+    result = PyString_Format(fmt, aslist);
+    Py_DECREF(fmt);
+    Py_DECREF(aslist);
+    Py_ReprLeave(tree);
+    return result;
 }
 
 static int
 
 static PyObject*
 Fibonacci_Tree_Merge(Fibonacci_Tree* tree, PyObject* other) {
-    Fibonacci_Tree* copy =
-        (Fibonacci_Tree*)Fibonacci_Tree_Copy((Fibonacci_Tree*)other);
-    if(copy != NULL) {
-        Fibonacci_Tree_Destructive_Merge(tree, copy);
-        Py_DECREF(copy);
-        Py_RETURN_NONE;
+    if(PyObject_TypeCheck(other, &Fibonacci_Tree_Type)) {
+        Fibonacci_Tree* copy =
+            (Fibonacci_Tree*)Fibonacci_Tree_Copy((Fibonacci_Tree*)other);
+        if(copy != NULL) {
+            Fibonacci_Tree_Destructive_Merge(tree, copy);
+            Py_DECREF(copy);
+            Py_RETURN_NONE;
+        } else {
+            return 0;
+        }
     } else {
+        PyErr_SetString(PyExc_TypeError,
+                "merge called with non fibonacci tree type.");
         return 0;
     }
 }

File tests/test_merge.py

         self.second_contents = range(7, 10)
         self.second.extend(self.second_contents)
 
-
     def testMergeOne(self):
         self.first.merge(self.second)
         self.assertSetEqual(
             set(self.second),
             set(self.first_contents + self.second_contents))
 
+    def testMergeNotTree(self):
+        self.assertRaises(TypeError, self.first.merge, range(10))
+