func TestUpdater(t *testing.T) { sml := SML.New() orig_data := []int{1, 2, 3} var double_data [3]int for idx, value := range orig_data { sml.Append(value) double_data[idx] = value * 2 } for idx := 0; idx < sml.Len(); idx++ { sml.Update(idx, func(orig interface{}) interface{} { return orig.(int) * 2 }) } sml_data := sml.Close() if len(sml_data) != len(double_data) { t.Errorf("there are something wrong!, len(sml_data):%d, len(double_data):%d", len(sml_data), len(double_data)) return } for idx, value := range double_data { if value != sml_data[idx] { t.Errorf("sml_orig invalid! idx:%d, sml:%d, should:%d", idx, sml_data[idx], value) return } } }
func TestDelete(t *testing.T) { sml := SML.New() sml.Append(0) sml.Append(1) sml.Append(2) sml.Delete(0) value, exist := sml.At(2) if exist { t.Errorf("append and delete failed, idx:%d, should not exist", 2) return } value, exist = sml.At(0) if !exist { t.Errorf("append and delete failed, idx:%d, should exist", 0) return } if int_value, ok := value.(int); !ok || int_value != 1 { t.Errorf("append and at failed, idx:%d, ok:%b, int_value:%d", 0, ok, int_value) return } }
func TestAppend(t *testing.T) { sml := SML.New() sml.Append(1) sml.Append(2) sml.Append(3) length := sml.Len() if length != 3 { t.Errorf("append item failed, len:%d, should:%d", length, 3) return } storage := sml.Close() if len(storage) != 3 { t.Errorf("append item failed, storage_len:%d, should:%d", len(storage), 3) return } for idx, item := range storage { if item.(int) != idx+1 { t.Errorf("append item failed, idx:%d, value:%d, should:%d", idx, item.(int), idx+1) return } } }
func TestAt(t *testing.T) { sml := SML.New() sml.Append(0) sml.Append(1) sml.Append(2) sml.Append(3) value, exist := sml.At(2) if !exist { t.Errorf("append and at failed, idx:%d", 2) return } if int_value, ok := value.(int); !ok || int_value != 2 { t.Errorf("append and at failed, idx:%d, ok:%b, int_value:%d", 2, ok, int_value) return } }
func TestTotal(t *testing.T) { store := SML.New() fmt.Printf("Initially has %d items\n", store.Len()) deleted := []int{0, 2, 3, 5, 7, 20, 399, 25, 30, 1000, 91, 97, 98, 99} var waiter sync.WaitGroup waiter.Add(1) go func() { // Concurrent Inserter for i := 0; i < 100; i++ { store.Append(fmt.Sprintf("%04X", i)) if i > 0 && i%15 == 0 { fmt.Printf("Inserted %d items\n", store.Len()) } } fmt.Printf("Inserted %d items\n", store.Len()) waiter.Done() }() waiter.Add(1) go func() { // Concurrent Deleter for _, i := range deleted { before := store.Len() store.Delete(i) fmt.Printf("Deleted m[%d] before=%d after=%d\n", i, before, store.Len()) } waiter.Done() }() waiter.Add(1) go func() { // Concurrent Finder for _, i := range deleted { for _, j := range []int{i, i + 1} { item, _ := store.At(j) if item != nil { fmt.Printf("Found m[%d] == %s\n", j, item) } else { fmt.Printf("Not found m[%d]\n", j) } } } waiter.Done() }() waiter.Wait() fmt.Printf("Finished with %d items\n", store.Len()) updater := func(value interface{}) interface{} { return value.(string) + ":updated" } for i := 0; i < store.Len() && i < 5; i++ { value, _ := store.At(i) fmt.Printf("m[%d] == %s -> ", i, value) store.Update(i, updater) value, _ = store.At(i) fmt.Printf("%s\n", value) } list := store.Close() fmt.Println("Closed") fmt.Printf("len == %d\n", len(list)) fmt.Println() }