Browse Source

Merge pull request #785 from herby/at-ifabsentput-tests

at:ifAbsentPut: tests, additional tests for Set
Nicolas Petton 11 years ago
parent
commit
14e0c540fa
5 changed files with 210 additions and 26 deletions
  1. 1 0
      API-CHANGES.txt
  2. 21 21
      js/Kernel-Collections.js
  3. 154 0
      js/Kernel-Tests.js
  4. 5 5
      st/Kernel-Collections.st
  5. 29 0
      st/Kernel-Tests.st

+ 1 - 0
API-CHANGES.txt

@@ -2,6 +2,7 @@
 
 
 * Package Import-Export renamed to Kernel-ImportExport
 * Package Import-Export renamed to Kernel-ImportExport
 * A dozen of methods moved from Object to ProtoObject
 * A dozen of methods moved from Object to ProtoObject
+* HashedCollection >> at:ifAbsentPut: pushed up to SequenceableCollection
 
 
 + CompiledMethod >>
 + CompiledMethod >>
   + defaultProtocol
   + defaultProtocol

+ 21 - 21
js/Kernel-Collections.js

@@ -1153,6 +1153,27 @@ referencedClasses: []
 }),
 }),
 smalltalk.IndexableCollection);
 smalltalk.IndexableCollection);
 
 
+smalltalk.addMethod(
+smalltalk.method({
+selector: "at:ifAbsentPut:",
+protocol: 'accessing',
+fn: function (aKey,aBlock){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
+var $1;
+$1=self._at_ifAbsent_(aKey,(function(){
+return smalltalk.withContext(function($ctx2) {
+return self._at_put_(aKey,_st(aBlock)._value());
+}, function($ctx2) {$ctx2.fillBlock({},$ctx1,1)})}));
+return $1;
+}, function($ctx1) {$ctx1.fill(self,"at:ifAbsentPut:",{aKey:aKey,aBlock:aBlock},smalltalk.IndexableCollection)})},
+args: ["aKey", "aBlock"],
+source: "at: aKey ifAbsentPut: aBlock\x0a\x09^ self at: aKey ifAbsent: [\x0a\x09\x09self at: aKey put: aBlock value ]",
+messageSends: ["at:ifAbsent:", "at:put:", "value"],
+referencedClasses: []
+}),
+smalltalk.IndexableCollection);
+
 smalltalk.addMethod(
 smalltalk.addMethod(
 smalltalk.method({
 smalltalk.method({
 selector: "at:ifPresent:",
 selector: "at:ifPresent:",
@@ -1474,27 +1495,6 @@ referencedClasses: []
 }),
 }),
 smalltalk.HashedCollection);
 smalltalk.HashedCollection);
 
 
-smalltalk.addMethod(
-smalltalk.method({
-selector: "at:ifAbsentPut:",
-protocol: 'accessing',
-fn: function (aKey,aBlock){
-var self=this;
-return smalltalk.withContext(function($ctx1) { 
-var $1;
-$1=self._at_ifAbsent_(aKey,(function(){
-return smalltalk.withContext(function($ctx2) {
-return self._at_put_(aKey,_st(aBlock)._value());
-}, function($ctx2) {$ctx2.fillBlock({},$ctx1,1)})}));
-return $1;
-}, function($ctx1) {$ctx1.fill(self,"at:ifAbsentPut:",{aKey:aKey,aBlock:aBlock},smalltalk.HashedCollection)})},
-args: ["aKey", "aBlock"],
-source: "at: aKey ifAbsentPut: aBlock\x0a\x09^ self at: aKey ifAbsent: [\x0a\x09\x09self at: aKey put: aBlock value ]",
-messageSends: ["at:ifAbsent:", "at:put:", "value"],
-referencedClasses: []
-}),
-smalltalk.HashedCollection);
-
 smalltalk.addMethod(
 smalltalk.addMethod(
 smalltalk.method({
 smalltalk.method({
 selector: "at:ifPresent:ifAbsent:",
 selector: "at:ifPresent:ifAbsent:",

+ 154 - 0
js/Kernel-Tests.js

@@ -2407,6 +2407,45 @@ referencedClasses: []
 }),
 }),
 smalltalk.IndexableCollectionTest);
 smalltalk.IndexableCollectionTest);
 
 
+smalltalk.addMethod(
+smalltalk.method({
+selector: "testAtIfAbsentPut",
+protocol: 'tests',
+fn: function (){
+var self=this;
+var newCollection;
+return smalltalk.withContext(function($ctx1) { 
+var $1;
+newCollection=self._collection();
+$ctx1.sendIdx["collection"]=1;
+self._samplesDo_((function(index,value){
+return smalltalk.withContext(function($ctx2) {
+$1=_st(newCollection)._at_ifAbsentPut_(index,(function(){
+return smalltalk.withContext(function($ctx3) {
+return self._sampleNewValue();
+$ctx3.sendIdx["sampleNewValue"]=1;
+}, function($ctx3) {$ctx3.fillBlock({},$ctx2,2)})}));
+$ctx2.sendIdx["at:ifAbsentPut:"]=1;
+return self._assert_equals_($1,value);
+$ctx2.sendIdx["assert:equals:"]=1;
+}, function($ctx2) {$ctx2.fillBlock({index:index,value:value},$ctx1,1)})}));
+self._assert_equals_(newCollection,self._collection());
+$ctx1.sendIdx["assert:equals:"]=2;
+self._assert_equals_(_st(newCollection)._at_ifAbsentPut_(self._sampleNewIndex(),(function(){
+return smalltalk.withContext(function($ctx2) {
+return self._sampleNewValue();
+$ctx2.sendIdx["sampleNewValue"]=2;
+}, function($ctx2) {$ctx2.fillBlock({},$ctx1,3)})})),self._sampleNewValue());
+$ctx1.sendIdx["assert:equals:"]=3;
+self._assert_equals_(newCollection,self._collectionWithNewValue());
+return self}, function($ctx1) {$ctx1.fill(self,"testAtIfAbsentPut",{newCollection:newCollection},smalltalk.IndexableCollectionTest)})},
+args: [],
+source: "testAtIfAbsentPut\x0a\x09| newCollection |\x0a\x09newCollection := self collection.\x0a\x09self samplesDo: [ :index :value |\x0a\x09\x09self assert: (newCollection at: index ifAbsentPut: [ self sampleNewValue ]) equals: value ].\x0a\x09self assert: newCollection equals: self collection.\x0a\x09self assert: (newCollection at: self sampleNewIndex ifAbsentPut: [ self sampleNewValue ]) equals: self sampleNewValue.\x0a\x09self assert: newCollection equals: self collectionWithNewValue",
+messageSends: ["collection", "samplesDo:", "assert:equals:", "at:ifAbsentPut:", "sampleNewValue", "sampleNewIndex", "collectionWithNewValue"],
+referencedClasses: []
+}),
+smalltalk.IndexableCollectionTest);
+
 smalltalk.addMethod(
 smalltalk.addMethod(
 smalltalk.method({
 smalltalk.method({
 selector: "testAtIfPresent",
 selector: "testAtIfPresent",
@@ -4627,6 +4666,29 @@ referencedClasses: []
 }),
 }),
 smalltalk.StringTest);
 smalltalk.StringTest);
 
 
+smalltalk.addMethod(
+smalltalk.method({
+selector: "testAtIfAbsentPut",
+protocol: 'tests',
+fn: function (){
+var self=this;
+function $Error(){return smalltalk.Error||(typeof Error=="undefined"?nil:Error)}
+return smalltalk.withContext(function($ctx1) { 
+self._should_raise_((function(){
+return smalltalk.withContext(function($ctx2) {
+return "hello"._at_ifAbsentPut_((6),(function(){
+return smalltalk.withContext(function($ctx3) {
+return "a";
+}, function($ctx3) {$ctx3.fillBlock({},$ctx2,2)})}));
+}, function($ctx2) {$ctx2.fillBlock({},$ctx1,1)})}),$Error());
+return self}, function($ctx1) {$ctx1.fill(self,"testAtIfAbsentPut",{},smalltalk.StringTest)})},
+args: [],
+source: "testAtIfAbsentPut\x0a\x09\x22String instances are read-only\x22\x0a\x09self should: [ 'hello' at: 6 ifAbsentPut: [ 'a' ] ] raise: Error",
+messageSends: ["should:raise:", "at:ifAbsentPut:"],
+referencedClasses: ["Error"]
+}),
+smalltalk.StringTest);
+
 smalltalk.addMethod(
 smalltalk.addMethod(
 smalltalk.method({
 smalltalk.method({
 selector: "testAtPut",
 selector: "testAtPut",
@@ -5143,6 +5205,57 @@ referencedClasses: ["Set"]
 }),
 }),
 smalltalk.SetTest);
 smalltalk.SetTest);
 
 
+smalltalk.addMethod(
+smalltalk.method({
+selector: "testAddAll",
+protocol: 'tests',
+fn: function (){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
+var $2,$3,$4,$5,$1,$6,$8,$9,$10,$11,$7,$12,$14,$15,$13;
+smalltalk.SetTest.superclass.fn.prototype._testAddAll.apply(_st(self), []);
+$2=self._collection();
+$ctx1.sendIdx["collection"]=1;
+$3=$2;
+$4=self._collection();
+$ctx1.sendIdx["collection"]=2;
+_st($3)._addAll_($4);
+$ctx1.sendIdx["addAll:"]=1;
+$5=_st($2)._yourself();
+$ctx1.sendIdx["yourself"]=1;
+$1=$5;
+$6=self._collection();
+$ctx1.sendIdx["collection"]=3;
+self._assert_equals_($1,$6);
+$ctx1.sendIdx["assert:equals:"]=1;
+$8=self._collection();
+$ctx1.sendIdx["collection"]=4;
+$9=$8;
+$10=self._collectionWithNewValue();
+$ctx1.sendIdx["collectionWithNewValue"]=1;
+_st($9)._addAll_($10);
+$ctx1.sendIdx["addAll:"]=2;
+$11=_st($8)._yourself();
+$ctx1.sendIdx["yourself"]=2;
+$7=$11;
+$12=self._collectionWithNewValue();
+$ctx1.sendIdx["collectionWithNewValue"]=2;
+self._assert_equals_($7,$12);
+$ctx1.sendIdx["assert:equals:"]=2;
+$14=self._collectionWithNewValue();
+$ctx1.sendIdx["collectionWithNewValue"]=3;
+_st($14)._addAll_(self._collection());
+$15=_st($14)._yourself();
+$13=$15;
+self._assert_equals_($13,self._collectionWithNewValue());
+return self}, function($ctx1) {$ctx1.fill(self,"testAddAll",{},smalltalk.SetTest)})},
+args: [],
+source: "testAddAll\x0a\x09super testAddAll.\x0a\x09self assert: (self collection addAll: self collection; yourself) equals: self collection.\x0a\x09self assert: (self collection addAll: self collectionWithNewValue; yourself) equals: self collectionWithNewValue.\x0a\x09self assert: (self collectionWithNewValue addAll: self collection; yourself) equals: self collectionWithNewValue",
+messageSends: ["testAddAll", "assert:equals:", "addAll:", "collection", "yourself", "collectionWithNewValue"],
+referencedClasses: []
+}),
+smalltalk.SetTest);
+
 smalltalk.addMethod(
 smalltalk.addMethod(
 smalltalk.method({
 smalltalk.method({
 selector: "testAddRemove",
 selector: "testAddRemove",
@@ -5221,6 +5334,47 @@ referencedClasses: []
 }),
 }),
 smalltalk.SetTest);
 smalltalk.SetTest);
 
 
+smalltalk.addMethod(
+smalltalk.method({
+selector: "testComma",
+protocol: 'tests',
+fn: function (){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
+var $2,$3,$1,$4,$6,$7,$5,$8,$10,$9;
+smalltalk.SetTest.superclass.fn.prototype._testComma.apply(_st(self), []);
+$2=self._collection();
+$ctx1.sendIdx["collection"]=1;
+$3=self._collection();
+$ctx1.sendIdx["collection"]=2;
+$1=_st($2).__comma($3);
+$ctx1.sendIdx[","]=1;
+$4=self._collection();
+$ctx1.sendIdx["collection"]=3;
+self._assert_equals_($1,$4);
+$ctx1.sendIdx["assert:equals:"]=1;
+$6=self._collection();
+$ctx1.sendIdx["collection"]=4;
+$7=self._collectionWithNewValue();
+$ctx1.sendIdx["collectionWithNewValue"]=1;
+$5=_st($6).__comma($7);
+$ctx1.sendIdx[","]=2;
+$8=self._collectionWithNewValue();
+$ctx1.sendIdx["collectionWithNewValue"]=2;
+self._assert_equals_($5,$8);
+$ctx1.sendIdx["assert:equals:"]=2;
+$10=self._collectionWithNewValue();
+$ctx1.sendIdx["collectionWithNewValue"]=3;
+$9=_st($10).__comma(self._collection());
+self._assert_equals_($9,self._collectionWithNewValue());
+return self}, function($ctx1) {$ctx1.fill(self,"testComma",{},smalltalk.SetTest)})},
+args: [],
+source: "testComma\x0a\x09super testComma.\x0a\x09self assert: self collection, self collection equals: self collection.\x0a\x09self assert: self collection, self collectionWithNewValue equals: self collectionWithNewValue.\x0a\x09self assert: self collectionWithNewValue, self collection equals: self collectionWithNewValue",
+messageSends: ["testComma", "assert:equals:", ",", "collection", "collectionWithNewValue"],
+referencedClasses: []
+}),
+smalltalk.SetTest);
+
 smalltalk.addMethod(
 smalltalk.addMethod(
 smalltalk.method({
 smalltalk.method({
 selector: "testComparing",
 selector: "testComparing",

+ 5 - 5
st/Kernel-Collections.st

@@ -377,6 +377,11 @@ at: anIndex ifAbsent: aBlock
 	self subclassResponsibility
 	self subclassResponsibility
 !
 !
 
 
+at: aKey ifAbsentPut: aBlock
+	^ self at: aKey ifAbsent: [
+		self at: aKey put: aBlock value ]
+!
+
 at: anIndex ifPresent: aBlock
 at: anIndex ifPresent: aBlock
 	"Lookup the given index in the receiver.
 	"Lookup the given index in the receiver.
 	If it is present, answer the value of evaluating aBlock with the value stored at anIndex.
 	If it is present, answer the value of evaluating aBlock with the value stored at anIndex.
@@ -453,11 +458,6 @@ at: aKey ifAbsent: aBlock
 		ifFalse: aBlock
 		ifFalse: aBlock
 !
 !
 
 
-at: aKey ifAbsentPut: aBlock
-	^ self at: aKey ifAbsent: [
-		self at: aKey put: aBlock value ]
-!
-
 at: aKey ifPresent: aBlock ifAbsent: anotherBlock
 at: aKey ifPresent: aBlock ifAbsent: anotherBlock
 	"Lookup the given key in the receiver.
 	"Lookup the given key in the receiver.
 	If it is present, answer the value of evaluating the oneArgBlock with the value associated with the key,
 	If it is present, answer the value of evaluating the oneArgBlock with the value associated with the key,

+ 29 - 0
st/Kernel-Tests.st

@@ -673,6 +673,16 @@ testAtIfAbsent
 		self assert: (self collection at: index ifAbsent: [ self sampleNewValue ]) equals: value ].
 		self assert: (self collection at: index ifAbsent: [ self sampleNewValue ]) equals: value ].
 !
 !
 
 
+testAtIfAbsentPut
+	| newCollection |
+	newCollection := self collection.
+	self samplesDo: [ :index :value |
+		self assert: (newCollection at: index ifAbsentPut: [ self sampleNewValue ]) equals: value ].
+	self assert: newCollection equals: self collection.
+	self assert: (newCollection at: self sampleNewIndex ifAbsentPut: [ self sampleNewValue ]) equals: self sampleNewValue.
+	self assert: newCollection equals: self collectionWithNewValue
+!
+
 testAtIfPresent
 testAtIfPresent
 	| visited sentinel |
 	| visited sentinel |
 	sentinel := Object new.
 	sentinel := Object new.
@@ -1326,6 +1336,11 @@ testAsciiValue
     self assert: (characterU asciiValue) equals:85
     self assert: (characterU asciiValue) equals:85
 !
 !
 
 
+testAtIfAbsentPut
+	"String instances are read-only"
+	self should: [ 'hello' at: 6 ifAbsentPut: [ 'a' ] ] raise: Error
+!
+
 testAtPut
 testAtPut
 	"String instances are read-only"
 	"String instances are read-only"
 	self should: [ 'hello' at: 1 put: 'a' ] raise: Error
 	self should: [ 'hello' at: 1 put: 'a' ] raise: Error
@@ -1483,6 +1498,13 @@ collectionWithNewValue
 
 
 !SetTest methodsFor: 'tests'!
 !SetTest methodsFor: 'tests'!
 
 
+testAddAll
+	super testAddAll.
+	self assert: (self collection addAll: self collection; yourself) equals: self collection.
+	self assert: (self collection addAll: self collectionWithNewValue; yourself) equals: self collectionWithNewValue.
+	self assert: (self collectionWithNewValue addAll: self collection; yourself) equals: self collectionWithNewValue
+!
+
 testAddRemove
 testAddRemove
 	| set |
 	| set |
 	set := Set new.
 	set := Set new.
@@ -1508,6 +1530,13 @@ testCollect
 	self assert: (#(5 6 8) asSet collect: [ :x | x \\ 3 ]) equals: #(0 2) asSet
 	self assert: (#(5 6 8) asSet collect: [ :x | x \\ 3 ]) equals: #(0 2) asSet
 !
 !
 
 
+testComma
+	super testComma.
+	self assert: self collection, self collection equals: self collection.
+	self assert: self collection, self collectionWithNewValue equals: self collectionWithNewValue.
+	self assert: self collectionWithNewValue, self collection equals: self collectionWithNewValue
+!
+
 testComparing
 testComparing
 	self assert: #(0 2) asSet equals: #(0 2) asSet.
 	self assert: #(0 2) asSet equals: #(0 2) asSet.
 	self assert: #(2 0) asSet equals: #(0 2) asSet.
 	self assert: #(2 0) asSet equals: #(0 2) asSet.