golang.org/x/tools@v0.21.0/cmd/stringer/util_test.go (about)

     1  // Copyright 2014 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  // This file contains tests for some of the internal functions.
     6  
     7  package main
     8  
     9  import (
    10  	"fmt"
    11  	"testing"
    12  )
    13  
    14  // Helpers to save typing in the test cases.
    15  type u []uint64
    16  type uu [][]uint64
    17  
    18  type SplitTest struct {
    19  	input  u
    20  	output uu
    21  	signed bool
    22  }
    23  
    24  var (
    25  	m2  = uint64(2)
    26  	m1  = uint64(1)
    27  	m0  = uint64(0)
    28  	m_1 = ^uint64(0)     // -1 when signed.
    29  	m_2 = ^uint64(0) - 1 // -2 when signed.
    30  )
    31  
    32  var splitTests = []SplitTest{
    33  	// No need for a test for the empty case; that's picked off before splitIntoRuns.
    34  	// Single value.
    35  	{u{1}, uu{u{1}}, false},
    36  	// Out of order.
    37  	{u{3, 2, 1}, uu{u{1, 2, 3}}, true},
    38  	// Out of order.
    39  	{u{3, 2, 1}, uu{u{1, 2, 3}}, false},
    40  	// A gap at the beginning.
    41  	{u{1, 33, 32, 31}, uu{u{1}, u{31, 32, 33}}, true},
    42  	// A gap in the middle, in mixed order.
    43  	{u{33, 7, 32, 31, 9, 8}, uu{u{7, 8, 9}, u{31, 32, 33}}, true},
    44  	// Gaps throughout
    45  	{u{33, 44, 1, 32, 45, 31}, uu{u{1}, u{31, 32, 33}, u{44, 45}}, true},
    46  	// Unsigned values spanning 0.
    47  	{u{m1, m0, m_1, m2, m_2}, uu{u{m0, m1, m2}, u{m_2, m_1}}, false},
    48  	// Signed values spanning 0
    49  	{u{m1, m0, m_1, m2, m_2}, uu{u{m_2, m_1, m0, m1, m2}}, true},
    50  }
    51  
    52  func TestSplitIntoRuns(t *testing.T) {
    53  Outer:
    54  	for n, test := range splitTests {
    55  		values := make([]Value, len(test.input))
    56  		for i, v := range test.input {
    57  			values[i] = Value{"", "", v, test.signed, fmt.Sprint(v)}
    58  		}
    59  		runs := splitIntoRuns(values)
    60  		if len(runs) != len(test.output) {
    61  			t.Errorf("#%d: %v: got %d runs; expected %d", n, test.input, len(runs), len(test.output))
    62  			continue
    63  		}
    64  		for i, run := range runs {
    65  			if len(run) != len(test.output[i]) {
    66  				t.Errorf("#%d: got %v; expected %v", n, runs, test.output)
    67  				continue Outer
    68  			}
    69  			for j, v := range run {
    70  				if v.value != test.output[i][j] {
    71  					t.Errorf("#%d: got %v; expected %v", n, runs, test.output)
    72  					continue Outer
    73  				}
    74  			}
    75  		}
    76  	}
    77  }