github.com/graywolf-at-work-2/terraform-vendor@v1.4.5/internal/lang/funcs/number_test.go (about)

     1  package funcs
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/internal/lang/marks"
     8  	"github.com/zclconf/go-cty/cty"
     9  )
    10  
    11  func TestLog(t *testing.T) {
    12  	tests := []struct {
    13  		Num  cty.Value
    14  		Base cty.Value
    15  		Want cty.Value
    16  		Err  bool
    17  	}{
    18  		{
    19  			cty.NumberFloatVal(1),
    20  			cty.NumberFloatVal(10),
    21  			cty.NumberFloatVal(0),
    22  			false,
    23  		},
    24  		{
    25  			cty.NumberFloatVal(10),
    26  			cty.NumberFloatVal(10),
    27  			cty.NumberFloatVal(1),
    28  			false,
    29  		},
    30  
    31  		{
    32  			cty.NumberFloatVal(0),
    33  			cty.NumberFloatVal(10),
    34  			cty.NegativeInfinity,
    35  			false,
    36  		},
    37  		{
    38  			cty.NumberFloatVal(10),
    39  			cty.NumberFloatVal(0),
    40  			cty.NumberFloatVal(-0),
    41  			false,
    42  		},
    43  	}
    44  
    45  	for _, test := range tests {
    46  		t.Run(fmt.Sprintf("log(%#v, %#v)", test.Num, test.Base), func(t *testing.T) {
    47  			got, err := Log(test.Num, test.Base)
    48  
    49  			if test.Err {
    50  				if err == nil {
    51  					t.Fatal("succeeded; want error")
    52  				}
    53  				return
    54  			} else if err != nil {
    55  				t.Fatalf("unexpected error: %s", err)
    56  			}
    57  
    58  			if !got.RawEquals(test.Want) {
    59  				t.Errorf("wrong result\ngot:  %#v\nwant: %#v", got, test.Want)
    60  			}
    61  		})
    62  	}
    63  }
    64  
    65  func TestPow(t *testing.T) {
    66  	tests := []struct {
    67  		Num   cty.Value
    68  		Power cty.Value
    69  		Want  cty.Value
    70  		Err   bool
    71  	}{
    72  		{
    73  			cty.NumberFloatVal(1),
    74  			cty.NumberFloatVal(0),
    75  			cty.NumberFloatVal(1),
    76  			false,
    77  		},
    78  		{
    79  			cty.NumberFloatVal(1),
    80  			cty.NumberFloatVal(1),
    81  			cty.NumberFloatVal(1),
    82  			false,
    83  		},
    84  
    85  		{
    86  			cty.NumberFloatVal(2),
    87  			cty.NumberFloatVal(0),
    88  			cty.NumberFloatVal(1),
    89  			false,
    90  		},
    91  		{
    92  			cty.NumberFloatVal(2),
    93  			cty.NumberFloatVal(1),
    94  			cty.NumberFloatVal(2),
    95  			false,
    96  		},
    97  		{
    98  			cty.NumberFloatVal(3),
    99  			cty.NumberFloatVal(2),
   100  			cty.NumberFloatVal(9),
   101  			false,
   102  		},
   103  		{
   104  			cty.NumberFloatVal(-3),
   105  			cty.NumberFloatVal(2),
   106  			cty.NumberFloatVal(9),
   107  			false,
   108  		},
   109  		{
   110  			cty.NumberFloatVal(2),
   111  			cty.NumberFloatVal(-2),
   112  			cty.NumberFloatVal(0.25),
   113  			false,
   114  		},
   115  		{
   116  			cty.NumberFloatVal(0),
   117  			cty.NumberFloatVal(2),
   118  			cty.NumberFloatVal(0),
   119  			false,
   120  		},
   121  	}
   122  
   123  	for _, test := range tests {
   124  		t.Run(fmt.Sprintf("pow(%#v, %#v)", test.Num, test.Power), func(t *testing.T) {
   125  			got, err := Pow(test.Num, test.Power)
   126  
   127  			if test.Err {
   128  				if err == nil {
   129  					t.Fatal("succeeded; want error")
   130  				}
   131  				return
   132  			} else if err != nil {
   133  				t.Fatalf("unexpected error: %s", err)
   134  			}
   135  
   136  			if !got.RawEquals(test.Want) {
   137  				t.Errorf("wrong result\ngot:  %#v\nwant: %#v", got, test.Want)
   138  			}
   139  		})
   140  	}
   141  }
   142  
   143  func TestSignum(t *testing.T) {
   144  	tests := []struct {
   145  		Num  cty.Value
   146  		Want cty.Value
   147  		Err  bool
   148  	}{
   149  		{
   150  			cty.NumberFloatVal(0),
   151  			cty.NumberFloatVal(0),
   152  			false,
   153  		},
   154  		{
   155  			cty.NumberFloatVal(12),
   156  			cty.NumberFloatVal(1),
   157  			false,
   158  		},
   159  		{
   160  			cty.NumberFloatVal(-29),
   161  			cty.NumberFloatVal(-1),
   162  			false,
   163  		},
   164  	}
   165  
   166  	for _, test := range tests {
   167  		t.Run(fmt.Sprintf("signum(%#v)", test.Num), func(t *testing.T) {
   168  			got, err := Signum(test.Num)
   169  
   170  			if test.Err {
   171  				if err == nil {
   172  					t.Fatal("succeeded; want error")
   173  				}
   174  				return
   175  			} else if err != nil {
   176  				t.Fatalf("unexpected error: %s", err)
   177  			}
   178  
   179  			if !got.RawEquals(test.Want) {
   180  				t.Errorf("wrong result\ngot:  %#v\nwant: %#v", got, test.Want)
   181  			}
   182  		})
   183  	}
   184  }
   185  
   186  func TestParseInt(t *testing.T) {
   187  	tests := []struct {
   188  		Num  cty.Value
   189  		Base cty.Value
   190  		Want cty.Value
   191  		Err  string
   192  	}{
   193  		{
   194  			cty.StringVal("128"),
   195  			cty.NumberIntVal(10),
   196  			cty.NumberIntVal(128),
   197  			``,
   198  		},
   199  		{
   200  			cty.StringVal("128").Mark(marks.Sensitive),
   201  			cty.NumberIntVal(10),
   202  			cty.NumberIntVal(128).Mark(marks.Sensitive),
   203  			``,
   204  		},
   205  		{
   206  			cty.StringVal("128"),
   207  			cty.NumberIntVal(10).Mark(marks.Sensitive),
   208  			cty.NumberIntVal(128).Mark(marks.Sensitive),
   209  			``,
   210  		},
   211  		{
   212  			cty.StringVal("128").Mark(marks.Sensitive),
   213  			cty.NumberIntVal(10).Mark(marks.Sensitive),
   214  			cty.NumberIntVal(128).Mark(marks.Sensitive),
   215  			``,
   216  		},
   217  		{
   218  			cty.StringVal("128").Mark("boop"),
   219  			cty.NumberIntVal(10).Mark(marks.Sensitive),
   220  			cty.NumberIntVal(128).WithMarks(cty.NewValueMarks("boop", marks.Sensitive)),
   221  			``,
   222  		},
   223  		{
   224  			cty.StringVal("-128"),
   225  			cty.NumberIntVal(10),
   226  			cty.NumberIntVal(-128),
   227  			``,
   228  		},
   229  		{
   230  			cty.StringVal("00128"),
   231  			cty.NumberIntVal(10),
   232  			cty.NumberIntVal(128),
   233  			``,
   234  		},
   235  		{
   236  			cty.StringVal("-00128"),
   237  			cty.NumberIntVal(10),
   238  			cty.NumberIntVal(-128),
   239  			``,
   240  		},
   241  		{
   242  			cty.StringVal("FF00"),
   243  			cty.NumberIntVal(16),
   244  			cty.NumberIntVal(65280),
   245  			``,
   246  		},
   247  		{
   248  			cty.StringVal("ff00"),
   249  			cty.NumberIntVal(16),
   250  			cty.NumberIntVal(65280),
   251  			``,
   252  		},
   253  		{
   254  			cty.StringVal("-FF00"),
   255  			cty.NumberIntVal(16),
   256  			cty.NumberIntVal(-65280),
   257  			``,
   258  		},
   259  		{
   260  			cty.StringVal("00FF00"),
   261  			cty.NumberIntVal(16),
   262  			cty.NumberIntVal(65280),
   263  			``,
   264  		},
   265  		{
   266  			cty.StringVal("-00FF00"),
   267  			cty.NumberIntVal(16),
   268  			cty.NumberIntVal(-65280),
   269  			``,
   270  		},
   271  		{
   272  			cty.StringVal("1011111011101111"),
   273  			cty.NumberIntVal(2),
   274  			cty.NumberIntVal(48879),
   275  			``,
   276  		},
   277  		{
   278  			cty.StringVal("aA"),
   279  			cty.NumberIntVal(62),
   280  			cty.NumberIntVal(656),
   281  			``,
   282  		},
   283  		{
   284  			cty.StringVal("Aa"),
   285  			cty.NumberIntVal(62),
   286  			cty.NumberIntVal(2242),
   287  			``,
   288  		},
   289  		{
   290  			cty.StringVal("999999999999999999999999999999999999999999999999999999999999"),
   291  			cty.NumberIntVal(10),
   292  			cty.MustParseNumberVal("999999999999999999999999999999999999999999999999999999999999"),
   293  			``,
   294  		},
   295  		{
   296  			cty.StringVal("FF"),
   297  			cty.NumberIntVal(10),
   298  			cty.UnknownVal(cty.Number),
   299  			`cannot parse "FF" as a base 10 integer`,
   300  		},
   301  		{
   302  			cty.StringVal("FF").Mark(marks.Sensitive),
   303  			cty.NumberIntVal(10),
   304  			cty.UnknownVal(cty.Number),
   305  			`cannot parse (sensitive value) as a base 10 integer`,
   306  		},
   307  		{
   308  			cty.StringVal("FF").Mark(marks.Sensitive),
   309  			cty.NumberIntVal(10).Mark(marks.Sensitive),
   310  			cty.UnknownVal(cty.Number),
   311  			`cannot parse (sensitive value) as a base (sensitive value) integer`,
   312  		},
   313  		{
   314  			cty.StringVal("00FF"),
   315  			cty.NumberIntVal(10),
   316  			cty.UnknownVal(cty.Number),
   317  			`cannot parse "00FF" as a base 10 integer`,
   318  		},
   319  		{
   320  			cty.StringVal("-00FF"),
   321  			cty.NumberIntVal(10),
   322  			cty.UnknownVal(cty.Number),
   323  			`cannot parse "-00FF" as a base 10 integer`,
   324  		},
   325  		{
   326  			cty.NumberIntVal(2),
   327  			cty.NumberIntVal(10),
   328  			cty.UnknownVal(cty.Number),
   329  			`first argument must be a string, not number`,
   330  		},
   331  		{
   332  			cty.StringVal("1"),
   333  			cty.NumberIntVal(63),
   334  			cty.UnknownVal(cty.Number),
   335  			`base must be a whole number between 2 and 62 inclusive`,
   336  		},
   337  		{
   338  			cty.StringVal("1"),
   339  			cty.NumberIntVal(-1),
   340  			cty.UnknownVal(cty.Number),
   341  			`base must be a whole number between 2 and 62 inclusive`,
   342  		},
   343  		{
   344  			cty.StringVal("1"),
   345  			cty.NumberIntVal(1),
   346  			cty.UnknownVal(cty.Number),
   347  			`base must be a whole number between 2 and 62 inclusive`,
   348  		},
   349  		{
   350  			cty.StringVal("1"),
   351  			cty.NumberIntVal(0),
   352  			cty.UnknownVal(cty.Number),
   353  			`base must be a whole number between 2 and 62 inclusive`,
   354  		},
   355  		{
   356  			cty.StringVal("1.2"),
   357  			cty.NumberIntVal(10),
   358  			cty.UnknownVal(cty.Number),
   359  			`cannot parse "1.2" as a base 10 integer`,
   360  		},
   361  	}
   362  
   363  	for _, test := range tests {
   364  		t.Run(fmt.Sprintf("parseint(%#v, %#v)", test.Num, test.Base), func(t *testing.T) {
   365  			got, err := ParseInt(test.Num, test.Base)
   366  
   367  			if test.Err != "" {
   368  				if err == nil {
   369  					t.Fatal("succeeded; want error")
   370  				}
   371  				if got, want := err.Error(), test.Err; got != want {
   372  					t.Errorf("wrong error\ngot:  %s\nwant: %s", got, want)
   373  				}
   374  				return
   375  			} else if err != nil {
   376  				t.Fatalf("unexpected error: %s", err)
   377  			}
   378  
   379  			if !got.RawEquals(test.Want) {
   380  				t.Errorf("wrong result\ngot:  %#v\nwant: %#v", got, test.Want)
   381  			}
   382  		})
   383  	}
   384  }