-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement assemble func prototype #72
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
/* | ||
* 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, uint32ToBytes(1)...) // 1 byte allocation | ||
rawByteCode = append(rawByteCode, uint8(opcode.Push)) | ||
rawByteCode = append(rawByteCode, uint32ToBytes(300)...) // 2 byte allocation | ||
rawByteCode = append(rawByteCode, uint8(opcode.Add)) | ||
|
||
testAsmExpected := asm{ | ||
code: []hexer{ | ||
push{}, Data{Body: uint32ToBytes(1)}, | ||
push{}, Data{Body: uint32ToBytes(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, uint32ToBytes(1)...) | ||
rawByteCode = append(rawByteCode, uint8(255)) // invaild code | ||
rawByteCode = append(rawByteCode, uint32ToBytes(300)...) | ||
rawByteCode = append(rawByteCode, uint8(opcode.Add)) | ||
|
||
_, err := assemble(rawByteCode) | ||
if err != ErrInvalidOpcode { | ||
t.Error("The desired error was not found") | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be better to compare like this
|
||
} | ||
|
||
func TestNext(t *testing.T) { | ||
rawByteCode := make([]byte, 0) | ||
rawByteCode = append(rawByteCode, uint8(opcode.Push)) | ||
rawByteCode = append(rawByteCode, uint32ToBytes(1)...) | ||
rawByteCode = append(rawByteCode, uint8(opcode.Push)) | ||
rawByteCode = append(rawByteCode, uint32ToBytes(2)...) | ||
rawByteCode = append(rawByteCode, uint8(opcode.Add)) | ||
|
||
asm, err := assemble(rawByteCode) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
for asm.pc+1 < uint64(len(asm.code)) { | ||
prevPc := asm.pc | ||
code := asm.next() | ||
if prevPc+1 != asm.pc { | ||
t.Error("Error in moving pc") | ||
} | ||
|
||
_, 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 := make([]byte, 0) | ||
rawByteCode = append(rawByteCode, uint8(opcode.Push)) | ||
rawByteCode = append(rawByteCode, uint32ToBytes(1)...) | ||
rawByteCode = append(rawByteCode, uint8(opcode.Push)) | ||
rawByteCode = append(rawByteCode, uint32ToBytes(2)...) | ||
rawByteCode = append(rawByteCode, uint8(opcode.Add)) | ||
|
||
asm, err := assemble(rawByteCode) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
defer func() { | ||
if r := recover(); r != nil { | ||
t.Fatal("Access to invalid program counter!") | ||
} | ||
}() | ||
asm.jump(1) | ||
if asm.pc != 1 { | ||
t.Errorf("Invalid pc - expected=1, got=%d", asm.pc) | ||
} | ||
|
||
asm.jump(2) | ||
if asm.pc != 2 { | ||
t.Errorf("Invalid pc - expected=2, got=%d", asm.pc) | ||
} | ||
|
||
asm.jump(4) | ||
if asm.pc != 4 { | ||
t.Errorf("Invalid pc - expected=4, got=%d", asm.pc) | ||
} | ||
} | ||
|
||
func TestJump_invalid(t *testing.T) { | ||
rawByteCode := make([]byte, 0) | ||
rawByteCode = append(rawByteCode, uint8(opcode.Push)) | ||
rawByteCode = append(rawByteCode, uint32ToBytes(1)...) | ||
rawByteCode = append(rawByteCode, uint8(opcode.Push)) | ||
rawByteCode = append(rawByteCode, uint32ToBytes(2)...) | ||
rawByteCode = append(rawByteCode, uint8(opcode.Add)) | ||
|
||
asm, err := assemble(rawByteCode) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
defer func() { | ||
if r := recover(); r == nil { | ||
t.Fatal("The desired error was not found") | ||
} | ||
}() | ||
asm.jump(15) | ||
asm.jump(16) | ||
asm.jump(17) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is important to distinguish between cases where err occurs and cases that do not occur. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's better to predefine
var ErrInvalidOpcode = errors.New("Invalid byteCode")
and returnErrInvalidOpcode