github.com/GeniusesGroup/libgo@v0.0.0-20220929090155-5ff932cb408e/convert/string-to-integer_test.go (about)

     1  /* For license and copyright information please see LEGAL file in repository */
     2  
     3  package convert
     4  
     5  import (
     6  	"fmt"
     7  	"testing"
     8  
     9  	"../protocol"
    10  )
    11  
    12  func TestStringToUint8Base10(t *testing.T) {
    13  	type args struct {
    14  		str string
    15  	}
    16  	tests := []struct {
    17  		name    string
    18  		args    args
    19  		wantNum uint8
    20  		wantErr protocol.Error
    21  	}{
    22  		{
    23  			name:    "empty",
    24  			args:    args{str: ""},
    25  			wantErr: ErrEmptyValue,
    26  		},
    27  		{
    28  			name:    "0",
    29  			args:    args{str: "0"},
    30  			wantNum: 0,
    31  		},
    32  		{
    33  			name:    "158",
    34  			args:    args{str: "158"},
    35  			wantNum: 158,
    36  		},
    37  		{
    38  			name:    "255",
    39  			args:    args{str: "255"},
    40  			wantNum: 255,
    41  		},
    42  		{
    43  			name:    "1582",
    44  			args:    args{str: "1582"},
    45  			wantErr: ErrValueOutOfRange,
    46  		},
    47  		{
    48  			name:    "15a",
    49  			args:    args{str: "15a"},
    50  			wantErr: ErrBadValue,
    51  		},
    52  	}
    53  	for _, tt := range tests {
    54  		t.Run(tt.name, func(t *testing.T) {
    55  			gotNum, gotErr := StringToUint8Base10(tt.args.str)
    56  			if gotNum != tt.wantNum {
    57  				t.Errorf("StringToUint8Base10() gotNum = %v, want %v", gotNum, tt.wantNum)
    58  			}
    59  			if gotErr != tt.wantErr {
    60  				t.Errorf("StringToUint8Base10() gotErr = %v, want %v", gotErr, tt.wantErr)
    61  			}
    62  			fmt.Println("Test number:", tt.args.str, gotNum)
    63  		})
    64  	}
    65  }
    66  
    67  func TestStringToUint32Base10(t *testing.T) {
    68  	type args struct {
    69  		str string
    70  	}
    71  	tests := []struct {
    72  		name    string
    73  		args    args
    74  		wantNum uint32
    75  		wantErr protocol.Error
    76  	}{
    77  		{
    78  			name:    "empty",
    79  			args:    args{str: ""},
    80  			wantNum: 0,
    81  			wantErr: ErrEmptyValue,
    82  		},
    83  		{
    84  			name:    "0",
    85  			args:    args{str: "0"},
    86  			wantNum: 0,
    87  		},
    88  		{
    89  			name:    "158564",
    90  			args:    args{str: "158564"},
    91  			wantNum: 158564,
    92  		},
    93  		{
    94  			name:    "4294967295",
    95  			args:    args{str: "4294967295"},
    96  			wantNum: 4294967295,
    97  		},
    98  		{
    99  			name:    "429496729546",
   100  			args:    args{str: "429496729546"},
   101  			wantNum: 0,
   102  			wantErr: ErrValueOutOfRange,
   103  		},
   104  		{
   105  			name:    "1545a",
   106  			args:    args{str: "1545a"},
   107  			wantErr: ErrBadValue,
   108  		},
   109  	}
   110  	for _, tt := range tests {
   111  		t.Run(tt.name, func(t *testing.T) {
   112  			gotNum, gotErr := StringToUint32Base10(tt.args.str)
   113  			if gotNum != tt.wantNum {
   114  				t.Errorf("StringToUint32Base10() gotNum = %v, want %v", gotNum, tt.wantNum)
   115  			}
   116  			if gotErr != tt.wantErr {
   117  				t.Errorf("StringToUint32Base10() gotErr = %v, want %v", gotErr, tt.wantErr)
   118  			}
   119  			fmt.Println("Test number:", tt.args.str, gotNum)
   120  		})
   121  	}
   122  }
   123  
   124  func TestStringToUint64Base10(t *testing.T) {
   125  	type args struct {
   126  		str string
   127  	}
   128  	tests := []struct {
   129  		name    string
   130  		args    args
   131  		wantNum uint64
   132  		wantErr protocol.Error
   133  	}{
   134  		{
   135  			name:    "empty",
   136  			args:    args{str: ""},
   137  			wantNum: 0,
   138  			wantErr: ErrEmptyValue,
   139  		},
   140  		{
   141  			name:    "0",
   142  			args:    args{str: "0"},
   143  			wantNum: 0,
   144  		},
   145  		{
   146  			name:    "4294967295",
   147  			args:    args{str: "4294967295"},
   148  			wantNum: 4294967295,
   149  		},
   150  		{
   151  			name:    "18446744073709551615",
   152  			args:    args{str: "18446744073709551615"},
   153  			wantNum: 18446744073709551615,
   154  		},
   155  		{
   156  			name:    "184467440737095516151",
   157  			args:    args{str: "184467440737095516151"},
   158  			wantErr: ErrValueOutOfRange,
   159  		},
   160  		{
   161  			name:    "154565a",
   162  			args:    args{str: "154565a"},
   163  			wantErr: ErrBadValue,
   164  		},
   165  	}
   166  	for _, tt := range tests {
   167  		t.Run(tt.name, func(t *testing.T) {
   168  			gotNum, gotErr := StringToUint64Base10(tt.args.str)
   169  			if gotNum != tt.wantNum {
   170  				t.Errorf("StringToUint64Base10() gotNum = %v, want %v", gotNum, tt.wantNum)
   171  			}
   172  			if gotErr != tt.wantErr {
   173  				t.Errorf("StringToUint64Base10() gotErr = %v, want %v", gotErr, tt.wantErr)
   174  			}
   175  			fmt.Println("Test number:", tt.args.str, gotNum)
   176  		})
   177  	}
   178  }