Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
itsubaki committed Jul 29, 2023
1 parent b4fbe68 commit dcf03b8
Showing 1 changed file with 47 additions and 1 deletion.
48 changes: 47 additions & 1 deletion evaluator/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,35 @@ import (
"github.com/itsubaki/qasm/evaluator"
)

func ExampleQubit_All() {
func ExampleBit() {
b := &evaluator.Bit{
Name: make([]string, 0),
Value: make(map[string][]int64),
}
b.Add(&ast.IdentExpr{Name: "a"}, []int64{1, 2, 3})
b.Add(&ast.IdentExpr{Name: "b"}, []int64{4, 5, 6})
fmt.Println(b)

fmt.Println(b.Get(&ast.IndexExpr{Name: "c"}))
fmt.Println(b.Get(&ast.IndexExpr{Name: "c", Value: "1"}))
fmt.Println(b.Get(&ast.IndexExpr{Name: "a", Value: "0"}))
fmt.Println(b.Get(&ast.IndexExpr{Name: "b", Value: "-1"}))
fmt.Println(b.Get(&ast.IndexExpr{Name: "b", Value: "10"}))
fmt.Println(b.Get(&ast.IndexExpr{Name: "b", Value: "-10"}))
fmt.Println(b.Get(&ast.BadExpr{}))

// Output:
// [a b], map[a:[1 2 3] b:[4 5 6]]
// [] false
// [] false
// [1] true
// [6] true
// [] false
// [] false
// [] false
}

func ExampleQubit() {
qb := &evaluator.Qubit{
Name: make([]string, 0),
Value: make(map[string][]q.Qubit),
Expand All @@ -18,8 +46,26 @@ func ExampleQubit_All() {
qb.Add(&ast.GenDecl{Name: "q1"}, []q.Qubit{4, 5, 6})
qb.Add(&ast.GenDecl{Name: "q2"}, []q.Qubit{7, 8, 9})

fmt.Println(qb)
fmt.Println(qb.All())
fmt.Println(qb.Get(&ast.IndexExpr{Name: "c"}))
fmt.Println(qb.Get(&ast.IndexExpr{Name: "q0", Value: "10"}))
fmt.Println(qb.Get(&ast.IndexExpr{Name: "q0", Value: "-10"}))
fmt.Println(qb.Get(&ast.BadExpr{}))

// Output:
// [q0 q1 q2], map[q0:[1 2 3] q1:[4 5 6] q2:[7 8 9]]
// [1 2 3 4 5 6 7 8 9] <nil>
// [] false
// [] false
// [] false
// [] false
}

func ExampleEnviron() {
env := evaluator.NewEnviron()
fmt.Println(env)

// Output:
// gatedef: map[], const: map[], bit: [], map[], qubit: [], map[], modifier: [], decl: []
}

0 comments on commit dcf03b8

Please sign in to comment.