github.com/riscv/riscv-go@v0.0.0-20200123204226-124ebd6fcc8e/src/math/big/intconv_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 big
     6  
     7  import (
     8  	"bytes"
     9  	"fmt"
    10  	"testing"
    11  )
    12  
    13  var stringTests = []struct {
    14  	in   string
    15  	out  string
    16  	base int
    17  	val  int64
    18  	ok   bool
    19  }{
    20  	{in: ""},
    21  	{in: "a"},
    22  	{in: "z"},
    23  	{in: "+"},
    24  	{in: "-"},
    25  	{in: "0b"},
    26  	{in: "0x"},
    27  	{in: "2", base: 2},
    28  	{in: "0b2", base: 0},
    29  	{in: "08"},
    30  	{in: "8", base: 8},
    31  	{in: "0xg", base: 0},
    32  	{in: "g", base: 16},
    33  	{"0", "0", 0, 0, true},
    34  	{"0", "0", 10, 0, true},
    35  	{"0", "0", 16, 0, true},
    36  	{"+0", "0", 0, 0, true},
    37  	{"-0", "0", 0, 0, true},
    38  	{"10", "10", 0, 10, true},
    39  	{"10", "10", 10, 10, true},
    40  	{"10", "10", 16, 16, true},
    41  	{"-10", "-10", 16, -16, true},
    42  	{"+10", "10", 16, 16, true},
    43  	{"0x10", "16", 0, 16, true},
    44  	{in: "0x10", base: 16},
    45  	{"-0x10", "-16", 0, -16, true},
    46  	{"+0x10", "16", 0, 16, true},
    47  	{"00", "0", 0, 0, true},
    48  	{"0", "0", 8, 0, true},
    49  	{"07", "7", 0, 7, true},
    50  	{"7", "7", 8, 7, true},
    51  	{"023", "19", 0, 19, true},
    52  	{"23", "23", 8, 19, true},
    53  	{"cafebabe", "cafebabe", 16, 0xcafebabe, true},
    54  	{"0b0", "0", 0, 0, true},
    55  	{"-111", "-111", 2, -7, true},
    56  	{"-0b111", "-7", 0, -7, true},
    57  	{"0b1001010111", "599", 0, 0x257, true},
    58  	{"1001010111", "1001010111", 2, 0x257, true},
    59  }
    60  
    61  func TestIntText(t *testing.T) {
    62  	z := new(Int)
    63  	for _, test := range stringTests {
    64  		if !test.ok {
    65  			continue
    66  		}
    67  
    68  		_, ok := z.SetString(test.in, test.base)
    69  		if !ok {
    70  			t.Errorf("%v: failed to parse", test)
    71  			continue
    72  		}
    73  
    74  		base := test.base
    75  		if base == 0 {
    76  			base = 10
    77  		}
    78  
    79  		if got := z.Text(base); got != test.out {
    80  			t.Errorf("%v: got %s; want %s", test, got, test.out)
    81  		}
    82  	}
    83  }
    84  
    85  func TestAppendText(t *testing.T) {
    86  	z := new(Int)
    87  	var buf []byte
    88  	for _, test := range stringTests {
    89  		if !test.ok {
    90  			continue
    91  		}
    92  
    93  		_, ok := z.SetString(test.in, test.base)
    94  		if !ok {
    95  			t.Errorf("%v: failed to parse", test)
    96  			continue
    97  		}
    98  
    99  		base := test.base
   100  		if base == 0 {
   101  			base = 10
   102  		}
   103  
   104  		i := len(buf)
   105  		buf = z.Append(buf, base)
   106  		if got := string(buf[i:]); got != test.out {
   107  			t.Errorf("%v: got %s; want %s", test, got, test.out)
   108  		}
   109  	}
   110  }
   111  
   112  func format(base int) string {
   113  	switch base {
   114  	case 2:
   115  		return "%b"
   116  	case 8:
   117  		return "%o"
   118  	case 16:
   119  		return "%x"
   120  	}
   121  	return "%d"
   122  }
   123  
   124  func TestGetString(t *testing.T) {
   125  	z := new(Int)
   126  	for i, test := range stringTests {
   127  		if !test.ok {
   128  			continue
   129  		}
   130  		z.SetInt64(test.val)
   131  
   132  		if test.base == 10 {
   133  			if got := z.String(); got != test.out {
   134  				t.Errorf("#%da got %s; want %s", i, got, test.out)
   135  			}
   136  		}
   137  
   138  		if got := fmt.Sprintf(format(test.base), z); got != test.out {
   139  			t.Errorf("#%db got %s; want %s", i, got, test.out)
   140  		}
   141  	}
   142  }
   143  
   144  func TestSetString(t *testing.T) {
   145  	tmp := new(Int)
   146  	for i, test := range stringTests {
   147  		// initialize to a non-zero value so that issues with parsing
   148  		// 0 are detected
   149  		tmp.SetInt64(1234567890)
   150  		n1, ok1 := new(Int).SetString(test.in, test.base)
   151  		n2, ok2 := tmp.SetString(test.in, test.base)
   152  		expected := NewInt(test.val)
   153  		if ok1 != test.ok || ok2 != test.ok {
   154  			t.Errorf("#%d (input '%s') ok incorrect (should be %t)", i, test.in, test.ok)
   155  			continue
   156  		}
   157  		if !ok1 {
   158  			if n1 != nil {
   159  				t.Errorf("#%d (input '%s') n1 != nil", i, test.in)
   160  			}
   161  			continue
   162  		}
   163  		if !ok2 {
   164  			if n2 != nil {
   165  				t.Errorf("#%d (input '%s') n2 != nil", i, test.in)
   166  			}
   167  			continue
   168  		}
   169  
   170  		if ok1 && !isNormalized(n1) {
   171  			t.Errorf("#%d (input '%s'): %v is not normalized", i, test.in, *n1)
   172  		}
   173  		if ok2 && !isNormalized(n2) {
   174  			t.Errorf("#%d (input '%s'): %v is not normalized", i, test.in, *n2)
   175  		}
   176  
   177  		if n1.Cmp(expected) != 0 {
   178  			t.Errorf("#%d (input '%s') got: %s want: %d", i, test.in, n1, test.val)
   179  		}
   180  		if n2.Cmp(expected) != 0 {
   181  			t.Errorf("#%d (input '%s') got: %s want: %d", i, test.in, n2, test.val)
   182  		}
   183  	}
   184  }
   185  
   186  var formatTests = []struct {
   187  	input  string
   188  	format string
   189  	output string
   190  }{
   191  	{"<nil>", "%x", "<nil>"},
   192  	{"<nil>", "%#x", "<nil>"},
   193  	{"<nil>", "%#y", "%!y(big.Int=<nil>)"},
   194  
   195  	{"10", "%b", "1010"},
   196  	{"10", "%o", "12"},
   197  	{"10", "%d", "10"},
   198  	{"10", "%v", "10"},
   199  	{"10", "%x", "a"},
   200  	{"10", "%X", "A"},
   201  	{"-10", "%X", "-A"},
   202  	{"10", "%y", "%!y(big.Int=10)"},
   203  	{"-10", "%y", "%!y(big.Int=-10)"},
   204  
   205  	{"10", "%#b", "1010"},
   206  	{"10", "%#o", "012"},
   207  	{"10", "%#d", "10"},
   208  	{"10", "%#v", "10"},
   209  	{"10", "%#x", "0xa"},
   210  	{"10", "%#X", "0XA"},
   211  	{"-10", "%#X", "-0XA"},
   212  	{"10", "%#y", "%!y(big.Int=10)"},
   213  	{"-10", "%#y", "%!y(big.Int=-10)"},
   214  
   215  	{"1234", "%d", "1234"},
   216  	{"1234", "%3d", "1234"},
   217  	{"1234", "%4d", "1234"},
   218  	{"-1234", "%d", "-1234"},
   219  	{"1234", "% 5d", " 1234"},
   220  	{"1234", "%+5d", "+1234"},
   221  	{"1234", "%-5d", "1234 "},
   222  	{"1234", "%x", "4d2"},
   223  	{"1234", "%X", "4D2"},
   224  	{"-1234", "%3x", "-4d2"},
   225  	{"-1234", "%4x", "-4d2"},
   226  	{"-1234", "%5x", " -4d2"},
   227  	{"-1234", "%-5x", "-4d2 "},
   228  	{"1234", "%03d", "1234"},
   229  	{"1234", "%04d", "1234"},
   230  	{"1234", "%05d", "01234"},
   231  	{"1234", "%06d", "001234"},
   232  	{"-1234", "%06d", "-01234"},
   233  	{"1234", "%+06d", "+01234"},
   234  	{"1234", "% 06d", " 01234"},
   235  	{"1234", "%-6d", "1234  "},
   236  	{"1234", "%-06d", "1234  "},
   237  	{"-1234", "%-06d", "-1234 "},
   238  
   239  	{"1234", "%.3d", "1234"},
   240  	{"1234", "%.4d", "1234"},
   241  	{"1234", "%.5d", "01234"},
   242  	{"1234", "%.6d", "001234"},
   243  	{"-1234", "%.3d", "-1234"},
   244  	{"-1234", "%.4d", "-1234"},
   245  	{"-1234", "%.5d", "-01234"},
   246  	{"-1234", "%.6d", "-001234"},
   247  
   248  	{"1234", "%8.3d", "    1234"},
   249  	{"1234", "%8.4d", "    1234"},
   250  	{"1234", "%8.5d", "   01234"},
   251  	{"1234", "%8.6d", "  001234"},
   252  	{"-1234", "%8.3d", "   -1234"},
   253  	{"-1234", "%8.4d", "   -1234"},
   254  	{"-1234", "%8.5d", "  -01234"},
   255  	{"-1234", "%8.6d", " -001234"},
   256  
   257  	{"1234", "%+8.3d", "   +1234"},
   258  	{"1234", "%+8.4d", "   +1234"},
   259  	{"1234", "%+8.5d", "  +01234"},
   260  	{"1234", "%+8.6d", " +001234"},
   261  	{"-1234", "%+8.3d", "   -1234"},
   262  	{"-1234", "%+8.4d", "   -1234"},
   263  	{"-1234", "%+8.5d", "  -01234"},
   264  	{"-1234", "%+8.6d", " -001234"},
   265  
   266  	{"1234", "% 8.3d", "    1234"},
   267  	{"1234", "% 8.4d", "    1234"},
   268  	{"1234", "% 8.5d", "   01234"},
   269  	{"1234", "% 8.6d", "  001234"},
   270  	{"-1234", "% 8.3d", "   -1234"},
   271  	{"-1234", "% 8.4d", "   -1234"},
   272  	{"-1234", "% 8.5d", "  -01234"},
   273  	{"-1234", "% 8.6d", " -001234"},
   274  
   275  	{"1234", "%.3x", "4d2"},
   276  	{"1234", "%.4x", "04d2"},
   277  	{"1234", "%.5x", "004d2"},
   278  	{"1234", "%.6x", "0004d2"},
   279  	{"-1234", "%.3x", "-4d2"},
   280  	{"-1234", "%.4x", "-04d2"},
   281  	{"-1234", "%.5x", "-004d2"},
   282  	{"-1234", "%.6x", "-0004d2"},
   283  
   284  	{"1234", "%8.3x", "     4d2"},
   285  	{"1234", "%8.4x", "    04d2"},
   286  	{"1234", "%8.5x", "   004d2"},
   287  	{"1234", "%8.6x", "  0004d2"},
   288  	{"-1234", "%8.3x", "    -4d2"},
   289  	{"-1234", "%8.4x", "   -04d2"},
   290  	{"-1234", "%8.5x", "  -004d2"},
   291  	{"-1234", "%8.6x", " -0004d2"},
   292  
   293  	{"1234", "%+8.3x", "    +4d2"},
   294  	{"1234", "%+8.4x", "   +04d2"},
   295  	{"1234", "%+8.5x", "  +004d2"},
   296  	{"1234", "%+8.6x", " +0004d2"},
   297  	{"-1234", "%+8.3x", "    -4d2"},
   298  	{"-1234", "%+8.4x", "   -04d2"},
   299  	{"-1234", "%+8.5x", "  -004d2"},
   300  	{"-1234", "%+8.6x", " -0004d2"},
   301  
   302  	{"1234", "% 8.3x", "     4d2"},
   303  	{"1234", "% 8.4x", "    04d2"},
   304  	{"1234", "% 8.5x", "   004d2"},
   305  	{"1234", "% 8.6x", "  0004d2"},
   306  	{"1234", "% 8.7x", " 00004d2"},
   307  	{"1234", "% 8.8x", " 000004d2"},
   308  	{"-1234", "% 8.3x", "    -4d2"},
   309  	{"-1234", "% 8.4x", "   -04d2"},
   310  	{"-1234", "% 8.5x", "  -004d2"},
   311  	{"-1234", "% 8.6x", " -0004d2"},
   312  	{"-1234", "% 8.7x", "-00004d2"},
   313  	{"-1234", "% 8.8x", "-000004d2"},
   314  
   315  	{"1234", "%-8.3d", "1234    "},
   316  	{"1234", "%-8.4d", "1234    "},
   317  	{"1234", "%-8.5d", "01234   "},
   318  	{"1234", "%-8.6d", "001234  "},
   319  	{"1234", "%-8.7d", "0001234 "},
   320  	{"1234", "%-8.8d", "00001234"},
   321  	{"-1234", "%-8.3d", "-1234   "},
   322  	{"-1234", "%-8.4d", "-1234   "},
   323  	{"-1234", "%-8.5d", "-01234  "},
   324  	{"-1234", "%-8.6d", "-001234 "},
   325  	{"-1234", "%-8.7d", "-0001234"},
   326  	{"-1234", "%-8.8d", "-00001234"},
   327  
   328  	{"16777215", "%b", "111111111111111111111111"}, // 2**24 - 1
   329  
   330  	{"0", "%.d", ""},
   331  	{"0", "%.0d", ""},
   332  	{"0", "%3.d", ""},
   333  }
   334  
   335  func TestFormat(t *testing.T) {
   336  	for i, test := range formatTests {
   337  		var x *Int
   338  		if test.input != "<nil>" {
   339  			var ok bool
   340  			x, ok = new(Int).SetString(test.input, 0)
   341  			if !ok {
   342  				t.Errorf("#%d failed reading input %s", i, test.input)
   343  			}
   344  		}
   345  		output := fmt.Sprintf(test.format, x)
   346  		if output != test.output {
   347  			t.Errorf("#%d got %q; want %q, {%q, %q, %q}", i, output, test.output, test.input, test.format, test.output)
   348  		}
   349  	}
   350  }
   351  
   352  var scanTests = []struct {
   353  	input     string
   354  	format    string
   355  	output    string
   356  	remaining int
   357  }{
   358  	{"1010", "%b", "10", 0},
   359  	{"0b1010", "%v", "10", 0},
   360  	{"12", "%o", "10", 0},
   361  	{"012", "%v", "10", 0},
   362  	{"10", "%d", "10", 0},
   363  	{"10", "%v", "10", 0},
   364  	{"a", "%x", "10", 0},
   365  	{"0xa", "%v", "10", 0},
   366  	{"A", "%X", "10", 0},
   367  	{"-A", "%X", "-10", 0},
   368  	{"+0b1011001", "%v", "89", 0},
   369  	{"0xA", "%v", "10", 0},
   370  	{"0 ", "%v", "0", 1},
   371  	{"2+3", "%v", "2", 2},
   372  	{"0XABC 12", "%v", "2748", 3},
   373  }
   374  
   375  func TestScan(t *testing.T) {
   376  	var buf bytes.Buffer
   377  	for i, test := range scanTests {
   378  		x := new(Int)
   379  		buf.Reset()
   380  		buf.WriteString(test.input)
   381  		if _, err := fmt.Fscanf(&buf, test.format, x); err != nil {
   382  			t.Errorf("#%d error: %s", i, err)
   383  		}
   384  		if x.String() != test.output {
   385  			t.Errorf("#%d got %s; want %s", i, x.String(), test.output)
   386  		}
   387  		if buf.Len() != test.remaining {
   388  			t.Errorf("#%d got %d bytes remaining; want %d", i, buf.Len(), test.remaining)
   389  		}
   390  	}
   391  }