Browse Source

base testAtPut for all IndexableCollections

Herbert Vojčík 11 years ago
parent
commit
67d2baa7a0
2 changed files with 267 additions and 4 deletions
  1. 203 3
      js/Kernel-Tests.js
  2. 64 1
      st/Kernel-Tests.st

+ 203 - 3
js/Kernel-Tests.js

@@ -1846,6 +1846,54 @@ smalltalk.CollectionTest.klass);
 
 
 smalltalk.addClass('IndexableCollectionTest', smalltalk.CollectionTest, [], 'Kernel-Tests');
+smalltalk.addMethod(
+smalltalk.method({
+selector: "collectionWithNewValue",
+protocol: 'fixture',
+fn: function (){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
+self._subclassResponsibility();
+return self}, function($ctx1) {$ctx1.fill(self,"collectionWithNewValue",{},smalltalk.IndexableCollectionTest)})},
+args: [],
+source: "collectionWithNewValue\x0a\x09\x22Answers a collection which shows how\x0a\x09self collection would look after adding\x0a\x09self sampleNewValue at self sampleNewIndex\x22\x0a\x09\x0a\x09self subclassResponsibility",
+messageSends: ["subclassResponsibility"],
+referencedClasses: []
+}),
+smalltalk.IndexableCollectionTest);
+
+smalltalk.addMethod(
+smalltalk.method({
+selector: "sampleNewIndex",
+protocol: 'fixture',
+fn: function (){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
+self._subclassResponsibility();
+return self}, function($ctx1) {$ctx1.fill(self,"sampleNewIndex",{},smalltalk.IndexableCollectionTest)})},
+args: [],
+source: "sampleNewIndex\x0a\x09\x22Answers a value that can be used as index in at:put: or at:ifAbsentPut:\x22\x0a\x09\x0a\x09self subclassResponsibility",
+messageSends: ["subclassResponsibility"],
+referencedClasses: []
+}),
+smalltalk.IndexableCollectionTest);
+
+smalltalk.addMethod(
+smalltalk.method({
+selector: "sampleNewValue",
+protocol: 'fixture',
+fn: function (){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
+return "N";
+}, function($ctx1) {$ctx1.fill(self,"sampleNewValue",{},smalltalk.IndexableCollectionTest)})},
+args: [],
+source: "sampleNewValue\x0a\x09\x22Answers a value that is not yet there\x0a\x09and can be put into a tested collection\x22\x0a\x09\x0a\x09^ 'N'",
+messageSends: [],
+referencedClasses: []
+}),
+smalltalk.IndexableCollectionTest);
+
 smalltalk.addMethod(
 smalltalk.method({
 selector: "sampleNonIndexesDo:",
@@ -2060,6 +2108,33 @@ referencedClasses: ["Object"]
 }),
 smalltalk.IndexableCollectionTest);
 
+smalltalk.addMethod(
+smalltalk.method({
+selector: "testAtPut",
+protocol: 'tests',
+fn: function (){
+var self=this;
+var newCollection;
+return smalltalk.withContext(function($ctx1) { 
+newCollection=self._collection();
+$ctx1.sendIdx["collection"]=1;
+self._samplesDo_((function(index,value){
+return smalltalk.withContext(function($ctx2) {
+return _st(newCollection)._at_put_(index,value);
+$ctx2.sendIdx["at:put:"]=1;
+}, function($ctx2) {$ctx2.fillBlock({index:index,value:value},$ctx1,1)})}));
+self._assert_equals_(newCollection,self._collection());
+$ctx1.sendIdx["assert:equals:"]=1;
+_st(newCollection)._at_put_(self._sampleNewIndex(),self._sampleNewValue());
+self._assert_equals_(newCollection,self._collectionWithNewValue());
+return self}, function($ctx1) {$ctx1.fill(self,"testAtPut",{newCollection:newCollection},smalltalk.IndexableCollectionTest)})},
+args: [],
+source: "testAtPut\x0a\x09| newCollection |\x0a\x09newCollection := self collection.\x0a\x09self samplesDo: [ :index :value |\x0a\x09\x09newCollection at: index put: value ].\x0a\x09self assert: newCollection equals: self collection.\x0a\x09newCollection at: self sampleNewIndex put: self sampleNewValue.\x0a\x09self assert: newCollection equals: self collectionWithNewValue",
+messageSends: ["collection", "samplesDo:", "at:put:", "assert:equals:", "sampleNewIndex", "sampleNewValue", "collectionWithNewValue"],
+referencedClasses: []
+}),
+smalltalk.IndexableCollectionTest);
+
 smalltalk.addMethod(
 smalltalk.method({
 selector: "testIndexOf",
@@ -2167,6 +2242,32 @@ referencedClasses: []
 }),
 smalltalk.HashedCollectionTest);
 
+smalltalk.addMethod(
+smalltalk.method({
+selector: "collectionWithNewValue",
+protocol: 'fixture',
+fn: function (){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
+var $2,$3,$4,$5,$1;
+$2="b".__minus_gt((1));
+$ctx1.sendIdx["->"]=1;
+$3="a".__minus_gt((2));
+$ctx1.sendIdx["->"]=2;
+$4="c".__minus_gt((3));
+$ctx1.sendIdx["->"]=3;
+$5="d".__minus_gt((-4));
+$ctx1.sendIdx["->"]=4;
+$1=smalltalk.HashedCollection._from_([$2,$3,$4,$5,"new".__minus_gt("N")]);
+return $1;
+}, function($ctx1) {$ctx1.fill(self,"collectionWithNewValue",{},smalltalk.HashedCollectionTest)})},
+args: [],
+source: "collectionWithNewValue\x0a\x09^ #{ 'b' -> 1. 'a' -> 2. 'c' -> 3. 'd' -> -4. 'new' -> 'N' }",
+messageSends: ["->"],
+referencedClasses: []
+}),
+smalltalk.HashedCollectionTest);
+
 smalltalk.addMethod(
 smalltalk.method({
 selector: "nonIndexesDo:",
@@ -2185,6 +2286,22 @@ referencedClasses: []
 }),
 smalltalk.HashedCollectionTest);
 
+smalltalk.addMethod(
+smalltalk.method({
+selector: "sampleNewIndex",
+protocol: 'fixture',
+fn: function (){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
+return "new";
+}, function($ctx1) {$ctx1.fill(self,"sampleNewIndex",{},smalltalk.HashedCollectionTest)})},
+args: [],
+source: "sampleNewIndex\x0a\x09^ 'new'",
+messageSends: [],
+referencedClasses: []
+}),
+smalltalk.HashedCollectionTest);
+
 smalltalk.addMethod(
 smalltalk.method({
 selector: "samplesDo:",
@@ -2304,14 +2421,14 @@ _st($2)._at_put_("a",(2));
 $ctx1.sendIdx["at:put:"]=2;
 _st($2)._at_put_(true,(3));
 $ctx1.sendIdx["at:put:"]=3;
-_st($2)._at_put_((4),(-4));
+_st($2)._at_put_((1).__at((3)),(-4));
 $3=_st($2)._yourself();
 $1=$3;
 return $1;
 }, function($ctx1) {$ctx1.fill(self,"collection",{},smalltalk.DictionaryTest)})},
 args: [],
-source: "collection\x0a\x09^ Dictionary new\x0a\x09\x09at: 1 put: 1;\x0a\x09\x09at: 'a' put: 2;\x0a\x09\x09at: true put: 3;\x0a\x09\x09at: 4 put: -4;\x0a\x09\x09yourself",
-messageSends: ["at:put:", "new", "yourself"],
+source: "collection\x0a\x09^ Dictionary new\x0a\x09\x09at: 1 put: 1;\x0a\x09\x09at: 'a' put: 2;\x0a\x09\x09at: true put: 3;\x0a\x09\x09at: 1@3 put: -4;\x0a\x09\x09yourself",
+messageSends: ["at:put:", "new", "@", "yourself"],
 referencedClasses: ["Dictionary"]
 }),
 smalltalk.DictionaryTest);
@@ -2350,6 +2467,55 @@ referencedClasses: ["Dictionary"]
 }),
 smalltalk.DictionaryTest);
 
+smalltalk.addMethod(
+smalltalk.method({
+selector: "collectionWithNewValue",
+protocol: 'fixture',
+fn: function (){
+var self=this;
+function $Dictionary(){return smalltalk.Dictionary||(typeof Dictionary=="undefined"?nil:Dictionary)}
+return smalltalk.withContext(function($ctx1) { 
+var $2,$3,$1;
+$2=_st($Dictionary())._new();
+_st($2)._at_put_((1),(1));
+$ctx1.sendIdx["at:put:"]=1;
+_st($2)._at_put_("a",(2));
+$ctx1.sendIdx["at:put:"]=2;
+_st($2)._at_put_(true,(3));
+$ctx1.sendIdx["at:put:"]=3;
+_st($2)._at_put_((1).__at((3)),(-4));
+$ctx1.sendIdx["at:put:"]=4;
+_st($2)._at_put_("new","N");
+$3=_st($2)._yourself();
+$1=$3;
+return $1;
+}, function($ctx1) {$ctx1.fill(self,"collectionWithNewValue",{},smalltalk.DictionaryTest)})},
+args: [],
+source: "collectionWithNewValue\x0a\x09^ Dictionary new\x0a\x09\x09at: 1 put: 1;\x0a\x09\x09at: 'a' put: 2;\x0a\x09\x09at: true put: 3;\x0a\x09\x09at: 1@3 put: -4;\x0a\x09\x09at: 'new' put: 'N';\x0a\x09\x09yourself",
+messageSends: ["at:put:", "new", "@", "yourself"],
+referencedClasses: ["Dictionary"]
+}),
+smalltalk.DictionaryTest);
+
+smalltalk.addMethod(
+smalltalk.method({
+selector: "samplesDo:",
+protocol: 'fixture',
+fn: function (aBlock){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
+smalltalk.DictionaryTest.superclass.fn.prototype._samplesDo_.apply(_st(self), [aBlock]);
+_st(aBlock)._value_value_(true,(3));
+$ctx1.sendIdx["value:value:"]=1;
+_st(aBlock)._value_value_((1).__at((3)),(-4));
+return self}, function($ctx1) {$ctx1.fill(self,"samplesDo:",{aBlock:aBlock},smalltalk.DictionaryTest)})},
+args: ["aBlock"],
+source: "samplesDo: aBlock\x0a\x09super samplesDo: aBlock.\x0a\x09aBlock value: true value: 3.\x0a\x09aBlock value: 1@3 value: -4",
+messageSends: ["samplesDo:", "value:value:", "@"],
+referencedClasses: []
+}),
+smalltalk.DictionaryTest);
+
 smalltalk.addMethod(
 smalltalk.method({
 selector: "testAccessing",
@@ -3238,6 +3404,40 @@ referencedClasses: []
 }),
 smalltalk.ArrayTest);
 
+smalltalk.addMethod(
+smalltalk.method({
+selector: "collectionWithNewValue",
+protocol: 'fixture',
+fn: function (){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
+var $1;
+$1=[(1), (2), (3), (-4), "N"];
+return $1;
+}, function($ctx1) {$ctx1.fill(self,"collectionWithNewValue",{},smalltalk.ArrayTest)})},
+args: [],
+source: "collectionWithNewValue\x0a\x09^ #(1 2 3 -4 'N')",
+messageSends: [],
+referencedClasses: []
+}),
+smalltalk.ArrayTest);
+
+smalltalk.addMethod(
+smalltalk.method({
+selector: "sampleNewIndex",
+protocol: 'fixture',
+fn: function (){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
+return (5);
+}, function($ctx1) {$ctx1.fill(self,"sampleNewIndex",{},smalltalk.ArrayTest)})},
+args: [],
+source: "sampleNewIndex\x0a\x09^ 5",
+messageSends: [],
+referencedClasses: []
+}),
+smalltalk.ArrayTest);
+
 smalltalk.addMethod(
 smalltalk.method({
 selector: "samplesDo:",

+ 64 - 1
st/Kernel-Tests.st

@@ -564,6 +564,27 @@ CollectionTest subclass: #IndexableCollectionTest
 
 !IndexableCollectionTest methodsFor: 'fixture'!
 
+collectionWithNewValue
+	"Answers a collection which shows how
+	self collection would look after adding
+	self sampleNewValue at self sampleNewIndex"
+	
+	self subclassResponsibility
+!
+
+sampleNewIndex
+	"Answers a value that can be used as index in at:put: or at:ifAbsentPut:"
+	
+	self subclassResponsibility
+!
+
+sampleNewValue
+	"Answers a value that is not yet there
+	and can be put into a tested collection"
+	
+	^ 'N'
+!
+
 sampleNonIndexesDo: aBlock
 	"Executes block a few times,
 	each time passing value that is known
@@ -622,6 +643,16 @@ testAtIfPresentIfAbsent
 		self assert: visited equals: (self collection at: index) ]
 !
 
+testAtPut
+	| newCollection |
+	newCollection := self collection.
+	self samplesDo: [ :index :value |
+		newCollection at: index put: value ].
+	self assert: newCollection equals: self collection.
+	newCollection at: self sampleNewIndex put: self sampleNewValue.
+	self assert: newCollection equals: self collectionWithNewValue
+!
+
 testIndexOf
 	self should: [ self collection indexOf: self sampleNonValue ] raise: Error.
 	self samplesDo: [ :index :value |
@@ -650,11 +681,19 @@ collectionWithDuplicates
 	^ #{ 'b' -> 1. 'a' -> 2. 'c' -> 3. 'd' -> -4. 'e' -> 1. 'f' -> 2. 'g' -> 10 }
 !
 
+collectionWithNewValue
+	^ #{ 'b' -> 1. 'a' -> 2. 'c' -> 3. 'd' -> -4. 'new' -> 'N' }
+!
+
 nonIndexesDo: aBlock
 	aBlock value: 5.
 	aBlock value: 'z'
 !
 
+sampleNewIndex
+	^ 'new'
+!
+
 samplesDo: aBlock
 	aBlock value: 'a' value: 2
 ! !
@@ -696,7 +735,7 @@ collection
 		at: 1 put: 1;
 		at: 'a' put: 2;
 		at: true put: 3;
-		at: 4 put: -4;
+		at: 1@3 put: -4;
 		yourself
 !
 
@@ -710,6 +749,22 @@ collectionWithDuplicates
 		at: 3 put: 3;
 		at: false put: 12;
 		yourself
+!
+
+collectionWithNewValue
+	^ Dictionary new
+		at: 1 put: 1;
+		at: 'a' put: 2;
+		at: true put: 3;
+		at: 1@3 put: -4;
+		at: 'new' put: 'N';
+		yourself
+!
+
+samplesDo: aBlock
+	super samplesDo: aBlock.
+	aBlock value: true value: 3.
+	aBlock value: 1@3 value: -4
 ! !
 
 !DictionaryTest methodsFor: 'tests'!
@@ -995,6 +1050,14 @@ collectionWithDuplicates
 	^ #('a' 'b' 'c' 1 2 1 'a')
 !
 
+collectionWithNewValue
+	^ #(1 2 3 -4 'N')
+!
+
+sampleNewIndex
+	^ 5
+!
+
 samplesDo: aBlock
 	super samplesDo: aBlock.
 	aBlock value: 3 value: 3.