github.com/q45/go@v0.0.0-20151101211701-a4fb8c13db3f/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  	"cmd/compile/internal/big"
     9  	"testing"
    10  )
    11  
    12  func TestExprcmp(t *testing.T) {
    13  	testdata := []struct {
    14  		a, b caseClause
    15  		want int
    16  	}{
    17  		// Non-constants.
    18  		{
    19  			caseClause{node: Nod(OXXX, nil, nil), typ: caseKindExprVar},
    20  			caseClause{node: Nod(OXXX, nil, nil), typ: caseKindExprConst},
    21  			+1,
    22  		},
    23  		{
    24  			caseClause{node: Nod(OXXX, nil, nil), typ: caseKindExprConst},
    25  			caseClause{node: Nod(OXXX, nil, nil), typ: caseKindExprVar},
    26  			-1,
    27  		},
    28  		// Type switches
    29  		{
    30  			caseClause{node: Nod(OXXX, Nodintconst(0), nil), typ: caseKindExprConst},
    31  			caseClause{node: Nod(OXXX, Nodbool(true), nil), typ: caseKindExprConst},
    32  			-1,
    33  		},
    34  		{
    35  			caseClause{node: Nod(OXXX, Nodbool(true), nil), typ: caseKindExprConst},
    36  			caseClause{node: Nod(OXXX, Nodintconst(1), nil), typ: caseKindExprConst},
    37  			+1,
    38  		},
    39  		{
    40  			caseClause{node: Nod(OXXX, &Node{Type: &Type{Etype: TBOOL, Vargen: 1}}, nil), typ: caseKindExprConst},
    41  			caseClause{node: Nod(OXXX, &Node{Type: &Type{Etype: TINT, Vargen: 0}}, nil), typ: caseKindExprConst},
    42  			+1,
    43  		},
    44  		{
    45  			caseClause{node: Nod(OXXX, &Node{Type: &Type{Etype: TBOOL, Vargen: 1}}, nil), typ: caseKindExprConst},
    46  			caseClause{node: Nod(OXXX, &Node{Type: &Type{Etype: TINT, Vargen: 1}}, nil), typ: caseKindExprConst},
    47  			-1,
    48  		},
    49  		{
    50  			caseClause{node: Nod(OXXX, &Node{Type: &Type{Etype: TBOOL, Vargen: 0}}, nil), typ: caseKindExprConst},
    51  			caseClause{node: Nod(OXXX, &Node{Type: &Type{Etype: TINT, Vargen: 1}}, nil), typ: caseKindExprConst},
    52  			-1,
    53  		},
    54  		// Constant values.
    55  		// CTFLT
    56  		{
    57  			caseClause{node: Nod(OXXX, nodlit(Val{&Mpflt{Val: *big.NewFloat(0.1)}}), nil), typ: caseKindExprConst},
    58  			caseClause{node: Nod(OXXX, nodlit(Val{&Mpflt{Val: *big.NewFloat(0.2)}}), nil), typ: caseKindExprConst},
    59  			-1,
    60  		},
    61  		{
    62  			caseClause{node: Nod(OXXX, nodlit(Val{&Mpflt{Val: *big.NewFloat(0.1)}}), nil), typ: caseKindExprConst},
    63  			caseClause{node: Nod(OXXX, nodlit(Val{&Mpflt{Val: *big.NewFloat(0.1)}}), nil), typ: caseKindExprConst},
    64  			0,
    65  		},
    66  		{
    67  			caseClause{node: Nod(OXXX, nodlit(Val{&Mpflt{Val: *big.NewFloat(0.2)}}), nil), typ: caseKindExprConst},
    68  			caseClause{node: Nod(OXXX, nodlit(Val{&Mpflt{Val: *big.NewFloat(0.1)}}), nil), typ: caseKindExprConst},
    69  			+1,
    70  		},
    71  		// CTINT
    72  		{
    73  			caseClause{node: Nod(OXXX, Nodintconst(0), nil), typ: caseKindExprConst},
    74  			caseClause{node: Nod(OXXX, Nodintconst(1), nil), typ: caseKindExprConst},
    75  			-1,
    76  		},
    77  		{
    78  			caseClause{node: Nod(OXXX, Nodintconst(1), nil), typ: caseKindExprConst},
    79  			caseClause{node: Nod(OXXX, Nodintconst(1), nil), typ: caseKindExprConst},
    80  			0,
    81  		},
    82  		{
    83  			caseClause{node: Nod(OXXX, Nodintconst(1), nil), typ: caseKindExprConst},
    84  			caseClause{node: Nod(OXXX, Nodintconst(0), nil), typ: caseKindExprConst},
    85  			+1,
    86  		},
    87  		// CTRUNE
    88  		{
    89  			caseClause{node: Nod(OXXX, nodlit(Val{&Mpint{Val: *big.NewInt('a'), Rune: true}}), nil), typ: caseKindExprConst},
    90  			caseClause{node: Nod(OXXX, nodlit(Val{&Mpint{Val: *big.NewInt('b'), Rune: true}}), nil), typ: caseKindExprConst},
    91  			-1,
    92  		},
    93  		{
    94  			caseClause{node: Nod(OXXX, nodlit(Val{&Mpint{Val: *big.NewInt('b'), Rune: true}}), nil), typ: caseKindExprConst},
    95  			caseClause{node: Nod(OXXX, nodlit(Val{&Mpint{Val: *big.NewInt('b'), Rune: true}}), nil), typ: caseKindExprConst},
    96  			0,
    97  		},
    98  		{
    99  			caseClause{node: Nod(OXXX, nodlit(Val{&Mpint{Val: *big.NewInt('b'), Rune: true}}), nil), typ: caseKindExprConst},
   100  			caseClause{node: Nod(OXXX, nodlit(Val{&Mpint{Val: *big.NewInt('a'), Rune: true}}), nil), typ: caseKindExprConst},
   101  			+1,
   102  		},
   103  		// CTSTR
   104  		{
   105  			caseClause{node: Nod(OXXX, nodlit(Val{"ab"}), nil), typ: caseKindExprConst},
   106  			caseClause{node: Nod(OXXX, nodlit(Val{"abc"}), nil), typ: caseKindExprConst},
   107  			-1,
   108  		},
   109  		{
   110  			caseClause{node: Nod(OXXX, nodlit(Val{"abc"}), nil), typ: caseKindExprConst},
   111  			caseClause{node: Nod(OXXX, nodlit(Val{"xyz"}), nil), typ: caseKindExprConst},
   112  			-1,
   113  		},
   114  		{
   115  			caseClause{node: Nod(OXXX, nodlit(Val{"abc"}), nil), typ: caseKindExprConst},
   116  			caseClause{node: Nod(OXXX, nodlit(Val{"abc"}), nil), typ: caseKindExprConst},
   117  			0,
   118  		},
   119  		{
   120  			caseClause{node: Nod(OXXX, nodlit(Val{"abc"}), nil), typ: caseKindExprConst},
   121  			caseClause{node: Nod(OXXX, nodlit(Val{"ab"}), nil), typ: caseKindExprConst},
   122  			+1,
   123  		},
   124  		{
   125  			caseClause{node: Nod(OXXX, nodlit(Val{"xyz"}), nil), typ: caseKindExprConst},
   126  			caseClause{node: Nod(OXXX, nodlit(Val{"abc"}), nil), typ: caseKindExprConst},
   127  			+1,
   128  		},
   129  		// Everything else should compare equal.
   130  		{
   131  			caseClause{node: Nod(OXXX, nodnil(), nil), typ: caseKindExprConst},
   132  			caseClause{node: Nod(OXXX, nodnil(), nil), typ: caseKindExprConst},
   133  			0,
   134  		},
   135  	}
   136  	for i, d := range testdata {
   137  		got := exprcmp(&d.a, &d.b)
   138  		if d.want != got {
   139  			t.Errorf("%d: exprcmp(a, b) = %d; want %d", i, got, d.want)
   140  			t.Logf("\ta = caseClause{node: %#v, typ: %#v}", d.a.node, d.a.typ)
   141  			t.Logf("\tb = caseClause{node: %#v, typ: %#v}", d.b.node, d.b.typ)
   142  		}
   143  	}
   144  }