github.com/zerosnake0/jzon@v0.0.9-0.20230801092939-1b135cb83f7f/val_decoder_native_uint_test.go (about)

     1  package jzon
     2  
     3  import (
     4  	"io"
     5  	"testing"
     6  )
     7  
     8  func TestValDecoder_Native_Uint(t *testing.T) {
     9  	f := func(t *testing.T, data string, ex error, initValue uint) {
    10  		var p1 *uint
    11  		var p2 *uint
    12  		if initValue != 0 {
    13  			b1 := initValue
    14  			p1 = &b1
    15  			b2 := initValue
    16  			p2 = &b2
    17  		}
    18  		checkDecodeWithStandard(t, DefaultDecoderConfig, data, ex, p1, p2)
    19  	}
    20  	f2 := func(t *testing.T, data string, ex error) {
    21  		f(t, data, ex, 1)
    22  	}
    23  	t.Run("nil pointer", func(t *testing.T) {
    24  		f(t, "null", ErrNilPointerReceiver, 0)
    25  	})
    26  	t.Run("eof", func(t *testing.T) {
    27  		f2(t, "", io.EOF)
    28  	})
    29  	t.Run("invalid first byte", func(t *testing.T) {
    30  		f2(t, `true`, InvalidDigitError{})
    31  	})
    32  	t.Run("invalid null", func(t *testing.T) {
    33  		f2(t, "nul", io.EOF)
    34  	})
    35  	t.Run("null", func(t *testing.T) {
    36  		f2(t, "null", nil)
    37  	})
    38  	t.Run("valid", func(t *testing.T) {
    39  		f2(t, "127", nil)
    40  	})
    41  }
    42  
    43  func TestValDecoder_Native_Uint8(t *testing.T) {
    44  	f := func(t *testing.T, data string, ex error, initValue uint8) {
    45  		var p1 *uint8
    46  		var p2 *uint8
    47  		if initValue != 0 {
    48  			b1 := initValue
    49  			p1 = &b1
    50  			b2 := initValue
    51  			p2 = &b2
    52  		}
    53  		checkDecodeWithStandard(t, DefaultDecoderConfig, data, ex, p1, p2)
    54  	}
    55  	f2 := func(t *testing.T, data string, ex error) {
    56  		f(t, data, ex, 1)
    57  	}
    58  	t.Run("nil pointer", func(t *testing.T) {
    59  		f(t, "null", ErrNilPointerReceiver, 0)
    60  	})
    61  	t.Run("eof", func(t *testing.T) {
    62  		f2(t, "", io.EOF)
    63  	})
    64  	t.Run("invalid first byte", func(t *testing.T) {
    65  		f2(t, `true`, InvalidDigitError{})
    66  	})
    67  	t.Run("invalid null", func(t *testing.T) {
    68  		f2(t, "nul", io.EOF)
    69  	})
    70  	t.Run("null", func(t *testing.T) {
    71  		f2(t, "null", nil)
    72  	})
    73  	t.Run("valid", func(t *testing.T) {
    74  		f2(t, "127", nil)
    75  	})
    76  }
    77  
    78  func TestValDecoder_Native_Uint16(t *testing.T) {
    79  	f := func(t *testing.T, data string, ex error, initValue uint16) {
    80  		var p1 *uint16
    81  		var p2 *uint16
    82  		if initValue != 0 {
    83  			b1 := initValue
    84  			p1 = &b1
    85  			b2 := initValue
    86  			p2 = &b2
    87  		}
    88  		checkDecodeWithStandard(t, DefaultDecoderConfig, data, ex, p1, p2)
    89  	}
    90  	f2 := func(t *testing.T, data string, ex error) {
    91  		f(t, data, ex, 1)
    92  	}
    93  	t.Run("nil pointer", func(t *testing.T) {
    94  		f(t, "null", ErrNilPointerReceiver, 0)
    95  	})
    96  	t.Run("eof", func(t *testing.T) {
    97  		f2(t, "", io.EOF)
    98  	})
    99  	t.Run("invalid first byte", func(t *testing.T) {
   100  		f2(t, `true`, InvalidDigitError{})
   101  	})
   102  	t.Run("invalid null", func(t *testing.T) {
   103  		f2(t, "nul", io.EOF)
   104  	})
   105  	t.Run("null", func(t *testing.T) {
   106  		f2(t, "null", nil)
   107  	})
   108  	t.Run("valid", func(t *testing.T) {
   109  		f2(t, "127", nil)
   110  	})
   111  }
   112  
   113  func TestValDecoder_Native_Uint32(t *testing.T) {
   114  	f := func(t *testing.T, data string, ex error, initValue uint32) {
   115  		var p1 *uint32
   116  		var p2 *uint32
   117  		if initValue != 0 {
   118  			b1 := initValue
   119  			p1 = &b1
   120  			b2 := initValue
   121  			p2 = &b2
   122  		}
   123  		checkDecodeWithStandard(t, DefaultDecoderConfig, data, ex, p1, p2)
   124  	}
   125  	f2 := func(t *testing.T, data string, ex error) {
   126  		f(t, data, ex, 1)
   127  	}
   128  	t.Run("nil pointer", func(t *testing.T) {
   129  		f(t, "null", ErrNilPointerReceiver, 0)
   130  	})
   131  	t.Run("eof", func(t *testing.T) {
   132  		f2(t, "", io.EOF)
   133  	})
   134  	t.Run("invalid first byte", func(t *testing.T) {
   135  		f2(t, `true`, InvalidDigitError{})
   136  	})
   137  	t.Run("invalid null", func(t *testing.T) {
   138  		f2(t, "nul", io.EOF)
   139  	})
   140  	t.Run("null", func(t *testing.T) {
   141  		f2(t, "null", nil)
   142  	})
   143  	t.Run("valid", func(t *testing.T) {
   144  		f2(t, "127", nil)
   145  	})
   146  }
   147  
   148  func TestValDecoder_Native_Uint64(t *testing.T) {
   149  	f := func(t *testing.T, data string, ex error, initValue uint64) {
   150  		var p1 *uint64
   151  		var p2 *uint64
   152  		if initValue != 0 {
   153  			b1 := initValue
   154  			p1 = &b1
   155  			b2 := initValue
   156  			p2 = &b2
   157  		}
   158  		checkDecodeWithStandard(t, DefaultDecoderConfig, data, ex, p1, p2)
   159  	}
   160  	f2 := func(t *testing.T, data string, ex error) {
   161  		f(t, data, ex, 1)
   162  	}
   163  	t.Run("nil pointer", func(t *testing.T) {
   164  		f(t, "null", ErrNilPointerReceiver, 0)
   165  	})
   166  	t.Run("eof", func(t *testing.T) {
   167  		f2(t, "", io.EOF)
   168  	})
   169  	t.Run("invalid first byte", func(t *testing.T) {
   170  		f2(t, `true`, InvalidDigitError{})
   171  	})
   172  	t.Run("invalid null", func(t *testing.T) {
   173  		f2(t, "nul", io.EOF)
   174  	})
   175  	t.Run("null", func(t *testing.T) {
   176  		f2(t, "null", nil)
   177  	})
   178  	t.Run("valid", func(t *testing.T) {
   179  		f2(t, "127", nil)
   180  	})
   181  }