-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
216 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/* | ||
* Copyright 2018 De-labtory | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package vm | ||
|
||
import ( | ||
"testing" | ||
|
||
"bytes" | ||
|
||
"github.com/DE-labtory/koa/opcode" | ||
) | ||
|
||
func TestAssemble(t *testing.T) { | ||
rawByteCode := make([]byte, 0) | ||
rawByteCode = append(rawByteCode, uint8(opcode.Push)) | ||
rawByteCode = append(rawByteCode, uint32To4Bytes(1)...) // 1 byte allocation | ||
rawByteCode = append(rawByteCode, uint8(opcode.Push)) | ||
rawByteCode = append(rawByteCode, uint32To4Bytes(300)...) // 2 byte allocation | ||
rawByteCode = append(rawByteCode, uint8(opcode.Add)) | ||
|
||
testAsmExpected := asm{ | ||
code: []hexer{ | ||
push{}, Data{Body: uint32To4Bytes(1)}, | ||
push{}, Data{Body: uint32To4Bytes(300)}, | ||
add{}, | ||
}, | ||
} | ||
|
||
asm, err := assemble(rawByteCode) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
//opCode is a byte | ||
//Data is 4 bytes | ||
for i, code := range asm.code { | ||
switch code.(type) { | ||
case opCode: | ||
if !bytes.Equal(testAsmExpected.code[i].(opCode).hex(), code.(opCode).hex()) { | ||
t.Fatal("There is an error in Opcode during analysis") | ||
} | ||
case Data: | ||
if !bytes.Equal(testAsmExpected.code[i].(Data).hex(), code.(Data).hex()) { | ||
t.Fatal("There is an error in Data during analysis") | ||
} | ||
} | ||
|
||
} | ||
} | ||
|
||
func TestAssemble_invalid(t *testing.T) { | ||
rawByteCode := make([]byte, 0) | ||
rawByteCode = append(rawByteCode, uint8(opcode.Push)) | ||
rawByteCode = append(rawByteCode, uint32To4Bytes(1)...) | ||
rawByteCode = append(rawByteCode, uint8(255)) // invaild code | ||
rawByteCode = append(rawByteCode, uint32To4Bytes(300)...) | ||
rawByteCode = append(rawByteCode, uint8(opcode.Add)) | ||
|
||
_, err := assemble(rawByteCode) | ||
if err == nil { | ||
t.Error("The desired error was not found ") | ||
} | ||
} | ||
|
||
func TestNext(t *testing.T) { | ||
rawByteCode := []byte{ | ||
uint8(opcode.Push), 1, | ||
uint8(opcode.Push), 2, | ||
uint8(opcode.Add), | ||
} | ||
|
||
asm, err := assemble(rawByteCode) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
for asm.pc+1 < uint64(len(asm.code)) { | ||
code := asm.next() | ||
_, ok := code.(hexer) | ||
if !ok { | ||
t.Errorf("Error at %d", asm.pc-1) | ||
} | ||
} | ||
|
||
if asm.next() != nil { | ||
t.Error("Error at last index") | ||
} | ||
} | ||
|
||
func TestJump(t *testing.T) { | ||
rawByteCode := []byte{ | ||
uint8(opcode.Push), 1, | ||
uint8(opcode.Push), 2, | ||
uint8(opcode.Add), | ||
} | ||
|
||
asm, err := assemble(rawByteCode) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
defer func() { | ||
if r := recover(); r == nil { | ||
t.Fatal("Access to in valid program counter!") | ||
} | ||
}() | ||
asm.jump(1) | ||
asm.jump(2) | ||
asm.jump(4) | ||
asm.jump(10) // Error occur | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters