#!/usr/bin/env python3

import re
from gradelib import *

r = Runner(save("chcore.out"),
           stop_breakpoint("break_point"))

@test(0)
def test_badinsn():
    r.make_kernel("badinsn")
    r.run_qemu(10)

@test(15, parent=test_badinsn)
def test_badinsn_output():
    r.match("\[INFO\] UnKnown")
    r.match("\[INFO\] sys_exit with value -12")

@test(0, parent=test_badinsn_output)
def test_badinsn2():
    r.make_kernel("badinsn2")
    r.run_qemu(10)

@test(15, parent=test_badinsn2)
def test_badinsn2_output():
    r.match("\[INFO\] UnKnown")
    r.match("\[INFO\] sys_exit with value -12")

@test(0, parent=test_badinsn_output)
def test_hello():
    r.make_kernel("hello")
    r.run_qemu(10)

@test(15, parent=test_hello)
def test_hello_output():
    r.match("hello, world")
    r.match("\[INFO\] sys_exit with value 0")

@test(0, parent=test_hello_output)
def test_putc():
    r.make_kernel("testputc")
    r.run_qemu(10)

@test(5, parent=test_putc)
def test_putc_output():
    r.match("&")
    r.match("\[INFO\] sys_exit with value 0")

@test(0, parent=test_putc_output)
def test_createpmo():
    r.make_kernel("testcreatepmo")
    r.run_qemu(10)

@test(5, parent=test_createpmo)
def test_createpmo_output():
    r.match("\[INFO\] sys_create_pmo called")
    r.match("\[INFO\] sys_exit with value 0")

@test(0, parent=test_createpmo_output)
def test_mappmo():
    r.make_kernel("testmappmo")
    r.run_qemu(10)

@test(5, parent=test_mappmo)
def test_mappmo_output():
    r.match("\[INFO\] sys_map_pmo called")
    r.match("\[INFO\] sys_exit with value 0")

@test(0, parent=test_mappmo_output)
def test_mappmoerr():
    r.make_kernel("testmappmoerr")
    r.run_qemu(10)

@test(5, parent=test_mappmoerr)
def test_mappmoerr_output():
    r.match("\[INFO\] sys_map_pmo called")
    r.match("\[INFO\] sys_exit with value -1")

@test(0, parent=test_mappmoerr_output)
def test_brk():
    r.make_kernel("testsbrk")
    r.run_qemu(10)

@test(10, parent=test_brk)
def test_brk_output():
    r.match("0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,")
    r.match("SBRK_TEST: ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUV.*")
    r.match("\[INFO\] sys_exit with value 0")

@test(0, parent=test_brk_output)
def test_faultread():
    r.make_kernel("faultread")
    r.run_qemu(10)

@test(5, parent=test_faultread)
def test_faultread_output():
    r.match("\[INFO\] pgfault at 0x1 failed")
    r.match("\[INFO\] sys_exit with value -14")

@test(0, parent=test_faultread_output)
def test_faultwrite():
    r.make_kernel("faultwrite")
    r.run_qemu(10)

@test(5, parent=test_faultwrite)
def test_faultwrite_output():
    r.match("\[INFO\] pgfault at 0x1 failed")
    r.match("\[INFO\] sys_exit with value -14")

@test(0, parent=test_faultwrite_output)
def test_pf():
    r.make_kernel("testpf")
    r.run_qemu(10)

@test(15, parent=test_pf)
def test_pf_output():
    r.match("value is deadbeef")
    r.match("\[INFO\] sys_exit with value 0")

run_tests()