Ver Fonte

Add test for Reflect.compare and BalancedTree with string keys.

Georg Koester há 10 anos atrás
pai
commit
3fc1d18b6f

+ 15 - 1
tests/unit/src/unit/TestReflect.hx

@@ -234,7 +234,7 @@ class TestReflect extends Test {
 		exc( function() Type.createEnum(MyEnum,"Z",[]) );
 	}
 
-	function testCompare() {
+	function testCompareMethods() {
 		var a = new MyClass(0);
 		var b = new MyClass(1);
 		t( Reflect.compareMethods(a.add,a.add) );
@@ -257,6 +257,20 @@ class TestReflect extends Test {
 		*/
 	}
 
+	function testCompare() {
+		eq( Reflect.compare(1,1), 0 );
+		t( Reflect.compare(0,1) < 0 );
+		t( Reflect.compare(1,0) > 0 );
+
+		eq( Reflect.compare(1.0,1.0), 0 );
+		t( Reflect.compare(0.0,1.0) < 0 );
+		t( Reflect.compare(1.0,0.0) > 0 );
+
+		t( Reflect.compare("","a") < 0 );
+		t( Reflect.compare("a","") > 0 );
+		eq( Reflect.compare("aasdf","aaasdf".substr(1, 5)), 0 ); // ensure new object is allocated for comparison
+	}
+
 	function testGetProp() {
 
 		var c = new ClassWithProp();

+ 33 - 3
tests/unit/src/unitstd/haxe/ds/BalancedTree.unit.hx

@@ -1,3 +1,10 @@
+var arrEq = function(arrA:Array<Dynamic>, arrB:Array<Dynamic>) {
+	eq(arrA.length, arrB.length);
+	for (i in 0...arrA.length) {
+		eq(arrA[i], arrB[i]);
+	}
+};
+
 var test = [
 	13 => 1,
 	8 => 2,
@@ -25,10 +32,33 @@ for (k in otherKeys) {
 	eq(false, m.exists(k));
 }
 var r = [for (key in m.keys()) key];
-r == [1,6,8,11,13,15,17,22,25,27];
+arrEq(r, [1,6,8,11,13,15,17,22,25,27]);
 var r = [for (val in m) val];
-r == [4,8,2,5,1,6,3,9,7,10];
+arrEq(r, [4,8,2,5,1,6,3,9,7,10]);
 for (k in test.keys()) {
 	eq(true, m.remove(k));
 	eq(false, m.exists(k));
-}
+}
+
+var ms = new haxe.ds.BalancedTree<String, Int>();
+for (k in test.keys()) {
+	ms.set(Std.string(k), test[k]);
+}
+for (k in test.keys()) {
+	eq(ms.get(Std.string(k)), test[k]);
+}
+for (k in test.keys()) {
+	eq(ms.exists(Std.string(k)), true);
+}
+for (k in otherKeys) {
+	eq(ms.exists(Std.string(k)), false);
+}
+var r2 = [for (key in ms.keys()) key];
+arrEq(r2, [for (k in [1,11,13,15,17,22,25,27,6,8]) Std.string(k)]);
+
+var r = [for (val in ms) val];
+arrEq(r, [4,5,1,6,3,9,7,10,8,2]);
+for (k in test.keys()) {
+	eq(ms.remove(Std.string(k)), true);
+	eq(ms.exists(Std.string(k)), false);
+}