github.com/llir/llvm@v0.3.6/ir/constant/const_int_test.go (about)

     1  package constant
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/llir/llvm/ir/types"
     7  )
     8  
     9  func TestIntIdent(t *testing.T) {
    10  	golden := []struct {
    11  		in   string
    12  		want string
    13  	}{
    14  		// integers < 0x1000 are always represented in decimal notation.
    15  		{in: "100", want: "100"},
    16  		{in: "256", want: "256"},
    17  		{in: "1000", want: "1000"},
    18  		{in: "4095", want: "4095"},
    19  		// integers >= 0x1000 are represented in hexadecimal notation if lower
    20  		// entropy than decimal notation.
    21  		{in: "4096", want: "u0x1000"},
    22  		{in: "2147483648", want: "u0x80000000"},
    23  		{in: "9218868437227405312", want: "u0x7FF0000000000000"},
    24  		{in: "1000000000000000000", want: "1000000000000000000"}, // hex would be u0xDE0B6B3A7640000
    25  		// negative integers are always represented in decimal notation.
    26  		{in: "-100", want: "-100"},
    27  		{in: "-256", want: "-256"},
    28  		{in: "-9218868437227405312", want: "-9218868437227405312"},
    29  		{in: "-1000000000000000000", want: "-1000000000000000000"},
    30  	}
    31  	for _, g := range golden {
    32  		c, err := NewIntFromString(types.I64, g.in)
    33  		if err != nil {
    34  			t.Errorf("unable to parse integer literal %q; %v", g.in, err)
    35  			continue
    36  		}
    37  		got := c.Ident()
    38  		if g.want != got {
    39  			t.Errorf("integer constant string mismatch; expected %q, got %q", g.want, got)
    40  			continue
    41  		}
    42  	}
    43  }