Explorar o código

added Array.unit.hx (splice and slice still missing)

Simon Krajewski %!s(int64=12) %!d(string=hai) anos
pai
achega
712f119624

+ 102 - 18
std/Array.hx

@@ -27,7 +27,7 @@
 extern class Array<T> {
 
 	/**
-		The length of the Array
+		The length of [this] Array.
 	**/
 	var length(default,null) : Int;
 
@@ -37,32 +37,76 @@ extern class Array<T> {
 	function new() : Void;
 
 	/**
-		Returns a new Array by appending [a] to [this].
+		Returns a new Array by appending the elements of [a] to the elements of
+		[this] Array.
+		
+		This function does not modify [this] Array in place.
+		
+		If [a] is the empty Array [], a copy of [this] Array is returned.
+		
+		The length of the returned Array is equal to the sum of [this].length
+		and [a].length.
+		
+		If [a] is null, the result is unspecified.
 	**/
 	function concat( a : Array<T> ) : Array<T>;
 
 	/**
-		Returns a representation of an array with [sep] for separating each element.
+		Returns a string representation of [this] Array, with [sep] separating
+		each element.
+		
+		The result of this operation is equal to Std.string(this[0]) + sep +
+		Std.string(this[1]) + sep + ... + sep + Std.string(this[this.length-1]).
+		
+		If [this] is the empty Array [], the result is the empty String "". If
+		[this] has exactly one element, the result is equal to a call to
+		Std.string(this[0]).
+		
+		If [a] is null, the result is unspecified.
 	**/
 	function join( sep : String ) : String;
 
 	/**
-		Removes the last element of the array and returns it.
+		Removes the last element of [this] Array and returns it.
+		
+		This operations modifies [this] Array in place.
+		
+		If [this] has at least one element, [this].length will decrease by 1.
+		
+		If [this] is the empty Array [], null is returned and the length remains
+		0.
 	**/
 	function pop() : Null<T>;
 
 	/**
-		Adds the element [x] at the end of the array.
+		Adds the element [x] at the end of [this] Array and returns the offset
+		it was added at.
+		
+		This operations modifies [this] Array in place.
+		
+		[this].length will increase by 1.
 	**/
 	function push(x : T) : Int;
 
 	/**
-		Reverse the order of elements of the Array.
+		Reverse the order of elements of [this] Array.
+		
+		This operations modifies [this] Array in place.
+		
+		If [this].length < 2, [this] remains unchanged.
 	**/
 	function reverse() : Void;
 
 	/**
-		Removes the first element and returns it.
+		Removes the first element of [this] Array and returns it.
+		
+		This operations modifies [this] Array in place.
+		
+		If [this] has at least one element, [this].length and the index of each
+		remaining element is decreased by 1.
+		
+		If [this] is the empty Array [], null is returned and the length remains
+		0.
 	**/
 	function shift() : Null<T>;
 
@@ -75,9 +119,15 @@ extern class Array<T> {
 	function slice( pos : Int, ?end : Int ) : Array<T>;
 
 	/**
-		Sort the Array according to the comparison function [f].
-		[f(x,y)] should return [0] if [x == y], [>0] if [x > y]
-		and [<0] if [x < y].
+		Sorts [this] Array according to the comparison function [f], where
+		[f(x,y)] returns 0 if x == y, a positive Int if x > y and a
+		negative Int if x < y.
+		
+		This operations modifies [this] Array in place.
+		
+		The sort operation is robust: Equal elements will retain their order.
+		
+		If [f] is null, the result is unspecified.
 	**/
 	function sort( f : T -> T -> Int ) : Void;
 
@@ -87,31 +137,65 @@ extern class Array<T> {
 	function splice( pos : Int, len : Int ) : Array<T>;
 
 	/**
-		Returns a displayable representation of the Array content.
+		Returns a string representation of [this] Array.
+		
+		The result will include the individual elements' String representations
+		separated by comma. The enclosing [ ] may be missing on some platforms,
+		use Std.string() to get a String representation that is consistent
+		across platforms.
 	**/
 	function toString() : String;
 
 	/**
-		Adds the element [x] at the start of the array.
+		Adds the element [x] at the start of [this] Array.
+		
+		This operations modifies [this] Array in place.
+		
+		[this].length and the index of each Array element increases by 1.
 	**/
 	function unshift( x : T ) : Void;
 
 	/**
 		Inserts the element [x] at the position [pos].
-		All elements after [pos] are moved one index ahead.
+		
+		This operations modifies [this] Array in place.
+		
+		The offset is calculated like so:
+			
+		- If [pos] exceeds [this].length, [this] Array is padded with the
+		default value until [this].length equals [pos]. The offset is then
+		[pos].
+		- If [pos] is negative, the offset is calculated from the end of [this]
+		Array, i.e. [this].length + [pos]. If this yields a negative value,
+		the offset is 0.
+		- Otherwise, the offset is [pos].
+		
+		If the resulting offset does not exceed [this].length, all elements from
+		and including that offset to the end of [this] Array are moved one index
+		ahead.
 	**/
 	function insert( pos : Int, x : T ) : Void;
 
 	/**
-		Removes the first occurence of [x].
-		Returns false if [x] was not present.
-		Elements are compared by using standard equality.
+		Removes the first occurence of [x] in [this] Array.
+		
+		This operations modifies [this] Array in place.
+		
+		If [x] is found by checking standard equality, it is removed from [this]
+		Array and all following elements are reindexed acoordingly. The function
+		then returns true.
+		
+		If [x] is not found, [this] Array is not changed and the function
+		returns false.
 	**/
 	function remove( x : T ) : Bool;
 
 	/**
-		Returns a copy of the Array. The values are not
-		copied, only the Array structure.
+		Returns a shallow copy of [this] Array.
+		
+		The elements are not copied and retain their identity, so
+		a[i] == a.copy()[i] is true for any valid i. However, a == a.copy() is
+		always false.
 	**/
 	function copy() : Array<T>;
 

+ 14 - 0
tests/unit/TestSpecification.hx

@@ -22,6 +22,20 @@ private enum SomeEnum<T> {
 	OneArgument(t:T);
 }
 
+private class IntWrap {
+	public var i(default, null):Int;
+	
+	public function new(i:Int) {
+		this.i == i;
+	}
+	
+	static public function compare(a:IntWrap, b:IntWrap) {
+		return if (a.i == b.i) 0;
+			else if (a.i > b.i) 1;
+			else -1;
+	}
+}
+
 #if !macro
 @:build(unit.UnitBuilder.build("unitstd"))
 #end

+ 3 - 3
tests/unit/UnitBuilder.hx

@@ -12,7 +12,7 @@ class UnitBuilder {
 		var ret = Context.getBuildFields();
 		var numFiles = 0;
 		for (file in dir) {
-			if (file.endsWith(".hxunit")) {
+			if (file.endsWith(".unit.hx")) {
 				numFiles++;
 				var func = {
 					args: [],
@@ -21,7 +21,7 @@ class UnitBuilder {
 					expr: read(basePath + file)
 				}
 				ret.push( {
-					name: "test" + ~/\./.map(file, function(_) return "_"),
+					name: "test" + ~/\./g.map(file, function(_) return "_"),
 					kind: FFun(func),
 					pos: Context.makePosition( { min:0, max:0, file:basePath + file } ),
 					access: [APublic],
@@ -30,7 +30,7 @@ class UnitBuilder {
 				});
 			}
 		}
-		trace("Added " +numFiles + " .hxunit files");
+		trace("Added " +numFiles + " .unit.hx files");
 		return ret;
 	}
 	

+ 169 - 0
tests/unit/unitstd/Array.unit.hx

@@ -0,0 +1,169 @@
+// length
+[].length == 0;
+[1].length == 1;
+var a = [];
+a[4] = 1;
+a.length == 5;
+
+// concat
+[].concat([]) == [];
+[1].concat([]) == [1];
+[].concat([1]) == [1];
+[1].concat([2]) == [1,2];
+[1,2].concat([2,1]) == [1,2,2,1];
+
+// join
+[1,2].join("") == "12";
+[].join("x") == "";
+[1].join("x") == "1";
+[1,2].join("x") == "1x2";
+[].join("") == "";
+[new ClassWithToString(), new ClassWithToStringChild(), new ClassWithToStringChild2()].join("_") == "ClassWithToString.toString()_ClassWithToString.toString()_ClassWithToStringChild2.toString()";
+
+// pop
+[].pop() == null;
+[1].pop() == 1;
+var a = [1, 2, 3];
+var b = a;
+a.pop() == 3;
+a == [1, 2];
+a == b;
+a.pop() == 2;
+a == [1];
+a == b;
+a.pop() == 1;
+a == [];
+a == b;
+a.pop() == null;
+a == [];
+a == b;
+
+// push
+var a:Array<Null<Int>> = [];
+var b = a;
+a.push(1) == 1;
+a == b;
+a == [1];
+a.push(2) == 2;
+a == b;
+a == [1, 2];
+a.push(null) == 3;
+a == [1, 2, null];
+
+// reverse
+var a = [1, 2, 3];
+var b = a;
+a.reverse();
+a == b;
+a == [3, 2, 1];
+var a = [];
+a.reverse();
+a == [];
+var a = [1];
+a.reverse();
+a == [1];
+
+// shift
+[].shift() == null;
+[1].shift() == 1;
+var a = [1, 2, 3];
+var b = a;
+a.shift() == 1;
+a == [2, 3];
+a == b;
+a.shift() == 2;
+a == [3];
+a == b;
+a.shift() == 3;
+a == [];
+a == b;
+a.shift() == null;
+a == [];
+a == b;
+
+// slice
+// TODO
+
+// sort
+var i0 = new IntWrap(1);
+var i1 = new IntWrap(1);
+var i2 = new IntWrap(5);
+var i3 = new IntWrap(9);
+var i4 = new IntWrap(2);
+var a = [i4,i0,i1,i3,i0,i2];
+a.sort(IntWrap.compare);
+a == [i0, i1, i0, i4, i2, i3];
+
+// splice
+// TODO
+
+// toString
+var a = [new ClassWithToString(), new ClassWithToStringChild(), new ClassWithToStringChild2()];
+var comp = "ClassWithToString.toString(),ClassWithToString.toString(),ClassWithToStringChild2.toString()";
+a.toString() in [comp, "[" + comp + "]"];
+
+// unshift
+var a:Array<Null<Int>> = [];
+var b = a;
+a.unshift(1);
+a == b;
+a == [1];
+a.unshift(2);
+a == b;
+a == [2, 1];
+a.unshift(null);
+a == [null, 2, 1];
+
+// insert
+var a = [];
+a.insert(5, 1);
+a == [0, 0, 0, 0, 0, 1];
+var a = [1, 2, 3];
+a.insert(1, 4);
+a == [1, 4, 2, 3];
+var a = [1, 2, 3];
+a.insert( -1, 4);
+a == [1, 2, 4, 3];
+a.insert( -2, 8);
+a == [1, 2, 8, 4, 3];
+a.insert ( -8, 9);
+a == [9, 1, 2, 8, 4, 3];
+
+// remove
+var i0 = new IntWrap(1);
+var i1 = new IntWrap(1);
+var i2 = new IntWrap(5);
+var i3 = new IntWrap(9);
+var i4 = new IntWrap(2);
+var a = [i4, i0, i1, i3, i0, i2];
+a.remove(i0) == true;
+a == [i4, i1, i3, i0, i2];
+a.remove(i0) == true;
+a == [i4, i1, i3, i2];
+a.remove(i0) == false;
+a == [i4, i1, i3, i2];
+var a = ["foo", "bar"];
+a.remove("foo") == true;
+a == ["bar"];
+var a = [i0, null, i1, null, null];
+a.remove(null) == true;
+a == [i0, i1, null, null];
+a.remove(null) == true;
+a == [i0, i1, null];
+a.remove(null) == true;
+a == [i0, i1];
+a.remove(null) == false;
+a == [i0, i1];
+
+// copy
+var i0 = new IntWrap(1);
+var i1 = new IntWrap(1);
+var i2 = new IntWrap(5);
+var a = [i0, i1, i2];
+var b = a.copy();
+a != b;
+b == [i0, i1, i2];
+var a = [];
+var b = a.copy();
+a != b;
+b == [];

+ 0 - 0
tests/unit/unitstd/Std.hxunit → tests/unit/unitstd/Std.unit.hx