github.com/zebozhuang/go@v0.0.0-20200207033046-f8a98f6f5c5d/src/cmd/compile/internal/gc/swt_test.go (about) 1 // Copyright 2015 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package gc 6 7 import ( 8 "testing" 9 ) 10 11 func nodrune(r rune) *Node { 12 v := new(Mpint) 13 v.SetInt64(int64(r)) 14 v.Rune = true 15 return nodlit(Val{v}) 16 } 17 18 func nodflt(f float64) *Node { 19 v := new(Mpflt) 20 v.SetFloat64(f) 21 return nodlit(Val{v}) 22 } 23 24 func TestCaseClauseByConstVal(t *testing.T) { 25 tests := []struct { 26 a, b *Node 27 }{ 28 // CTFLT 29 {nodflt(0.1), nodflt(0.2)}, 30 // CTINT 31 {nodintconst(0), nodintconst(1)}, 32 // CTRUNE 33 {nodrune('a'), nodrune('b')}, 34 // CTSTR 35 {nodlit(Val{"ab"}), nodlit(Val{"abc"})}, 36 {nodlit(Val{"ab"}), nodlit(Val{"xyz"})}, 37 {nodlit(Val{"abc"}), nodlit(Val{"xyz"})}, 38 } 39 for i, test := range tests { 40 a := caseClause{node: nod(OXXX, test.a, nil)} 41 b := caseClause{node: nod(OXXX, test.b, nil)} 42 s := caseClauseByConstVal{a, b} 43 if less := s.Less(0, 1); !less { 44 t.Errorf("%d: caseClauseByConstVal(%v, %v) = false", i, test.a, test.b) 45 } 46 if less := s.Less(1, 0); less { 47 t.Errorf("%d: caseClauseByConstVal(%v, %v) = true", i, test.a, test.b) 48 } 49 } 50 }