Skip to content

Commit

Permalink
class library: fix issues with asPairs, asAssociations, asDict create…
Browse files Browse the repository at this point in the history
…d collection size (#3101)
  • Loading branch information
miguel-negrao authored and telephon committed Aug 11, 2017
1 parent 6ea3780 commit f207182
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
6 changes: 3 additions & 3 deletions SCClassLibrary/Common/Collections/Collection.sc
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ Collection {
^if(class == this.class) { this } { this.as(class) }
};
res = class.new(this.size div: 2);
this.pairsDo { |key, val| res.add(key -> val) }
this.pairsDo { |key, val| res = res.add(key -> val) }
^res
}

Expand All @@ -575,8 +575,8 @@ Collection {
if(this.isAssociationArray.not) {
^if(class == this.class) { this } { this.as(class) }
};
res = class.new(this.size div: 2);
this.do { |assoc| res.add(assoc.key).add(assoc.value) }
res = class.new(this.size * 2);
this.do { |assoc| res = res.add(assoc.key).add(assoc.value) }
^res
}

Expand Down
10 changes: 10 additions & 0 deletions testsuite/classlibrary/TestCollection.sc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
TestCollection : UnitTest {

test_asPairs {

this.assert([2,10,100,1000].collect{ |size| size.collect{ |i| i -> i }.asPairs.size >= (size * 2) }.reduce('&&')
, "asPairs on an array of associations should create an array with size higher or equal to twice the number of elements in the original array");

}

}
12 changes: 12 additions & 0 deletions testsuite/classlibrary/TestDictionary.sc
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,16 @@ TestDictionary : UnitTest {

}

test_asAssociations {

this.assert(
[2,10,100,1000].collect{ |size|
var dict = Dictionary.new(size);
size.do{ |i| dict.put(i,i) };
dict.asAssociations.size >= size
}.reduce('&&')
, "asAssociations on a dictionary should create array of associations with size higher or equal to the number of elements in the original dictionary");

}

}

0 comments on commit f207182

Please sign in to comment.