Skip to content

Commit

Permalink
tests/interpreter: add simple tests for external functions for #1
Browse files Browse the repository at this point in the history
  • Loading branch information
pdarragh committed Jul 6, 2022
1 parent 3e8408c commit bd7138f
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions tests/interpreter.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,55 @@
(Sar 'rax 1)
(Jmp 'compare)
(Label 'finish))))

;; Runtimes.
(define (test-external-func1)
(let* ([prog (Program (list (Extern 'extern)
(Label 'entry)
(Mov 'rax 42)))]
[runtime (hash 'extern (λ (x) (+ x 2)))]
[state (initialize-state prog runtime)])
(interp state 0)))

(define (test-external-func2)
(let* ([prog (Program (list (Extern 'extern)
(Label 'entry)
(Mov 'rdi 42)
(Call 'extern)))]
[runtime (hash 'extern (λ (x) (+ x 2)))]
[state (initialize-state prog runtime)])
(interp state 4)))

(define (test-external-func3)
(let* ([prog (Program (list (Extern 'extern)
(Label 'entry)
(Mov 'rdi 1)
(Mov 'rsi 2)
(Mov 'rdx 3)
(Mov 'rcx 4)
(Mov 'r8 5)
(Mov 'r9 6)
(Push 7)
(Push 8)
(Call 'extern)))]
[runtime (hash 'extern (λ (a1 a2 a3 a4 a5 a6 a7 a8)
(+ a1 a2 a3 a4 a5 a6 a7 a8)))]
[state (initialize-state prog runtime)])
(interp state -1)))

(define (test-read-byte)
(let* ([prog (Program (list (Extern 'read-byte)
(Label 'entry)
(Call 'read-byte)))]
[runtime (hash 'read-byte (λ () (read-byte)))]
[state (initialize-state prog runtime)])
(interp state -1)))

(define (test-write-byte)
(let* ([prog (Program (list (Extern 'write-byte)
(Label 'entry)
(Mov 'rdi 97)
(Call 'write-byte)))]
[runtime (hash 'write-byte (λ (b) (write-byte b)))]
[state (initialize-state prog runtime)])
(interp state -1)))

0 comments on commit bd7138f

Please sign in to comment.