Skip to content

Commit

Permalink
Adds basic tests and patterns for type inference
Browse files Browse the repository at this point in the history
Issue: #92
  • Loading branch information
kpp committed Aug 13, 2016
1 parent a1c3cb7 commit 9c654c5
Show file tree
Hide file tree
Showing 5 changed files with 561 additions and 59 deletions.
219 changes: 164 additions & 55 deletions image/imageSource.st
Original file line number Diff line number Diff line change
Expand Up @@ -776,18 +776,115 @@ COMMENT
CLASS BranchTest Test
METHOD BranchTest
ifTrue |x|
ifBlock |x|
[ true ] ifTrue: [ x <- 123 ].
[ x ] assertEq: 123 withComment: '1'.
[ false ] ifFalse: [ x <- 151 ].
[ x ] assertEq: 151 withComment: '2'.
[ true ] ifTrue: [ x <- 123 ]
ifFalse: [ x <- 151 ]
[ x ] assertEq: 123
!
ifFalse: [ x <- 151 ].
[ x ] assertEq: 123 withComment: '3'.
[ false ] ifFalse: [ x <- 521 ]
ifTrue: [ x <- 34 ].
[ x ] assertEq: 521 withComment: '4'.
!
METHOD BranchTest
ifFalse |x|
ifBlockBadCodegenIfTrue |x|
[ false ] ifTrue: [ x <- 34 ]
ifFalse: [ x <- 521 ]
[ x ] assertEq: 521
ifFalse: [ x <- 521 ].
[ x ] assertEq: 521.
!
METHOD BranchTest
ifBlockBadCodegenIfFalse |x|
[ true ] ifFalse: [ x <- 151 ]
ifTrue: [ x <- 123 ].
[ x ] assertEq: 123.
!
METHOD BranchTest
ifLiteral |x|
true ifTrue: [ x <- 123 ].
[ x ] assertEq: 123 withComment: '1'.
false ifFalse: [ x <- 151 ].
[ x ] assertEq: 151 withComment: '2'.
true ifTrue: [ x <- 123 ]
ifFalse: [ x <- 151 ].
[ x ] assertEq: 123 withComment: '3'.
false ifTrue: [ x <- 34 ]
ifFalse: [ x <- 521 ].
[ x ] assertEq: 521 withComment: '4'.
true ifFalse: [ x <- 151 ]
ifTrue: [ x <- 123 ].
[ x ] assertEq: 123 withComment: '5'.
false ifFalse: [ x <- 521 ]
ifTrue: [ x <- 34 ].
[ x ] assertEq: 521 withComment: '6'.
!
METHOD BranchTest
ifRealBooleanMethods |arr x|
Context new performTest: (Boolean methods at: #ifTrue:)
withArguments: (Array with: true with: [ x <- 123 ]
).
[ x ] assertEq: 123 withComment: '1'.
Context new performTest: (Boolean methods at: #ifFalse:)
withArguments: (Array with: false with: [ x <- 151 ]
).
[ x ] assertEq: 151 withComment: '2'.
Context new performTest: (Boolean methods at: #ifTrue:ifFalse:)
withArguments: (Array with: true with: [ x <- 123 ] with: [ x <- 151 ]
).
[ x ] assertEq: 123 withComment: '3'.
Context new performTest: (Boolean methods at: #ifTrue:ifFalse:)
withArguments: (Array with: false with: [ x <- 32 ] with: [ x <- 521 ]
).
[ x ] assertEq: 521 withComment: '4'.
Context new performTest: (Boolean methods at: #ifFalse:ifTrue:)
withArguments: (Array with: true with: [ x <- 151 ] with: [ x <- 123 ]
).
[ x ] assertEq: 123 withComment: '5'.
Context new performTest: (Boolean methods at: #ifFalse:ifTrue:)
withArguments: (Array with: false with: [ x <- 521 ] with: [ x <- 34 ]
).
[ x ] assertEq: 521 withComment: '6'.
!
METHOD BranchTest
ifRealTrueFalseMethods |arr x|
Context new performTest: (True methods at: #ifTrue:)
withArguments: (Array with: true with: [ x <- 123 ]
).
[ x ] assertEq: 123 withComment: '1'.
Context new performTest: (True methods at: #ifFalse:)
withArguments: (Array with: true with: [ x <- 151 ]
).
[ x ] assertEq: 123 withComment: '2'.
Context new performTest: (False methods at: #ifTrue:)
withArguments: (Array with: false with: [ x <- 151 ]
).
[ x ] assertEq: 123 withComment: '3'.
Context new performTest: (False methods at: #ifFalse:)
withArguments: (Array with: false with: [ x <- 151 ]
).
[ x ] assertEq: 151 withComment: '4'.
!
METHOD BranchTest
Expand Down Expand Up @@ -1008,7 +1105,7 @@ init
arrOfChars <- Array new: 257.
1 to: 257 do: [:idx|
arrOfChars at: idx put: (Char basicNew: idx-1)
arrOfChars at: idx put: (Char new: idx-1)
].
!
Expand Down Expand Up @@ -2057,7 +2154,6 @@ run4: rounds | list indices tree |

METHOD Undefined
main | command data x |
Char initialize.
Class fillChildren.
System fixMethodClasses.

Expand All @@ -2072,35 +2168,40 @@ main | command data x |


COMMENT -----------Boolean--------------
METHOD MetaBoolean
new: arg
arg ifTrue: [ ^ true ].
^ false
!
METHOD Boolean
and: aBlock
^ self
ifTrue: [ aBlock value ]
ifFalse: [ false ]
ifTrue: aBlock
self ifTrue: [ ^ aBlock value ].
!
METHOD Boolean
or: aBlock
^ self
ifTrue: [ true ]
ifFalse: [ aBlock value ]
ifFalse: aBlock
self ifFalse: [ ^ aBlock value ].
!
METHOD Boolean
not
^ self
ifTrue: [ false ]
ifFalse: [ true ]
ifTrue: trueBlock ifFalse: falseBlock
self ifTrue: [ ^ trueBlock value ].
self ifFalse:[ ^ falseBlock value ].
!
METHOD Boolean
ifFalse: falseBlock ifTrue: trueBlock
^ self ifTrue: [ trueBlock value ] ifFalse: [ falseBlock value ]
self ifTrue: [ ^ trueBlock value ].
self ifFalse:[ ^ falseBlock value ].
!
METHOD Boolean
ifTrue: aBlock
^ self ifTrue: [ aBlock value ] ifFalse: [ nil ]
and: aBlock
^ self and: [ ^ Boolean new: aBlock value ]
!
METHOD Boolean
ifFalse: aBlock
^ self ifTrue: [ nil ] ifFalse: [ aBlock value ]
or: aBlock
^ self or: [ ^ Boolean new: aBlock value ]
!
METHOD Boolean
not
^ self not
!
COMMENT -----------True--------------
METHOD MetaTrue
Expand All @@ -2109,10 +2210,6 @@ new
^ true
!
METHOD True
not
^ false
!
METHOD True
asString
^'true'
!
Expand All @@ -2121,16 +2218,24 @@ printString
^self asString
!
METHOD True
ifTrue: trueBlock ifFalse: falseBlock
^ trueBlock value
ifTrue: aBlock
^ aBlock value
!
METHOD True
or: aBlock
^ true
ifFalse: aBlock
^ nil
!
METHOD True
and: aBlock
^ aBlock value
^ Boolean new: aBlock value
!
METHOD True
or: aBlock
^ true
!
METHOD True
not
^ false
!
COMMENT -----------False--------------
METHOD MetaFalse
Expand All @@ -2139,10 +2244,6 @@ new
^ false
!
METHOD False
not
^ true
!
METHOD False
asString
^'false'
!
Expand All @@ -2151,17 +2252,27 @@ printString
^self asString
!
METHOD False
ifTrue: trueBlock ifFalse: falseBlock
^ falseBlock value
ifTrue: aBlock
^ nil
!
METHOD False
or: aBlock
^ aBlock value
ifFalse: aBlock
^ aBlock value
!
METHOD False
and: aBlock
^ false
!
METHOD False
or: aBlock
^ Boolean new: aBlock value
!
METHOD False
not
^ true
!

COMMENT -----------Thread--------------
METHOD MetaThread
new: aBlock |instance|
instance <- self new.
Expand Down Expand Up @@ -2484,30 +2595,28 @@ args: argNames inst: instNames temp: tempNames
!
COMMENT -----------Chars--------------
METHOD MetaChar
initialize
chars isNil ifTrue: [
chars <- Array new: 257.
1 to: 257 do: [:idx|
chars at: idx put: (Char basicNew: idx-1)
]
]
!
METHOD MetaChar
basicNew: value
" create and initialize a new char "
^ self in: self new at: 1 put: value
!
METHOD MetaChar
new: value
" return unique Char for ASCII value (or EOF) "
(value < 257) ifTrue: [ ^ chars at: value+1 ].

(value < 257) ifTrue: [
chars isNil ifTrue: [
chars <- Array new: 257.
1 to: 257 do: [:idx|
chars at: idx put: (Char basicNew: idx-1)
]
].
^ chars at: value+1
].
" otherwise build a custom Char "
^ self basicNew: value
!
METHOD MetaChar
newline
" return newline character "
" return newline character "
^ self new: 10
!
METHOD MetaChar
Expand Down
4 changes: 4 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND} --output-on-failure)
macro(cxx_test pretty_name bin_name sources libs)
add_executable(${bin_name} ${sources})
target_link_libraries(${bin_name} supc++ -pthread ${libs} ${GTEST_BOTH_LIBRARIES} ${READLINE_LIBS_TO_LINK})
if (USE_LLVM)
target_link_libraries(${bin_name} jit trampoline ${LLVM_LIBS} ${LLVM_LD_FLAGS})
endif()
set_target_properties(${bin_name} PROPERTIES COMPILE_DEFINITIONS TESTS_DIR=\"${CMAKE_CURRENT_SOURCE_DIR}/\")
add_test(${pretty_name} ${bin_name})
add_dependencies(check ${bin_name})
Expand All @@ -22,3 +25,4 @@ cxx_test(StackSemantics test_stack_semantics "${CMAKE_CURRENT_SOURCE_DIR}/stack_
# TODO cxx_test(StackUnderflow test_stack_underflow "${CMAKE_CURRENT_SOURCE_DIR}/stack_underflow.cpp" "stapi")
cxx_test(DecodeAllMethods test_decode_all_methods "${CMAKE_CURRENT_SOURCE_DIR}/decode_all_methods.cpp" "stapi;memory_managers;standard_set")
cxx_test("VM::primitives" test_vm_primitives "${CMAKE_CURRENT_SOURCE_DIR}/vm_primitives.cpp" "memory_managers;standard_set")
cxx_test(Inference test_inference "${CMAKE_CURRENT_SOURCE_DIR}/inference.cpp" "stapi;memory_managers;standard_set")
Binary file added tests/data/Inference.image
Binary file not shown.
Loading

0 comments on commit 9c654c5

Please sign in to comment.