github.com/muyo/sno@v1.2.1/global_test.go (about)

     1  package sno
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  )
     7  
     8  func TestGlobal_Init(t *testing.T) {
     9  	t.Run("sane", func(t *testing.T) {
    10  		defer func() {
    11  			if err := recover(); err != nil {
    12  				t.Fatal("expected init to not panic")
    13  			}
    14  		}()
    15  
    16  		// Must never panic.
    17  		doInit()
    18  	})
    19  
    20  	t.Run("panics", func(t *testing.T) {
    21  		defer func() {
    22  			err := recover()
    23  			if err == nil {
    24  				t.Fatal("expected init to panic")
    25  			}
    26  
    27  			if _, ok := err.(*PartitionPoolExhaustedError); !ok {
    28  				t.Errorf("expected panic with type [%T], got [%T]", &PartitionPoolExhaustedError{}, err)
    29  				return
    30  			}
    31  		}()
    32  
    33  		// Theoretically impossible to happen but ensure that we cover all "potential" cases
    34  		// where the global generator could fail to get constructed and we need to panic.
    35  		//
    36  		// At present only one branch even has an error return, so we simulate that... impossibility
    37  		// by trying to create more Generators without snapshots than we have a Partition pool for.
    38  		// Note that we are invoking doInit() instead of NewGenerator() directly.
    39  		for i := 0; i < 2*MaxPartition; i++ {
    40  			doInit()
    41  		}
    42  	})
    43  }
    44  
    45  func TestGlobal_FromEncodedString_Valid(t *testing.T) {
    46  	src := "brpk4q72xwf2m63l"
    47  	expected := ID{78, 111, 33, 96, 160, 255, 154, 10, 16, 51}
    48  
    49  	actual, err := FromEncodedString(src)
    50  	if err != nil {
    51  		t.Fatal(err)
    52  	}
    53  
    54  	if actual != expected {
    55  		t.Errorf("expected [%v], got [%v]", expected, actual)
    56  	}
    57  }
    58  
    59  func TestGlobal_FromEncodedString_Invalid(t *testing.T) {
    60  	_, err := FromEncodedString("012brpk4q72xwf2m63l1245453gfdgxz")
    61  
    62  	if _, ok := err.(*InvalidDataSizeError); !ok {
    63  		t.Errorf("expected error with type [%T], got [%T]", &InvalidDataSizeError{}, err)
    64  	}
    65  
    66  	if err != nil && err.Error() != errInvalidDataSizeMsg {
    67  		t.Errorf("expected error [%s], got [%s]", errInvalidDataSizeMsg, err.Error())
    68  	}
    69  }
    70  
    71  func TestGlobal_FromEncodedBytes_Valid(t *testing.T) {
    72  	src := []byte("brpk4q72xwf2m63l")
    73  	expected := ID{78, 111, 33, 96, 160, 255, 154, 10, 16, 51}
    74  
    75  	actual, err := FromEncodedBytes(src)
    76  	if err != nil {
    77  		t.Fatal(err)
    78  	}
    79  
    80  	if actual != expected {
    81  		t.Errorf("expected [%v], got [%v]", expected, actual)
    82  	}
    83  }
    84  
    85  func TestGlobal_FromEncodedBytes_Invalid(t *testing.T) {
    86  	_, err := FromEncodedBytes([]byte("012brpk4q72xwf2m63l1245453gfdgxz"))
    87  
    88  	if _, ok := err.(*InvalidDataSizeError); !ok {
    89  		t.Errorf("expected error with type [%T], got [%T]", &InvalidDataSizeError{}, err)
    90  	}
    91  
    92  	if err != nil && err.Error() != errInvalidDataSizeMsg {
    93  		t.Errorf("expected error [%s], got [%s]", errInvalidDataSizeMsg, err.Error())
    94  	}
    95  }
    96  
    97  func TestGlobal_FromBinaryBytes_Valid(t *testing.T) {
    98  	src := []byte{78, 111, 33, 96, 160, 255, 154, 10, 16, 51}
    99  	expected := ID{78, 111, 33, 96, 160, 255, 154, 10, 16, 51}
   100  
   101  	actual, err := FromBinaryBytes(src)
   102  	if err != nil {
   103  		t.Fatal(err)
   104  	}
   105  
   106  	if actual != expected {
   107  		t.Errorf("expected [%v], got [%v]", expected, actual)
   108  	}
   109  }
   110  
   111  func TestGlobal_FromBinaryBytes_Invariant(t *testing.T) {
   112  	expected := New(255)
   113  	actual, err := FromBinaryBytes(expected[:])
   114  	if err != nil {
   115  		t.Fatal(err)
   116  	}
   117  
   118  	if actual != expected {
   119  		t.Errorf("expected [%v], got [%v]", expected, actual)
   120  	}
   121  }
   122  
   123  func TestGlobal_FromBinaryBytes_Invalid(t *testing.T) {
   124  	for _, c := range []struct {
   125  		n       int
   126  		invalid bool
   127  	}{
   128  		{4, true},
   129  		{8, true},
   130  		{10, false},
   131  		{12, true},
   132  		{16, true},
   133  	} {
   134  		b := make([]byte, c.n)
   135  		_, err := FromBinaryBytes(b)
   136  
   137  		if actual, expected := err != nil, c.invalid; actual != expected {
   138  			t.Errorf("expected error [%v], got [%v]", expected, actual)
   139  		}
   140  	}
   141  }
   142  
   143  func TestGlobal_Collection(t *testing.T) {
   144  	var ids = []ID{{1}, {2}, {3}, {4}, {5}, {6}}
   145  
   146  	t.Run("len", makeCollectionLenTest(ids))
   147  	t.Run("less", makeCollectionLessTest(ids))
   148  	t.Run("swap", makeCollectionSwapTest(ids))
   149  	t.Run("sort", makeCollectionSortTest(ids))
   150  }
   151  
   152  func makeCollectionLenTest(ids []ID) func(t *testing.T) {
   153  	n := len(ids)
   154  	return func(t *testing.T) {
   155  		if actual, expected := collection([]ID{}).Len(), 0; actual != expected {
   156  			t.Errorf("Len() %v, want %v", expected, actual)
   157  		}
   158  
   159  		if actual, expected := collection(ids).Len(), n; actual != expected {
   160  			t.Errorf("expected [%v], got [%v]", expected, actual)
   161  		}
   162  	}
   163  }
   164  
   165  func makeCollectionLessTest(ids []ID) func(t *testing.T) {
   166  	return func(t *testing.T) {
   167  		c := collection(ids)
   168  		if c.Less(0, 0) {
   169  			t.Errorf("expected [false], got [true]")
   170  		}
   171  
   172  		if !c.Less(0, 1) {
   173  			t.Errorf("expected [true], got [false]")
   174  		}
   175  
   176  		if !c.Less(1, 2) {
   177  			t.Errorf("expected [true], got [false]")
   178  		}
   179  	}
   180  }
   181  
   182  func makeCollectionSwapTest(ids []ID) func(t *testing.T) {
   183  	return func(t *testing.T) {
   184  		b := make([]ID, len(ids))
   185  		copy(b, ids)
   186  
   187  		c := collection(b)
   188  		c.Swap(1, 2)
   189  		if actual, expected := c[1], ids[2]; actual != expected {
   190  			t.Errorf("expected [%v], got [%v]", expected, actual)
   191  		}
   192  		if actual, expected := c[2], ids[1]; actual != expected {
   193  			t.Errorf("expected [%v], got [%v]", expected, actual)
   194  		}
   195  		c.Swap(3, 3)
   196  		if actual, expected := c[3], ids[3]; actual != expected {
   197  			t.Errorf("expected [%v], got [%v]", expected, actual)
   198  		}
   199  	}
   200  }
   201  
   202  func makeCollectionSortTest(ids []ID) func(t *testing.T) {
   203  	return func(t *testing.T) {
   204  		src := make([]ID, len(ids))
   205  		copy(src, ids)
   206  
   207  		// Input IDs are sorted, so a comparison will do the trick.
   208  		src[2], src[1] = src[1], src[2]
   209  		src[4], src[3] = src[3], src[4]
   210  
   211  		Sort(src)
   212  
   213  		if actual, expected := src, ids; !reflect.DeepEqual(actual, expected) {
   214  			t.Errorf("expected [%v], got [%v]", expected, actual)
   215  		}
   216  	}
   217  }
   218  
   219  func TestGlobal_Zero(t *testing.T) {
   220  	if actual := Zero(); actual != (ID{}) {
   221  		t.Error("Zero() not equal to ID{}")
   222  	}
   223  }
   224  
   225  func TestGlobal_Zero_IsZero(t *testing.T) {
   226  	if !Zero().IsZero() {
   227  		t.Error("Zero().IsZero() is not true")
   228  	}
   229  }