Browse Source

- fixes SequenceableCollection >> endsWith:
- adds tests new SequenceableCollection methods

Nicolas Petton 11 years ago
parent
commit
58cc0bbce9
4 changed files with 85 additions and 9 deletions
  1. 6 5
      js/Kernel-Collections.js
  2. 67 2
      js/Kernel-Tests.js
  3. 1 1
      st/Kernel-Collections.st
  4. 11 1
      st/Kernel-Tests.st

+ 6 - 5
js/Kernel-Collections.js

@@ -2807,22 +2807,23 @@ protocol: 'accessing',
 fn: function (aNumber){
 var self=this;
 return smalltalk.withContext(function($ctx1) { 
-var $2,$1,$5,$4,$3;
+var $2,$1,$6,$5,$4,$3;
 $2=self._size();
 $ctx1.sendIdx["size"]=1;
 $1=_st($2).__lt(aNumber);
 if(smalltalk.assert($1)){
 self._error_("Invalid number of elements");
 };
-$5=self._size();
+$6=self._size();
 $ctx1.sendIdx["size"]=2;
-$4=_st($5).__minus(aNumber);
+$5=_st($6).__minus(aNumber);
+$4=_st($5).__plus((1));
 $3=self._copyFrom_to_($4,self._size());
 return $3;
 }, function($ctx1) {$ctx1.fill(self,"last:",{aNumber:aNumber},smalltalk.SequenceableCollection)})},
 args: ["aNumber"],
-source: "last: aNumber\x0a\x09\x22Answer the last aNumber elements of the receiver.\x0a\x09Raise an error if there are not enough elements in the receiver.\x22\x0a\x0a\x09self size < aNumber ifTrue: [ self error: 'Invalid number of elements' ].\x0a\x0a\x09^ self copyFrom: self size - aNumber to: self size",
-messageSends: ["ifTrue:", "<", "size", "error:", "copyFrom:to:", "-"],
+source: "last: aNumber\x0a\x09\x22Answer the last aNumber elements of the receiver.\x0a\x09Raise an error if there are not enough elements in the receiver.\x22\x0a\x0a\x09self size < aNumber ifTrue: [ self error: 'Invalid number of elements' ].\x0a\x0a\x09^ self copyFrom: self size - aNumber + 1 to: self size",
+messageSends: ["ifTrue:", "<", "size", "error:", "copyFrom:to:", "+", "-"],
 referencedClasses: []
 }),
 smalltalk.SequenceableCollection);

+ 67 - 2
js/Kernel-Tests.js

@@ -2737,6 +2737,71 @@ smalltalk.DictionaryTest.klass);
 
 
 smalltalk.addClass('SequenceableCollectionTest', smalltalk.IndexableCollectionTest, [], 'Kernel-Tests');
+smalltalk.addMethod(
+smalltalk.method({
+selector: "testBeginsWith",
+protocol: 'tests',
+fn: function (){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
+var $2,$4,$3,$1,$6,$5;
+$2=self._collection();
+$ctx1.sendIdx["collection"]=1;
+$4=self._collection();
+$ctx1.sendIdx["collection"]=2;
+$3=_st($4)._first_((3));
+$1=_st($2)._beginsWith_($3);
+$ctx1.sendIdx["beginsWith:"]=1;
+self._assert_($1);
+$6=self._collection();
+$ctx1.sendIdx["collection"]=3;
+$5=_st($6)._beginsWith_(_st(self._collection())._copyFrom_to_((2),(3)));
+self._deny_($5);
+return self}, function($ctx1) {$ctx1.fill(self,"testBeginsWith",{},smalltalk.SequenceableCollectionTest)})},
+args: [],
+source: "testBeginsWith\x0a\x09self assert: (self collection beginsWith: (self collection first: 3)).\x0a\x09self deny: (self collection beginsWith: (self collection copyFrom: 2 to: 3))",
+messageSends: ["assert:", "beginsWith:", "collection", "first:", "deny:", "copyFrom:to:"],
+referencedClasses: []
+}),
+smalltalk.SequenceableCollectionTest);
+
+smalltalk.addMethod(
+smalltalk.method({
+selector: "testEndsWith",
+protocol: 'tests',
+fn: function (){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
+var $2,$4,$3,$1,$6,$8,$11,$10,$9,$7,$5;
+$2=self._collection();
+$ctx1.sendIdx["collection"]=1;
+$4=self._collection();
+$ctx1.sendIdx["collection"]=2;
+$3=_st($4)._last_((3));
+$1=_st($2)._endsWith_($3);
+$ctx1.sendIdx["endsWith:"]=1;
+self._assert_($1);
+$6=self._collection();
+$ctx1.sendIdx["collection"]=3;
+$8=self._collection();
+$ctx1.sendIdx["collection"]=4;
+$11=self._collection();
+$ctx1.sendIdx["collection"]=5;
+$10=_st($11)._size();
+$ctx1.sendIdx["size"]=1;
+$9=_st($10).__minus((3));
+$ctx1.sendIdx["-"]=1;
+$7=_st($8)._copyFrom_to_($9,_st(_st(self._collection())._size()).__minus((1)));
+$5=_st($6)._endsWith_($7);
+self._deny_($5);
+return self}, function($ctx1) {$ctx1.fill(self,"testEndsWith",{},smalltalk.SequenceableCollectionTest)})},
+args: [],
+source: "testEndsWith\x0a\x09self assert: (self collection endsWith: (self collection last: 3)).\x0a\x09self deny: (self collection endsWith: (self collection copyFrom: self collection size - 3 to: self collection size - 1))",
+messageSends: ["assert:", "endsWith:", "collection", "last:", "deny:", "copyFrom:to:", "-", "size"],
+referencedClasses: []
+}),
+smalltalk.SequenceableCollectionTest);
+
 smalltalk.addMethod(
 smalltalk.method({
 selector: "testFirst",
@@ -2848,7 +2913,7 @@ $7=self._collection();
 $ctx1.sendIdx["collection"]=3;
 $6=_st($7)._size();
 $ctx1.sendIdx["size"]=1;
-$5=_st($6).__minus((2));
+$5=_st($6).__minus((1));
 $9=self._collection();
 $ctx1.sendIdx["collection"]=4;
 $8=_st($9)._size();
@@ -2860,7 +2925,7 @@ return _st(self._collection())._last_((33));
 }, function($ctx2) {$ctx2.fillBlock({},$ctx1,1)})}),$Error());
 return self}, function($ctx1) {$ctx1.fill(self,"testLastN",{},smalltalk.SequenceableCollectionTest)})},
 args: [],
-source: "testLastN\x0a\x09self \x0a\x09\x09assert: (self collection last: 2) \x0a\x09\x09equals: (self collection copyFrom: self collection size -2 to: self collection size).\x0a\x09\x09\x0a\x09self should: [ self collection last: 33 ] raise: Error",
+source: "testLastN\x0a\x09self \x0a\x09\x09assert: (self collection last: 2) \x0a\x09\x09equals: (self collection copyFrom: self collection size -1 to: self collection size).\x0a\x09\x09\x0a\x09self should: [ self collection last: 33 ] raise: Error",
 messageSends: ["assert:equals:", "last:", "collection", "copyFrom:to:", "-", "size", "should:raise:"],
 referencedClasses: ["Error"]
 }),

+ 1 - 1
st/Kernel-Collections.st

@@ -871,7 +871,7 @@ last: aNumber
 
 	self size < aNumber ifTrue: [ self error: 'Invalid number of elements' ].
 
-	^ self copyFrom: self size - aNumber to: self size
+	^ self copyFrom: self size - aNumber + 1 to: self size
 !
 
 second

+ 11 - 1
st/Kernel-Tests.st

@@ -842,6 +842,16 @@ IndexableCollectionTest subclass: #SequenceableCollectionTest
 
 !SequenceableCollectionTest methodsFor: 'tests'!
 
+testBeginsWith
+	self assert: (self collection beginsWith: (self collection first: 3)).
+	self deny: (self collection beginsWith: (self collection copyFrom: 2 to: 3))
+!
+
+testEndsWith
+	self assert: (self collection endsWith: (self collection last: 3)).
+	self deny: (self collection endsWith: (self collection copyFrom: self collection size - 3 to: self collection size - 1))
+!
+
 testFirst
 	self assert: (self collection first) equals: (self collection at: 1)
 !
@@ -865,7 +875,7 @@ testLast
 testLastN
 	self 
 		assert: (self collection last: 2) 
-		equals: (self collection copyFrom: self collection size -2 to: self collection size).
+		equals: (self collection copyFrom: self collection size -1 to: self collection size).
 		
 	self should: [ self collection last: 33 ] raise: Error
 !