Skip to content

Commit

Permalink
some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
carolahp committed May 29, 2020
1 parent 611d269 commit 8f6d66d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
16 changes: 16 additions & 0 deletions src/Kernel-Tests/SmallIntegerTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ SmallIntegerTest >> testBasicNew [
self should: [SmallInteger basicNew] raise: self defaultTestError.
]

{ #category : #'tests - operations' }
SmallIntegerTest >> testBitAnd [
self assert: (2r1010101 bitAnd: 2r0101010) equals: 0.
self assert: (2r1010101 bitAnd: 2r1010101) equals: 2r1010101
]

{ #category : #'tests - Class Methods' }
SmallIntegerTest >> testByteAt [

Expand Down Expand Up @@ -102,3 +108,13 @@ SmallIntegerTest >> testPrintString [
Smalltalk vm wordSize = 8
ifTrue: [ self assert: SmallInteger maxVal decimalDigitLength equals: 19 ].
]

{ #category : #tests }
SmallIntegerTest >> testQuo [
self should: [ 1 quo: 0] raise: ZeroDivide.
self assert: ( 1 quo: 2) equals: 0.
self assert: ( 4 quo: 2) equals: 2.
self assert: (-4 quo: 2) equals: -2.
self assert: (-5 quo: 2) equals: -2.
self assert: ( 5 quo: 2) equals: 2
]
29 changes: 24 additions & 5 deletions src/Kernel/SmallInteger.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,13 @@ SmallInteger >> bytesCount [
"Answer the number of indexable fields in the receiver. This value is the
same as the largest legal subscript. Included so that a SmallInteger can
behave like a LargePositiveInteger or LargeNegativeInteger."


"32768 == (1 bitShift: 15)"
"32768 bytesCount >>> 2"

"65536 == (1 bitShift: 16)"
"65536 bytesCount >>> 3"

| value length |
length := 1.
value := self.
Expand All @@ -328,6 +334,9 @@ SmallInteger >> bytesCount [
SmallInteger >> decimalDigitLength [
"Answer the number of digits printed out in base 10.
Note that this only works for positive SmallIntegers up to 64-bits."
"1 decimalDigitLength >>> 1"
"100000000 decimalDigitLength >>> 9"
"SmallInteger maxVal decimalDigitLength >>> 19"

^self < 10000
ifTrue:
Expand Down Expand Up @@ -366,7 +375,12 @@ SmallInteger >> deepCopy [

{ #category : #testing }
SmallInteger >> even [

" 0 odd >>> true"
" 2 odd >>> true"
"-2 odd >>> true"
" 3 odd >>> false"
"-3 odd >>> false"

^(self bitAnd: 1) = 0
]

Expand Down Expand Up @@ -469,11 +483,11 @@ SmallInteger >> isLarge [
{ #category : #'bit manipulation' }
SmallInteger >> lowBit [
" Answer the index of the low order one bit.
2r00101000 lowBit (Answers: 4)
2r-00101000 lowBit (Answers: 4)
First we skip bits in groups of 8, then do a lookup in a table.
While not optimal, this is a good tradeoff; long
integer #lowBit always invokes us with bytes."
"2r00101000 lowBit >>> 4"
"2r-00101000 lowBit >>> 4"
| n result lastByte |
n := self.
n = 0 ifTrue: [ ^ 0 ].
Expand Down Expand Up @@ -516,7 +530,11 @@ SmallInteger >> numberOfDigitsInBase: b [

{ #category : #testing }
SmallInteger >> odd [

" 0 odd >>> false"
" 2 odd >>> false"
"-2 odd >>> false"
" 3 odd >>> true"
"-3 odd >>> true"
^(self bitAnd: 1) = 1
]

Expand Down Expand Up @@ -638,6 +656,7 @@ SmallInteger >> quo: aNumber [
result. Round the result down towards zero to make it a whole integer.
Fail if the argument is 0 or is not a SmallInteger. Optional. See Object
documentation whatIsAPrimitive."

<primitive: 13>
aNumber = 0 ifTrue: [^ (ZeroDivide dividend: self) signal].
(aNumber isMemberOf: SmallInteger)
Expand Down

0 comments on commit 8f6d66d

Please sign in to comment.