modernc.org/strutil@v1.1.3/all_test.go (about)

     1  // Copyright (c) 2014 The sortutil 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 strutil // import "modernc.org/strutil"
     6  
     7  import (
     8  	"bytes"
     9  	"fmt"
    10  	"math"
    11  	"modernc.org/mathutil"
    12  	"os"
    13  	"path"
    14  	"runtime"
    15  	"strings"
    16  	"testing"
    17  	"unsafe"
    18  )
    19  
    20  func caller(s string, va ...interface{}) {
    21  	_, fn, fl, _ := runtime.Caller(2)
    22  	fmt.Fprintf(os.Stderr, "caller: %s:%d: ", path.Base(fn), fl)
    23  	fmt.Fprintf(os.Stderr, s, va...)
    24  	fmt.Fprintln(os.Stderr)
    25  	_, fn, fl, _ = runtime.Caller(1)
    26  	fmt.Fprintf(os.Stderr, "\tcallee: %s:%d: ", path.Base(fn), fl)
    27  	fmt.Fprintln(os.Stderr)
    28  }
    29  
    30  func dbg(s string, va ...interface{}) {
    31  	if s == "" {
    32  		s = strings.Repeat("%v ", len(va))
    33  	}
    34  	_, fn, fl, _ := runtime.Caller(1)
    35  	fmt.Fprintf(os.Stderr, "dbg %s:%d: ", path.Base(fn), fl)
    36  	fmt.Fprintf(os.Stderr, s, va...)
    37  	fmt.Fprintln(os.Stderr)
    38  }
    39  
    40  func TODO(...interface{}) string { //TODOOK
    41  	_, fn, fl, _ := runtime.Caller(1)
    42  	return fmt.Sprintf("TODO: %s:%d:\n", path.Base(fn), fl) //TODOOK
    43  }
    44  
    45  func use(...interface{}) {}
    46  
    47  func init() {
    48  	use(caller, dbg, TODO) //TODOOK
    49  }
    50  
    51  func TestBase64(t *testing.T) {
    52  	const max = 768
    53  	r, err := mathutil.NewFC32(math.MinInt32, math.MaxInt32, true)
    54  	if err != nil {
    55  		t.Fatal(err)
    56  	}
    57  
    58  	bin := []byte{}
    59  	for i := 0; i < max; i++ {
    60  		bin = append(bin, byte(r.Next()))
    61  		cmp, err := Base64Decode(Base64Encode(bin))
    62  		if err != nil {
    63  			t.Fatal(err)
    64  		}
    65  
    66  		if !bytes.Equal(bin, cmp) {
    67  			t.Fatalf("a: % x\nb: % x", bin, cmp)
    68  		}
    69  	}
    70  }
    71  
    72  func TestBase32Ext(t *testing.T) {
    73  	const max = 640
    74  	r, err := mathutil.NewFC32(math.MinInt32, math.MaxInt32, true)
    75  	if err != nil {
    76  		t.Fatal(err)
    77  	}
    78  
    79  	bin := []byte{}
    80  	for i := 0; i < max; i++ {
    81  		bin = append(bin, byte(r.Next()))
    82  		cmp, err := Base32ExtDecode(Base32ExtEncode(bin))
    83  		if err != nil {
    84  			t.Fatal(err)
    85  		}
    86  
    87  		if !bytes.Equal(bin, cmp) {
    88  			t.Fatalf("a: % x\nb: % x", bin, cmp)
    89  		}
    90  	}
    91  }
    92  
    93  func TestFields(t *testing.T) {
    94  	p := []string{"", "\\", "|", "0", "1", "2"}
    95  	one := func(n int) string {
    96  		s := ""
    97  		for i := 0; i < 3; i++ {
    98  			s += p[n%len(p)]
    99  			n /= len(p)
   100  		}
   101  		return s
   102  	}
   103  	max := len(p) * len(p) * len(p)
   104  	var a [3]string
   105  	for x := 0; x < max; x++ {
   106  		a[0] = one(x)
   107  		for x := 0; x < max; x++ {
   108  			a[1] = one(x)
   109  			for x := 0; x < len(p)*len(p); x++ {
   110  				a[2] = one(x)
   111  				enc := JoinFields(a[:], "|")
   112  				dec := SplitFields(enc, "|")
   113  				if g, e := strings.Join(dec, ","), strings.Join(a[:], ","); g != e {
   114  					t.Fatal(g, e)
   115  				}
   116  			}
   117  		}
   118  	}
   119  }
   120  
   121  func ExamplePrettyString() {
   122  	type prettyStringType struct {
   123  		Array         [3]int
   124  		Bool          bool
   125  		Chan          <-chan int
   126  		Complex128    complex128
   127  		Complex64     complex64
   128  		Float32       float32
   129  		Float64       float64
   130  		Func          func()
   131  		Func2         interface{}
   132  		Func3         interface{}
   133  		Int           int
   134  		Int16         int16
   135  		Int32         int32
   136  		Int64         int64
   137  		Int8          int8
   138  		Interface     interface{}
   139  		Map           map[int]string
   140  		Ptr           *int
   141  		Slice         []int
   142  		String        string
   143  		Struct        *prettyStringType
   144  		Struct2       *prettyStringType
   145  		Uint          uint
   146  		Uint16        uint16
   147  		Uint32        uint32
   148  		Uint64        uint64
   149  		Uint8         byte
   150  		Uintptr       uintptr
   151  		UnsafePointer unsafe.Pointer
   152  	}
   153  
   154  	i := 314
   155  	v := &prettyStringType{
   156  		Array:         [...]int{10, 20, 30},
   157  		Bool:          true,
   158  		Chan:          make(<-chan int, 100),
   159  		Complex128:    3 - 4i,
   160  		Complex64:     1 + 2i,
   161  		Float32:       1.5,
   162  		Float64:       3.5,
   163  		Func2:         func(a, b, c int, z ...string) (d, e, f string) { return },
   164  		Func3:         func(a, b, c int, z ...string) {},
   165  		Func:          func() {},
   166  		Int16:         -44,
   167  		Int32:         -45,
   168  		Int64:         -46,
   169  		Int8:          -43,
   170  		Int:           -42,
   171  		Map:           map[int]string{100: "100", 200: "200", 300: "300"},
   172  		Ptr:           &i,
   173  		Slice:         []int{10, 20, 30},
   174  		String:        "foo",
   175  		Struct:        &prettyStringType{Int: 8888},
   176  		Struct2:       &prettyStringType{},
   177  		Uint16:        44,
   178  		Uint32:        45,
   179  		Uint64:        46,
   180  		Uint8:         43,
   181  		Uint:          42,
   182  		Uintptr:       uintptr(99),
   183  		UnsafePointer: unsafe.Pointer(uintptr(0x12345678)),
   184  	}
   185  	v.Interface = v
   186  	fmt.Println(PrettyString(v, "", "", nil))
   187  	// Output:
   188  	// &strutil.prettyStringType{
   189  	// · Array: [3]int{
   190  	// · · 0: 10,
   191  	// · · 1: 20,
   192  	// · · 2: 30,
   193  	// · },
   194  	// · Bool: true,
   195  	// · Chan: <-chan int// capacity: 100,
   196  	// · Complex128: (3-4i),
   197  	// · Complex64: (1+2i),
   198  	// · Float32: 1.5,
   199  	// · Float64: 3.5,
   200  	// · Func: func() { ... },
   201  	// · Func2: func(int, int, int, ...string) (string, string, string) { ... },
   202  	// · Func3: func(int, int, int, ...string) { ... },
   203  	// · Int: -42,
   204  	// · Int16: -44,
   205  	// · Int32: -45,
   206  	// · Int64: -46,
   207  	// · Int8: -43,
   208  	// · Interface: &strutil.prettyStringType{ /* recursive/repetitive pointee not shown */ },
   209  	// · Map: map[int]string{
   210  	// · · 100: "100",
   211  	// · · 200: "200",
   212  	// · · 300: "300",
   213  	// · },
   214  	// · Ptr: &314,
   215  	// · Slice: []int{ // len 3
   216  	// · · 0: 10,
   217  	// · · 1: 20,
   218  	// · · 2: 30,
   219  	// · },
   220  	// · String: "foo",
   221  	// · Struct: &strutil.prettyStringType{
   222  	// · · Int: 8888,
   223  	// · },
   224  	// · Uint: 42,
   225  	// · Uint16: 44,
   226  	// · Uint32: 45,
   227  	// · Uint64: 46,
   228  	// · Uint8: 43,
   229  	// · Uintptr: 99,
   230  	// · UnsafePointer: 0x12345678,
   231  	// }
   232  }
   233  
   234  func TestGopath(t *testing.T) {
   235  	gp := Gopath()
   236  	if gp == "" {
   237  		t.Fatal("empty GOPATH")
   238  	}
   239  
   240  	t.Log(gp)
   241  }
   242  
   243  func TestImportPath(t *testing.T) {
   244  	ip, err := ImportPath()
   245  	if err != nil {
   246  		t.Fatal(err)
   247  	}
   248  
   249  	if ip == "" {
   250  		t.Fatal("empty import path")
   251  	}
   252  
   253  	t.Log(ip)
   254  }