github.com/danil/iso8583@v0.21.0/tsysbitmap64r/tsysbitmap64r_test.go (about)

     1  package tsysbitmap64r_test
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"log"
     7  	"runtime"
     8  	"strconv"
     9  	"testing"
    10  
    11  	"github.com/danil/iso8583/tsysbitmap64r"
    12  )
    13  
    14  var NewTestCases = []struct {
    15  	name      string
    16  	input     []byte
    17  	expected  map[int]bool
    18  	line      int
    19  	benchmark bool
    20  }{
    21  	{
    22  		name:      "13th bit is set and this is a recurring payment",
    23  		input:     []byte{0x0, 0x8, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x52},
    24  		expected:  map[int]bool{13: true},
    25  		line:      func() int { _, _, l, _ := runtime.Caller(1); return l }(),
    26  		benchmark: true,
    27  	},
    28  	{
    29  		name:      "13th bit is not setted",
    30  		input:     []byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},
    31  		expected:  map[int]bool{13: false},
    32  		line:      func() int { _, _, l, _ := runtime.Caller(1); return l }(),
    33  		benchmark: false,
    34  	},
    35  }
    36  
    37  func TestNew(t *testing.T) {
    38  	_, testFile, _, _ := runtime.Caller(0)
    39  	for _, tc := range NewTestCases {
    40  		tc := tc
    41  		t.Run(fmt.Sprintf("%s %s", tc.name, strconv.Itoa(tc.line)), func(t *testing.T) {
    42  			t.Parallel()
    43  			linkToExample := fmt.Sprintf("%s:%d", testFile, tc.line)
    44  			b := tsysbitmap64r.New(tc.input)
    45  			for k, v := range tc.expected {
    46  				if b.Get(k) != v {
    47  					t.Errorf("[bit %[1]d] expected: %[1]d=%[2]t, received: %[1]d=%[3]t set - %s", k, v, b.Get(k), linkToExample)
    48  				}
    49  			}
    50  		})
    51  	}
    52  }
    53  
    54  func BenchmarkNew(b *testing.B) {
    55  	b.ReportAllocs()
    56  	for _, tc := range NewTestCases {
    57  		if !tc.benchmark {
    58  			continue
    59  		}
    60  		b.Run(strconv.Itoa(tc.line), func(b *testing.B) {
    61  			for i := 0; i < b.N; i++ {
    62  				_ = tsysbitmap64r.New(tc.input)
    63  			}
    64  		})
    65  	}
    66  }
    67  
    68  var NewStringTestCases = []struct {
    69  	name      string
    70  	input     string
    71  	expected  map[int]bool
    72  	line      int
    73  	benchmark bool
    74  }{
    75  	{
    76  		name:      "13th bit is set and this is a recurring payment",
    77  		input:     "0000000000001000000000000000000000000000000000000000000000000000",
    78  		expected:  map[int]bool{13: true},
    79  		line:      func() int { _, _, l, _ := runtime.Caller(1); return l }(),
    80  		benchmark: true,
    81  	},
    82  	{
    83  		name:      "13th bit is not setted",
    84  		input:     "0000000000000000000000000000000000000000000000000000000000000000",
    85  		expected:  map[int]bool{13: false},
    86  		line:      func() int { _, _, l, _ := runtime.Caller(1); return l }(),
    87  		benchmark: false,
    88  	},
    89  }
    90  
    91  func TestNewString(t *testing.T) {
    92  	_, testFile, _, _ := runtime.Caller(0)
    93  	for _, tc := range NewStringTestCases {
    94  		tc := tc
    95  		t.Run(fmt.Sprintf("%s %s", tc.name, strconv.Itoa(tc.line)), func(t *testing.T) {
    96  			t.Parallel()
    97  			linkToExample := fmt.Sprintf("%s:%d", testFile, tc.line)
    98  			b, err := tsysbitmap64r.NewString(tc.input)
    99  			if err != nil {
   100  				t.Fatalf("unexpected error: %#v - %s", err, linkToExample)
   101  			}
   102  			for k, v := range tc.expected {
   103  				if b.Get(k) != v {
   104  					t.Errorf("[bit %[1]d] expected: %[1]d=%[2]t, received: %[1]d=%[3]t set - %s", k, v, b.Get(k), linkToExample)
   105  				}
   106  			}
   107  		})
   108  	}
   109  }
   110  
   111  func BenchmarkNewString(b *testing.B) {
   112  	b.ReportAllocs()
   113  	for _, tc := range NewStringTestCases {
   114  		if !tc.benchmark {
   115  			continue
   116  		}
   117  		b.Run(strconv.Itoa(tc.line), func(b *testing.B) {
   118  			for i := 0; i < b.N; i++ {
   119  				_, err := tsysbitmap64r.NewString(tc.input)
   120  				if err != nil {
   121  					log.Fatal(err)
   122  				}
   123  			}
   124  		})
   125  	}
   126  }
   127  
   128  func getLine() int {
   129  	_, _, line, _ := runtime.Caller(1)
   130  	return line
   131  }
   132  
   133  var BitmapGetOutOfRangeTestCases = []struct {
   134  	input     [8]byte
   135  	index     int
   136  	expected  interface{}
   137  	line      int
   138  	benchmark bool
   139  }{
   140  	{testArray, 0, "index out of range from 1 to 64", getLine(), true},
   141  	{testArray, 1, nil, getLine(), true},
   142  	{testArray, 64, nil, getLine(), false},
   143  	{testArray, 65, "index out of range from 1 to 64", getLine(), false},
   144  }
   145  
   146  func TestBitmapGetOutOfRange(t *testing.T) {
   147  	_, testFile, _, _ := runtime.Caller(0)
   148  	for _, tc := range BitmapGetOutOfRangeTestCases {
   149  		tc := tc
   150  		t.Run(fmt.Sprintf("%d %t %d", tc.index, tc.expected, tc.line), func(t *testing.T) {
   151  			t.Parallel()
   152  			linkToExample := fmt.Sprintf("%s:%d", testFile, tc.line)
   153  			defer func() {
   154  				if p := recover(); p != tc.expected {
   155  					t.Errorf("[panic] expected: %#v, received: %#v - %s", tc.expected, p, linkToExample)
   156  				}
   157  			}()
   158  			b := tsysbitmap64r.Bitmap(tc.input)
   159  			b.Get(tc.index)
   160  		})
   161  	}
   162  }
   163  
   164  var testArray = [8]byte{0x0, 0x8, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}
   165  
   166  var BitmapGetTestCases = []struct {
   167  	input     [8]byte
   168  	index     int
   169  	expected  bool
   170  	line      int
   171  	benchmark bool
   172  }{
   173  	{testArray, 1, false, getLine(), false},
   174  	{testArray, 2, false, getLine(), false},
   175  	{testArray, 3, false, getLine(), false},
   176  	{testArray, 4, false, getLine(), false},
   177  	{testArray, 5, false, getLine(), false},
   178  	{testArray, 6, false, getLine(), false},
   179  	{testArray, 7, false, getLine(), false},
   180  	{testArray, 8, false, getLine(), false},
   181  	{testArray, 9, false, getLine(), false},
   182  	{testArray, 10, false, getLine(), false},
   183  	{testArray, 11, false, getLine(), false},
   184  	{testArray, 12, false, getLine(), false},
   185  	{testArray, 13, true, getLine(), true},
   186  	{testArray, 14, false, getLine(), false},
   187  	{testArray, 15, false, getLine(), false},
   188  	{testArray, 16, false, getLine(), false},
   189  	{testArray, 17, false, getLine(), false},
   190  	{testArray, 18, false, getLine(), false},
   191  	{testArray, 19, false, getLine(), false},
   192  	{testArray, 20, false, getLine(), false},
   193  	{testArray, 21, false, getLine(), false},
   194  	{testArray, 22, false, getLine(), false},
   195  	{testArray, 23, false, getLine(), false},
   196  	{testArray, 24, false, getLine(), false},
   197  	{testArray, 25, false, getLine(), false},
   198  	{testArray, 26, false, getLine(), false},
   199  	{testArray, 27, false, getLine(), false},
   200  	{testArray, 28, false, getLine(), false},
   201  	{testArray, 29, false, getLine(), false},
   202  	{testArray, 30, false, getLine(), false},
   203  	{testArray, 31, false, getLine(), false},
   204  	{testArray, 32, false, getLine(), false},
   205  	{testArray, 33, false, getLine(), false},
   206  	{testArray, 34, false, getLine(), false},
   207  	{testArray, 35, false, getLine(), false},
   208  	{testArray, 36, false, getLine(), false},
   209  	{testArray, 37, false, getLine(), false},
   210  	{testArray, 38, false, getLine(), false},
   211  	{testArray, 39, false, getLine(), false},
   212  	{testArray, 40, false, getLine(), false},
   213  	{testArray, 41, false, getLine(), false},
   214  	{testArray, 42, false, getLine(), false},
   215  	{testArray, 43, false, getLine(), false},
   216  	{testArray, 44, false, getLine(), false},
   217  	{testArray, 45, false, getLine(), false},
   218  	{testArray, 46, false, getLine(), false},
   219  	{testArray, 47, false, getLine(), false},
   220  	{testArray, 48, false, getLine(), false},
   221  	{testArray, 49, false, getLine(), false},
   222  	{testArray, 50, false, getLine(), false},
   223  	{testArray, 51, false, getLine(), false},
   224  	{testArray, 52, false, getLine(), false},
   225  	{testArray, 53, false, getLine(), false},
   226  	{testArray, 54, false, getLine(), false},
   227  	{testArray, 55, false, getLine(), false},
   228  	{testArray, 56, false, getLine(), false},
   229  	{testArray, 57, false, getLine(), false},
   230  	{testArray, 58, false, getLine(), false},
   231  	{testArray, 59, false, getLine(), false},
   232  	{testArray, 60, false, getLine(), false},
   233  	{testArray, 61, false, getLine(), false},
   234  	{testArray, 62, false, getLine(), false},
   235  	{testArray, 63, false, getLine(), false},
   236  	{testArray, 64, false, getLine(), false},
   237  }
   238  
   239  func TestBitmapGet(t *testing.T) {
   240  	_, testFile, _, _ := runtime.Caller(0)
   241  	for _, tc := range BitmapGetTestCases {
   242  		tc := tc
   243  		t.Run(fmt.Sprintf("%d %t %d", tc.index, tc.expected, tc.line), func(t *testing.T) {
   244  			t.Parallel()
   245  			linkToExample := fmt.Sprintf("%s:%d", testFile, tc.line)
   246  			b := tsysbitmap64r.Bitmap(tc.input)
   247  			ok := b.Get(tc.index)
   248  			if ok != tc.expected {
   249  				t.Errorf("[bit %d] expected: %t, received: %t - %s", tc.index, tc.expected, ok, linkToExample)
   250  			}
   251  		})
   252  	}
   253  }
   254  
   255  func BenchmarkBitmapGet(b *testing.B) {
   256  	b.ReportAllocs()
   257  	for _, tc := range BitmapGetTestCases {
   258  		if !tc.benchmark {
   259  			continue
   260  		}
   261  		b.Run(strconv.Itoa(tc.line), func(b *testing.B) {
   262  			for i := 0; i < b.N; i++ {
   263  				b := tsysbitmap64r.Bitmap(tc.input)
   264  				_ = b.Get(tc.index)
   265  			}
   266  		})
   267  	}
   268  }
   269  
   270  var BitmapGetSetTestCases = []struct {
   271  	input     [8]byte
   272  	index     int
   273  	line      int
   274  	benchmark bool
   275  }{
   276  	{testArray, 1, getLine(), true},
   277  	{testArray, 2, getLine(), false},
   278  	{testArray, 3, getLine(), false},
   279  	{testArray, 4, getLine(), false},
   280  	{testArray, 5, getLine(), false},
   281  	{testArray, 6, getLine(), false},
   282  	{testArray, 7, getLine(), false},
   283  	{testArray, 8, getLine(), false},
   284  	{testArray, 9, getLine(), false},
   285  	{testArray, 10, getLine(), false},
   286  	{testArray, 11, getLine(), false},
   287  	{testArray, 12, getLine(), false},
   288  	{testArray, 13, getLine(), false},
   289  	{testArray, 14, getLine(), false},
   290  	{testArray, 15, getLine(), false},
   291  	{testArray, 16, getLine(), false},
   292  	{testArray, 17, getLine(), false},
   293  	{testArray, 18, getLine(), false},
   294  	{testArray, 19, getLine(), false},
   295  	{testArray, 20, getLine(), false},
   296  	{testArray, 21, getLine(), false},
   297  	{testArray, 22, getLine(), false},
   298  	{testArray, 23, getLine(), false},
   299  	{testArray, 24, getLine(), false},
   300  	{testArray, 25, getLine(), false},
   301  	{testArray, 26, getLine(), false},
   302  	{testArray, 27, getLine(), false},
   303  	{testArray, 28, getLine(), false},
   304  	{testArray, 29, getLine(), false},
   305  	{testArray, 30, getLine(), false},
   306  	{testArray, 31, getLine(), false},
   307  	{testArray, 32, getLine(), false},
   308  	{testArray, 33, getLine(), false},
   309  	{testArray, 34, getLine(), false},
   310  	{testArray, 35, getLine(), false},
   311  	{testArray, 36, getLine(), false},
   312  	{testArray, 37, getLine(), false},
   313  	{testArray, 38, getLine(), false},
   314  	{testArray, 39, getLine(), false},
   315  	{testArray, 40, getLine(), false},
   316  	{testArray, 41, getLine(), false},
   317  	{testArray, 42, getLine(), false},
   318  	{testArray, 43, getLine(), false},
   319  	{testArray, 44, getLine(), false},
   320  	{testArray, 45, getLine(), false},
   321  	{testArray, 46, getLine(), false},
   322  	{testArray, 47, getLine(), false},
   323  	{testArray, 48, getLine(), false},
   324  	{testArray, 49, getLine(), false},
   325  	{testArray, 50, getLine(), false},
   326  	{testArray, 51, getLine(), false},
   327  	{testArray, 52, getLine(), false},
   328  	{testArray, 53, getLine(), false},
   329  	{testArray, 54, getLine(), false},
   330  	{testArray, 55, getLine(), false},
   331  	{testArray, 56, getLine(), false},
   332  	{testArray, 57, getLine(), false},
   333  	{testArray, 58, getLine(), false},
   334  	{testArray, 59, getLine(), false},
   335  	{testArray, 60, getLine(), false},
   336  	{testArray, 61, getLine(), false},
   337  	{testArray, 62, getLine(), false},
   338  	{testArray, 63, getLine(), false},
   339  	{testArray, 64, getLine(), false},
   340  }
   341  
   342  func TestBitmapGetSet(t *testing.T) {
   343  	_, testFile, _, _ := runtime.Caller(0)
   344  	for _, tc := range BitmapGetSetTestCases {
   345  		tc := tc
   346  		t.Run(fmt.Sprintf("%d %d", tc.index, tc.line), func(t *testing.T) {
   347  			t.Parallel()
   348  			linkToExample := fmt.Sprintf("%s:%d", testFile, tc.line)
   349  			b := tsysbitmap64r.Bitmap([8]byte{})
   350  			b.Set(tc.index)
   351  			if !b.Get(tc.index) {
   352  				t.Errorf("[bit %d] expected true, but it is false - %s", tc.index, linkToExample)
   353  			}
   354  		})
   355  	}
   356  }
   357  
   358  func BenchmarkBitmapGetSet(b *testing.B) {
   359  	b.ReportAllocs()
   360  	for _, tc := range BitmapGetSetTestCases {
   361  		if !tc.benchmark {
   362  			continue
   363  		}
   364  		b.Run(strconv.Itoa(tc.line), func(b *testing.B) {
   365  			_, testFile, _, _ := runtime.Caller(0)
   366  			linkToExample := fmt.Sprintf("%s:%d", testFile, tc.line)
   367  			for i := 0; i < b.N; i++ {
   368  				b := tsysbitmap64r.Bitmap(tc.input)
   369  				b.Set(tc.index)
   370  				if !b.Get(tc.index) {
   371  					log.Fatalf("[bit %d] expected true, but it is false - %s", tc.index, linkToExample)
   372  				}
   373  			}
   374  		})
   375  	}
   376  }
   377  
   378  var BitmapSetOutOfRangeTestCases = []struct {
   379  	input     [8]byte
   380  	index     int
   381  	expected  interface{}
   382  	line      int
   383  	benchmark bool
   384  }{
   385  	{testArray, 0, "index out of range from 1 to 64", getLine(), true},
   386  	{testArray, 1, nil, getLine(), true},
   387  	{testArray, 64, nil, getLine(), false},
   388  	{testArray, 65, "index out of range from 1 to 64", getLine(), false},
   389  }
   390  
   391  func TestBitmapSetOutOfRange(t *testing.T) {
   392  	_, testFile, _, _ := runtime.Caller(0)
   393  	for _, tc := range BitmapSetOutOfRangeTestCases {
   394  		tc := tc
   395  		t.Run(fmt.Sprintf("%d %t %d", tc.index, tc.expected, tc.line), func(t *testing.T) {
   396  			t.Parallel()
   397  			linkToExample := fmt.Sprintf("%s:%d", testFile, tc.line)
   398  			defer func() {
   399  				if p := recover(); p != tc.expected {
   400  					t.Errorf("[panic] expected: %#v, received: %#v - %s", tc.expected, p, linkToExample)
   401  				}
   402  			}()
   403  			b := tsysbitmap64r.Bitmap(tc.input)
   404  			b.Set(tc.index)
   405  		})
   406  	}
   407  }
   408  
   409  var BitmapStringTestCases = []struct {
   410  	name      string
   411  	input     [8]byte
   412  	expected  string
   413  	line      int
   414  	benchmark bool
   415  }{
   416  	{
   417  		name:      "13th bit is set and this is a recurring payment",
   418  		input:     [8]byte{0x0, 0x8, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},
   419  		expected:  "0000000000001000000000000000000000000000000000000000000000000000",
   420  		line:      func() int { _, _, l, _ := runtime.Caller(1); return l }(),
   421  		benchmark: true,
   422  	},
   423  	{
   424  		name:      "13th bit is not setted",
   425  		input:     [8]byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},
   426  		expected:  "0000000000000000000000000000000000000000000000000000000000000000",
   427  		line:      func() int { _, _, l, _ := runtime.Caller(1); return l }(),
   428  		benchmark: false,
   429  	},
   430  }
   431  
   432  func TestBitmapString(t *testing.T) {
   433  	_, testFile, _, _ := runtime.Caller(0)
   434  	for _, tc := range BitmapStringTestCases {
   435  		tc := tc
   436  		t.Run(fmt.Sprintf("%s %s", tc.name, strconv.Itoa(tc.line)), func(t *testing.T) {
   437  			t.Parallel()
   438  			linkToExample := fmt.Sprintf("%s:%d", testFile, tc.line)
   439  			b := tsysbitmap64r.Bitmap(tc.input)
   440  			if b.String() != tc.expected {
   441  				t.Errorf("[string] expected: %q, received: %q - %s", tc.expected, b.String(), linkToExample)
   442  			}
   443  		})
   444  	}
   445  }
   446  
   447  func BenchmarkBitmapString(b *testing.B) {
   448  	b.ReportAllocs()
   449  	for _, tc := range BitmapStringTestCases {
   450  		if !tc.benchmark {
   451  			continue
   452  		}
   453  		b.Run(strconv.Itoa(tc.line), func(b *testing.B) {
   454  			for i := 0; i < b.N; i++ {
   455  				b := tsysbitmap64r.Bitmap(tc.input)
   456  				_ = b.String()
   457  			}
   458  		})
   459  	}
   460  }
   461  
   462  var BitmapMarshalISO8583TestCases = []struct {
   463  	name      string
   464  	input     [8]byte
   465  	expected  []byte
   466  	line      int
   467  	benchmark bool
   468  }{
   469  	{
   470  		name:      "13th bit is set and this is a recurring payment",
   471  		input:     [8]byte{0x0, 0x8, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},
   472  		expected:  []byte{0x0, 0x8, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x52},
   473  		line:      func() int { _, _, l, _ := runtime.Caller(1); return l }(),
   474  		benchmark: true,
   475  	},
   476  	{
   477  		name:      "13th bit is not setted",
   478  		input:     [8]byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},
   479  		expected:  []byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},
   480  		line:      func() int { _, _, l, _ := runtime.Caller(1); return l }(),
   481  		benchmark: false,
   482  	},
   483  }
   484  
   485  func TestBitmapMarshalISO8583(t *testing.T) {
   486  	_, testFile, _, _ := runtime.Caller(0)
   487  	for _, tc := range BitmapMarshalISO8583TestCases {
   488  		tc := tc
   489  		t.Run(fmt.Sprintf("%s %s", tc.name, strconv.Itoa(tc.line)), func(t *testing.T) {
   490  			t.Parallel()
   491  			linkToExample := fmt.Sprintf("%s:%d", testFile, tc.line)
   492  			b := tsysbitmap64r.Bitmap(tc.input)
   493  			p, err := b.MarshalISO8583()
   494  			if err != nil {
   495  				t.Fatalf("unexpected error: %#v - %s", err, linkToExample)
   496  			}
   497  			if !bytes.Equal(p, tc.expected) {
   498  				t.Errorf("[marshal ISO 8583] expected: %#v, received: %#v - %s", tc.expected, p, linkToExample)
   499  			}
   500  		})
   501  	}
   502  }
   503  
   504  func BenchmarkBitmapMarshalISO8583(b *testing.B) {
   505  	b.ReportAllocs()
   506  	for _, tc := range BitmapMarshalISO8583TestCases {
   507  		if !tc.benchmark {
   508  			continue
   509  		}
   510  		b.Run(strconv.Itoa(tc.line), func(b *testing.B) {
   511  			for i := 0; i < b.N; i++ {
   512  				b := tsysbitmap64r.Bitmap(tc.input)
   513  				_, err := b.MarshalISO8583()
   514  				if err != nil {
   515  					log.Fatal(err)
   516  				}
   517  			}
   518  		})
   519  	}
   520  }