github.com/thepudds/swisstable@v0.0.0-20221011152303-9c77dc657777/match_test.go (about)

     1  package swisstable
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  )
     7  
     8  func TestMatchByte(t *testing.T) {
     9  	tests := []struct {
    10  		name     string
    11  		c        uint8
    12  		buffer   []byte
    13  		wantMask uint32
    14  		wantOk   bool
    15  	}{
    16  		{
    17  			"match 3",
    18  			42,
    19  			[]byte{42, 0, 0, 42, 42, 0, 17, 17, 0, 0, 0, 0, 0, 0, 0, 0},
    20  			1<<0 | 1<<3 | 1<<4,
    21  			true,
    22  		},
    23  		{
    24  			"match 1 at end",
    25  			42,
    26  			[]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 42},
    27  			1 << 15,
    28  			true,
    29  		},
    30  		{
    31  			"match 2 at start and end",
    32  			42,
    33  			[]byte{42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 42},
    34  			1<<0 | 1<<15,
    35  			true,
    36  		},
    37  		{
    38  			"match all",
    39  			42,
    40  			[]byte{42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42},
    41  			1<<16 - 1,
    42  			true,
    43  		},
    44  		{
    45  			"match none - no match",
    46  			255,
    47  			[]byte{42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 42},
    48  			0,
    49  			true,
    50  		},
    51  		{
    52  			"match none - len short by 1",
    53  			42,
    54  			[]byte{42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    55  			0,
    56  			false,
    57  		},
    58  	}
    59  	for _, tt := range tests {
    60  		t.Run(tt.name, func(t *testing.T) {
    61  			gotMask, gotOk := MatchByte(tt.c, tt.buffer)
    62  			if gotMask != tt.wantMask {
    63  				t.Errorf("MatchByte() gotMask = %v, want %v", gotMask, tt.wantMask)
    64  			}
    65  			if gotOk != tt.wantOk {
    66  				t.Errorf("MatchByte() gotOk = %v, want %v", gotOk, tt.wantOk)
    67  			}
    68  		})
    69  	}
    70  }
    71  
    72  func TestMatchByteAlignment(t *testing.T) {
    73  	tests := []struct {
    74  		name     string
    75  		c        uint8
    76  		buffer   []byte
    77  		wantMask uint32
    78  		wantOk   bool
    79  	}{
    80  		{
    81  			"match all",
    82  			42,
    83  			bytes.Repeat([]byte{42}, 10000),
    84  			1<<16 - 1,
    85  			true,
    86  		},
    87  		{
    88  			"match none",
    89  			255,
    90  			bytes.Repeat([]byte{42}, 10000),
    91  			0,
    92  			true,
    93  		},
    94  	}
    95  	for _, tt := range tests {
    96  		t.Run(tt.name, func(t *testing.T) {
    97  			for i := 0; i < len(tt.buffer)-16; i++ {
    98  				buffer := tt.buffer[i : i+16]
    99  
   100  				gotMask, gotOk := MatchByte(tt.c, buffer)
   101  				if gotMask != tt.wantMask {
   102  					t.Fatalf("MatchByte() offset %d gotMask = %v, want %v", i, gotMask, tt.wantMask)
   103  				}
   104  				if gotOk != tt.wantOk {
   105  					t.Fatalf("MatchByte() offset %d gotOk = %v, want %v", i, gotOk, tt.wantOk)
   106  				}
   107  			}
   108  		})
   109  	}
   110  }