Source

newspeak / CollectionTestingUsingStrategy.ns3

The default branch has multiple heads

Full commit
Newspeak3
'NSCollections-tests'

class CollectionTestingUsingStrategy usingPlatform: platform collectionsModuleSource: cms createCollectionsSource: ccs  testingStrategy: strategy testFramework: utf = NewspeakObject (
"Porting the collection tests to use a strategy"
|
	private TestContext = utf TestContext.
	private testingStrategy = strategy.
	private collectionsModuleSource = cms.
	private createCollectionsSource = ccs.
|
)
(

class CollectionsTest = TestContext (
|
|
)
('as yet unclassified'
test: sampleClassSource <String> with: testClassSource <String> = (
	testingStrategy
		test: sampleClassSource
		with: testClassSource
		ifFailure: [:message | failWithMessage: message]
		ifError: [:message | Error new signal: message]
)

testBag01 = (

	testMethod: '
		| bag |
		bag:: cl Bag new.
		assert: [bag ~= nil].
		assert: [bag size = 0].'
)

testBag02 = (

	testMethod: '
		| bag <Bag[Int]> |
		bag:: cl Bag with: 1.
		test: 1 is: [ bag = (cl Bag with: 1) ].
		test: 2 is: [ bag ~= (cl Bag with: 2) ].
		bag add: 1.
		bag include: 9.
		test: 3 is: [ bag = (cl Bag with: 1 with: 1 with: 9) ].
		bag include: 9.
		test: 3 is: [ bag = (cl Bag with: 1 with: 1 with: 9) ].
		test: 4 is: [ bag ~= (cl Bag with: 1) ].
		test: 5 is: [ bag ~= (cl Bag with: 1 with: 2) ].
		test: 6 is: [ (bag occurrencesOf: 1) = 2 ].
		test: 7 is: [ (bag occurrencesOf: 5) = 0 ].
		test: 8 is: [ bag size = 3 ].
		bag remove: 1.
		test: 9  is: [ (bag occurrencesOf: 1) = 1 ].
		test: 10 is: [ bag includes: 1 ].
		bag copy remove: 1.
		test: 11 is: [ bag includes: 1 ].
		bag remove: 1.
		test: 12  is: [ (bag includes: 1) not ].'
)

testIdentitySet = (

	testMethod: '
		| iSet <IdentitySet> a b |
		iSet:: cl IdentitySet new.
		"Number may be boxed in Javascript"
		a:: {}.
		b:: {}.
		assert:[ a = b].
		assert:[(a == b) not].
		iSet add: a.
		assert:[iSet includes: a].
		assert:[(iSet includes: b) not].'
)

testInterval = (

	testMethod: '
		| empty one |
		empty:: cl MutableArrayList withAll: {}.
		one:: cl MutableArrayList withAll: {1}.

		check: (cl Interval from: 1 to: 3) against: (array: {1. 2. 3.}).
		check: (cl Interval from: -1 to: -3 by: -1) against:  (array: {-1. -2. -3}).
		check: (cl Interval from: 100 to: 1 by: -20) against: (array: {100. 80. 60. 40. 20}).
		check: (cl Interval from: 100 to: 0 by: -20) against: (array: {100. 80. 60. 40. 20. 0}).
		check: (cl Interval from: 1 to: 2 by: 3) against: one.
		check: (cl Interval from: 1 to: 2 by: 1) against: (array: {1. 2}).
		check: (cl Interval from: 1 to: 1 by: 1) against: one.
		check: (cl Interval from: 1 to: 2 by: 0.5) against: (array: {1. (1.5). (2.0)}).
		check: (cl Interval from: 1 to: 2 by: -1) against: empty.
		check: (cl Interval from: -1 to: -2 by: 1) against: empty.'
)

testMethod: methodSource = (

	testingStrategy
		test: 'class Sample = ()()'
		with: 'class SampleTest test: ignored =
		(
		|
		cl =', createCollectionsSource, '.
		|)
		(
		',
		collectionsModuleSource,
		'
		''foo''
		test: i is: predicate = (
			assert: predicate description: (''Test '', i printString, '' failed'').
		)
		
		check: interv <Interval> against: seq <List[Number]> = (

			| oc <MutableArrayList[Number]> |
			verify: interv hasSameElementsAndOrderAs: seq.

			oc:: cl MutableArrayList"[Number]" new.
			interv do: [ :n <Number> | oc add: n ].
			verify: oc hasSameElementsAndOrderAs: seq.

			"do the following just to exercise collection #hasSameElementsAs: a bit"
			assert: [ interv hasSameElementsAs: seq ].
		)
		
		verify: interv <Interval> hasSameElementsAndOrderAs: other <List[Number]>
 = (
			| c <Int> |
			assert: [interv size = other size].
			1 to: interv size do:
				[ :i <Int> |
					assert:[(interv at: i) = (other at: i)].	].
			"now test using do:"
			c:: 0.
			interv do:
				[ :i <Number> |
					c:: c + 1.
					assert: [i = (other at: c) ]].
			assert: [ c = interv size ].
		)

		key: k value: v = (
			^cl Association key: k value: v
		)
		
		array: values = (
			^cl MutableArrayList withAll: values
		)

		test = (', methodSource, ')
		)'
		ifFailure: [:message | failWithMessage: message]
		ifError: [:message | Error new signal: message]
)

testMutableArrayList
 = (

	testMethod: '
		"Set behavior of this class is tested by Set test."
		| oc <MutableArrayList[Int]> |
		oc:: cl MutableArrayList"[Int]" new: 3.
		oc addLast: 3; addLast: 4; addFirst: 2; addFirst: 1; addLast: 5; addLast: 6; addLast: 7.
		"%todo: Use #(1 2 3 4 5 6 7) as withAll: arg below when literals arrays work"
		test: 1 is: [ oc = (cl MutableArrayList"[Int]" withAll:
						((cl MutableArrayList"[Int]" new)
						addLast: 1;
						addLast: 2;
						addLast: 3;
						addLast: 4;
						addLast: 5;
						addLast: 6;
						addLast: 7;
						yourself )) ].
		test: 2 is: [ (oc at: 1) = 1 ].
		test: 3 is: [ oc size = 7 ].
		test: 4 is: [ (oc at: 7 ifAbsent: [ nil ]) = 7 ].
		test: 5 is: [ (oc at: 0 ifAbsent: [ nil ]) = nil ].
		test: 6 is: [ (oc at: 8 ifAbsent: [ nil ]) = nil ].
		test: 7 is: [ (oc indexOf: 3 ifAbsent: [ nil ]) = 3 ].
		oc remove: 3; removeAt: 3; removeAt: 5; removeAt: 1; remove: 2.
		test: 8 is: [  oc = ((cl MutableArrayList"[Int]" new)
							addLast: 5;
							addLast: 6;
							yourself )	].
		oc addFirst: 4; addLast: 7; removeAt: 2; removeAt: 2.
		test: 9 is: [  oc = ((cl MutableArrayList"[Int]" new)
							addLast: 4;
							addLast: 7;
							yourself )	].
		oc removeAt: 1; removeAt: 1.
		test: 10 is: [ oc size = 0 ].
		test: 11 is: [ (oc remove: 2 ifAbsent: [ nil ]) = nil ].
		oc add: 2; add: 1; add: 2; add: 2; add: 3; add: 2; removeEvery: 2.
		test: 12 is: [  oc = ((cl MutableArrayList"[Int]" new)
							addLast: 1;
							addLast: 3;
							yourself )	].'
)

testMutableHashedMap = (

	testMethod: '
		| d <MutableHashedMap[Str,Str]> isNew <Boolean> oc <MutableArrayList[Assoc[Str,Str]]> |
		d:: cl MutableHashedMap new.
		d at: #this put: #that.
		test: 1 is: [ (d at: #this) = #that ].
		isNew:: false.
		d at: #this put: #that ifNew: [ isNew:: true ].
		d checkValidity.
		test: 2 is: [ isNew not ].
		d at: #hi put: #there ifNew: [ isNew:: true ].
		d checkValidity.
		test: 3 is: [ isNew ].
		oc:: cl MutableArrayList new.
		d keysAndValuesDo:
			[ :k <Str> :v <Str> |
				oc add: (key: k value: v) ].
		test: 4 is: [ oc size = 2 and: [ oc = d associations] ]. "hasSameElementsAs:"
		d addAssociation: (key: #hi value: #mom).
		d checkValidity.
		test: 5 is: [ (d associationAt: #hi) value = #mom ].
		test: 6 is: [ d includesAssociation: (key: #hi value: #mom) ].
		test: 7 is: [ (d includesAssociation: (key: #yo value: #momma)) not ].
		(d copy) at: #hi put: #there; checkValidity.
		d checkValidity.
		test: 8 is: [ (d at: #hi) = #mom ].
		isNew:: false.
		d removeAssociation: (cl Association key: #this value: #that) ifAbsent: [ isNew:: true ].
		d checkValidity.
		test: 9 is: [ (d includesKey: #this) not and: [isNew not] ].
		d removeAssociation: (key: #bubba value: #that) ifAbsent: [ isNew:: true ].
		d checkValidity.
		test: 10 is: [ isNew ].'
)

testQueue = (

	testMethod: '
		| q <Queue[Int]> |
		q:: cl Queue new.
		q nextPut: 1; checkValidity; nextPut: 2; checkValidity; nextPut: 3; checkValidity; nextPut: 4; checkValidity.
		assert: [ (array: {1. 2. 3. 4}) hasSameElementsAndOrderAs: q].
	
		q remove: 2; checkValidity.
		assert: [(array: {1. 3. 4}) hasSameElementsAndOrderAs: q ].

		q remove: 4; checkValidity.
		assert: [(array: {1. 3}) hasSameElementsAndOrderAs: q ].

		q remove: 1; checkValidity.
		assert: [(array: {3}) hasSameElementsAndOrderAs: q ].

		q remove: 3; checkValidity.
		assert: [ (array: {}) hasSameElementsAndOrderAs: q ].'
)

testSet
 = (

	testMethod: '
		| s oc <MutableArrayList[Str]> |

		s:: cl Set new.
		oc:: cl MutableArrayList"[Str]" new.

		3 timesRepeat: [ 
			assert: [s hasSameElementsAs: oc].
			assert: [s size = 0]. assert: [(s includes: 3) not]. assert: [(oc includes: 3) not].

			s include: ''hi''.
			assert: [(s hasSameElementsAs: oc) not].
			oc add: ''hi''.
			assert: [s hasSameElementsAs: oc].
		
			assert: [s size = 1]. assert: [s includes: ''hi'' ]. assert: [oc includes: ''hi''].

			s include: ''there''.
			oc add: ''there''.

			assert: [s hasSameElementsAs: oc].
			assert: [s size = 2]. assert: [s includes: ''hi'' ]. assert: [s includes: ''there''].

			s remove: ''there''.
			oc remove: ''there''.

			assert: [s hasSameElementsAs: oc].
			assert: [s size = 1]. assert: [s includes: ''hi'' ]. assert: [oc includes: ''hi''].

			s include: ''there''.
			oc add: ''there''.

			assert: [s hasSameElementsAs: oc].
			assert: [s size = 2]. assert: [s includes: ''hi'' ]. assert: [s includes: ''there''].

			s include: ''hello''; include: #there; include: #bub.
			oc add: ''hello''; add: #bub.

			assert: [s hasSameElementsAs: oc].
			assert: [s size = 4]. assert: [s includes: ''hi'' ]. assert: [oc includes: ''bub''].

			s include: ''hello''; include: #there; include: #bub.
			"oc add: ''hello''; add: #there; add: #bub."

			assert: [s hasSameElementsAs: oc].
			assert: [s size = 4]. assert: [s includes: ''hi'' ]. assert: [s includes: ''hi''].

			s include: ''mello''.	[ s size = 5 ].
			s include: #here.		 [ s size = 6 ].
			s include: #blub. 		[ s size = 7].
			oc add: ''mello''; add: #here; add: #blub.

			assert: [s hasSameElementsAs: oc].
			assert: [s size = 7]. assert: [s includes: ''mello'' ]. assert: [s includes: ''hi''].

			s remove: ''mello''.
			oc remove: ''mello'' ifAbsent: [].

			assert: [s hasSameElementsAs: oc].
			assert: [s size = 6]. assert: [(s includes: ''mello'') not]. assert: [(oc includes: ''mello'') not].

			s removeAll: s copy.
			oc removeAll: oc copy. "oc:: cl MutableArrayList[Str] new."
		
			"loop must end with empty collections, so we can do it again with shrunken state"]'
)

testSortedList = (
	"self test"

	testMethod: '
		| r <Random> c <Array[Int]> s <SortedList[Int]> index <Int> Random |
		
		Random:: TestingPlatform new Random.

		"A binary search test"
		1 to: 9 do: [ :i <Int> |
			index:: ((cl MutableArrayList) withAll: {1. 2. 3. 4. 5. 6. 7. 8. 9}) binarySearchFor: i toCompare: [ :i1 <Int> :i2 <Int> | i1 <= i2 ].
			assert: [ index = i or: [ index = ( i + 1) ] ].
		].

		"A descending sort"
		r:: Random new.
		100 timesRepeat: [
			c:: Array"[Int]" new: (r next * 500) // 1.
			1 to: c size do:
				[ :i <Int> |
					c at: i put: r next].
			s:: cl SortedList withAll: c.
			assert: [ (s sort) isSorted ].		].

	"and an ascending sort"
	r:: Random new.
	100 timesRepeat:
		[	c:: Array"[Int]" new: (r next * 500) // 1.
			1 to: c size do:
				[ :i <Int> |
					c at: i put: r next].
			s:: cl SortedList withAll: c.
			assert: [ (s sortBlock: [ :i1 <Int> :i2 <Int> | i1 >= i2 ]) isSorted ].		].

		"An insertion sort test"
		s:: cl SortedList"[Int]" new.
		s add: 5; add: 2; add: 6; add: 8; add: 9; add: 1; add: 123; add: 4321; add: 12.
		assert: [s isSorted ].'
)

) : (
'as yet unclassified'
TEST_CONTEXT = (
	
)

))