github.com/arnodel/golua@v0.0.0-20230215163904-e0b5347eaaa1/runtime/numconv_test.go (about)

     1  package runtime
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  )
     7  
     8  func TestToNumber(t *testing.T) {
     9  	tests := []struct {
    10  		name string
    11  		v    Value
    12  		n    int64
    13  		x    float64
    14  		tp   NumberType
    15  	}{
    16  		{
    17  			name: "int",
    18  			v:    IntValue(23),
    19  			n:    23,
    20  			tp:   IsInt,
    21  		},
    22  		{
    23  			name: "float",
    24  			v:    FloatValue(1.1),
    25  			x:    1.1,
    26  			tp:   IsFloat,
    27  		},
    28  		{
    29  			name: "int string",
    30  			v:    StringValue("-12"),
    31  			n:    -12,
    32  			tp:   IsInt,
    33  		},
    34  		{
    35  			name: "float string",
    36  			v:    StringValue("1.45"),
    37  			x:    1.45,
    38  			tp:   IsFloat,
    39  		},
    40  		{
    41  			name: "non numeric string",
    42  			v:    StringValue("hello"),
    43  			tp:   NaN,
    44  		},
    45  		{
    46  			name: "string with numeric prefix",
    47  			v:    StringValue("49ers"),
    48  			tp:   NaN,
    49  		},
    50  		{
    51  			name: "boolean",
    52  			v:    BoolValue(false),
    53  			tp:   NaN,
    54  		},
    55  		{
    56  			name: "nil",
    57  			v:    NilValue,
    58  			tp:   NaN,
    59  		},
    60  	}
    61  	for _, tt := range tests {
    62  		t.Run(tt.name, func(t *testing.T) {
    63  			got, got1, got2 := ToNumber(tt.v)
    64  			if got != tt.n {
    65  				t.Errorf("ToNumber() got = %v, want %v", got, tt.n)
    66  			}
    67  			if got1 != tt.x {
    68  				t.Errorf("ToNumber() got1 = %v, want %v", got1, tt.x)
    69  			}
    70  			if got2 != tt.tp {
    71  				t.Errorf("ToNumber() got2 = %v, want %v", got2, tt.tp)
    72  			}
    73  		})
    74  	}
    75  }
    76  
    77  func TestToNumberValue(t *testing.T) {
    78  
    79  	tests := []struct {
    80  		name string
    81  		v    Value
    82  		want Value
    83  		tp   NumberType
    84  	}{
    85  		{
    86  			name: "int",
    87  			v:    IntValue(23),
    88  			want: IntValue(23),
    89  			tp:   IsInt,
    90  		},
    91  		{
    92  			name: "float",
    93  			v:    FloatValue(1.1),
    94  			want: FloatValue(1.1),
    95  			tp:   IsFloat,
    96  		},
    97  		{
    98  			name: "int string",
    99  			v:    StringValue("-12"),
   100  			want: IntValue(-12),
   101  			tp:   IsInt,
   102  		},
   103  		{
   104  			name: "float string",
   105  			v:    StringValue("1.45"),
   106  			want: FloatValue(1.45),
   107  			tp:   IsFloat,
   108  		},
   109  		{
   110  			name: "non numeric string",
   111  			v:    StringValue("hello"),
   112  			want: StringValue("hello"),
   113  			tp:   NaN,
   114  		},
   115  		{
   116  			name: "string with numeric prefix",
   117  			v:    StringValue("49ers"),
   118  			want: StringValue("49ers"),
   119  			tp:   NaN,
   120  		},
   121  		{
   122  			name: "boolean",
   123  			v:    BoolValue(false),
   124  			want: BoolValue(false),
   125  			tp:   NaN,
   126  		},
   127  		{
   128  			name: "nil",
   129  			v:    NilValue,
   130  			want: NilValue,
   131  			tp:   NaN,
   132  		},
   133  	}
   134  	for _, tt := range tests {
   135  		t.Run(tt.name, func(t *testing.T) {
   136  			got, got1 := ToNumberValue(tt.v)
   137  			if !reflect.DeepEqual(got, tt.want) {
   138  				t.Errorf("ToNumberValue() got = %v, want %v", got, tt.want)
   139  			}
   140  			if got1 != tt.tp {
   141  				t.Errorf("ToNumberValue() got1 = %v, want %v", got1, tt.tp)
   142  			}
   143  		})
   144  	}
   145  }
   146  
   147  func TestToInt(t *testing.T) {
   148  
   149  	tests := []struct {
   150  		name string
   151  		v    Value
   152  		want int64
   153  		ok   bool
   154  	}{
   155  		{
   156  			name: "int",
   157  			v:    IntValue(100),
   158  			want: 100,
   159  			ok:   true,
   160  		},
   161  		{
   162  			name: "integral float",
   163  			v:    FloatValue(53),
   164  			want: 53,
   165  			ok:   true,
   166  		},
   167  		{
   168  			name: "int string",
   169  			v:    StringValue("1e6"),
   170  			want: 1e6,
   171  			ok:   true,
   172  		},
   173  		{
   174  			name: "decimal float",
   175  			v:    FloatValue(2.3),
   176  		},
   177  		{
   178  			name: "decimal string",
   179  			v:    StringValue("55.5"),
   180  		},
   181  		{
   182  			name: "nil",
   183  		},
   184  		{
   185  			name: "boolean",
   186  			v:    BoolValue(true),
   187  		},
   188  	}
   189  	for _, tt := range tests {
   190  		t.Run(tt.name, func(t *testing.T) {
   191  			got, got1 := ToInt(tt.v)
   192  			if got != tt.want {
   193  				t.Errorf("ToInt() got = %v, want %v", got, tt.want)
   194  			}
   195  			if got1 != tt.ok {
   196  				t.Errorf("ToInt() got1 = %v, want %v", got1, tt.ok)
   197  			}
   198  		})
   199  	}
   200  }
   201  
   202  func TestToIntNoString(t *testing.T) {
   203  	tests := []struct {
   204  		name string
   205  		v    Value
   206  		want int64
   207  		ok   bool
   208  	}{
   209  		{
   210  			name: "int",
   211  			v:    IntValue(100),
   212  			want: 100,
   213  			ok:   true,
   214  		},
   215  		{
   216  			name: "integral float",
   217  			v:    FloatValue(53),
   218  			want: 53,
   219  			ok:   true,
   220  		},
   221  		{
   222  			name: "int string",
   223  			v:    StringValue("1e6"),
   224  		},
   225  		{
   226  			name: "decimal float",
   227  			v:    FloatValue(2.3),
   228  		},
   229  		{
   230  			name: "decimal string",
   231  			v:    StringValue("55.5"),
   232  		},
   233  		{
   234  			name: "nil",
   235  		},
   236  		{
   237  			name: "boolean",
   238  			v:    BoolValue(true),
   239  		},
   240  	}
   241  	for _, tt := range tests {
   242  		t.Run(tt.name, func(t *testing.T) {
   243  			got, got1 := ToIntNoString(tt.v)
   244  			if got != tt.want {
   245  				t.Errorf("ToIntNoString() got = %v, want %v", got, tt.want)
   246  			}
   247  			if got1 != tt.ok {
   248  				t.Errorf("ToIntNoString() got1 = %v, want %v", got1, tt.ok)
   249  			}
   250  		})
   251  	}
   252  }
   253  
   254  func TestToFloat(t *testing.T) {
   255  	tests := []struct {
   256  		name string
   257  		v    Value
   258  		want float64
   259  		ok   bool
   260  	}{
   261  		{
   262  			name: "int",
   263  			v:    IntValue(100),
   264  			want: 100,
   265  			ok:   true,
   266  		},
   267  		{
   268  			name: "integral float",
   269  			v:    FloatValue(53),
   270  			want: 53,
   271  			ok:   true,
   272  		},
   273  		{
   274  			name: "int string",
   275  			v:    StringValue("10"),
   276  			want: 10,
   277  			ok:   true,
   278  		},
   279  		{
   280  			name: "decimal float",
   281  			v:    FloatValue(2.3),
   282  			want: 2.3,
   283  			ok:   true,
   284  		},
   285  		{
   286  			name: "decimal string",
   287  			v:    StringValue("55.5"),
   288  			want: 55.5,
   289  			ok:   true,
   290  		},
   291  		{
   292  			name: "nil",
   293  		},
   294  		{
   295  			name: "boolean",
   296  			v:    BoolValue(true),
   297  		},
   298  	}
   299  	for _, tt := range tests {
   300  		t.Run(tt.name, func(t *testing.T) {
   301  			got, got1 := ToFloat(tt.v)
   302  			if got != tt.want {
   303  				t.Errorf("ToFloat() got = %v, want %v", got, tt.want)
   304  			}
   305  			if got1 != tt.ok {
   306  				t.Errorf("ToFloat() got1 = %v, want %v", got1, tt.ok)
   307  			}
   308  		})
   309  	}
   310  }