github.com/hata/goseq@v0.0.0-20150316021154-a5ca66a92399/sequence_test.go (about)

     1  package goseq
     2  
     3  import "testing"
     4  
     5  func TestNewSequence(t *testing.T) {
     6  	seq := NewSequence()
     7  	if seq.Next() != 0 {
     8  		t.Error("New Sequence should generate 0 for the first id.")
     9  	}
    10  }
    11  
    12  func TestNewSequenceStruct(t *testing.T) {
    13  	seq := newSequence()
    14  	if seq.value != initialSequenceValue {
    15  		t.Error("Configured value should be the initial value")
    16  	}
    17  }
    18  
    19  func TestGet(t *testing.T) {
    20  	seq := NewSequence()
    21  	id := seq.Get()
    22  	if id != initialSequenceValue {
    23  		t.Error("Get() returns a wrong value.")
    24  	}
    25  }
    26  
    27  func TestNext(t *testing.T) {
    28  	seq := NewSequence()
    29  	id := seq.Next()
    30  	if id != 0 {
    31  		t.Error("First ID should be 0.")
    32  	}
    33  	id = seq.Next()
    34  	if id != 1 {
    35  		t.Error("Next() should increase 1")
    36  	}
    37  
    38  }
    39  
    40  func TestGetStruct(t *testing.T) {
    41  	seq := newSequence()
    42  	id := seq.get()
    43  	if id != initialSequenceValue {
    44  		t.Error("get() should return an initialSequenceValue.")
    45  	}
    46  }
    47  
    48  func TestSet(t *testing.T) {
    49  	seq := newSequence()
    50  	seq.set(1)
    51  	id := seq.get()
    52  	if id != 1 {
    53  		t.Error("set() should set a new value.")
    54  	}
    55  }
    56  
    57  func TestIncrementAndGet(t *testing.T) {
    58  	seq := newSequence()
    59  	id := seq.incrementAndGet()
    60  	if id != 0 {
    61  		t.Error("incrementAndGet() should increment 1 and return the value.")
    62  	}
    63  	id = seq.incrementAndGet()
    64  	if id != 1 {
    65  		t.Error("incrementAndGet() should increment 1 and return the value.")
    66  	}
    67  }
    68  
    69  func TestCompareAndSetAndSuccess(t *testing.T) {
    70  	seq := newSequence()
    71  	id := seq.incrementAndGet()
    72  	seq.compareAndSet(id, 2)
    73  	id2 := seq.get()
    74  	if id2 != 2 {
    75  		t.Error("compareAndSet should return ", 2)
    76  	}
    77  }
    78  
    79  func TestCompareAndSetAndIgnore(t *testing.T) {
    80  	seq := newSequence()
    81  	id := seq.incrementAndGet()
    82  	seq.compareAndSet(id+1, 2)
    83  	id2 := seq.get()
    84  	if id != id2 {
    85  		t.Error("compareAndSet should fail when expected is not match.")
    86  	}
    87  }
    88  
    89  func TestAddAndGet(t *testing.T) {
    90  	seq := newSequence()
    91  	seq.incrementAndGet()
    92  	id := seq.incrementAndGet()
    93  	id2 := seq.addAndGet(3)
    94  	if id2 != (id + 3) {
    95  		t.Error("addAndSet() should add delta and return it.")
    96  	}
    97  	if id2 != seq.get() {
    98  		t.Error("addAndSet() should set a new value.")
    99  	}
   100  }