github.heygears.com/openimsdk/tools@v0.0.49/utils/splitter/splitter_test.go (about)

     1  // Copyright © 2023 OpenIM. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package splitter
    16  
    17  import (
    18  	"reflect"
    19  	"testing"
    20  )
    21  
    22  func TestNewSplitter(t *testing.T) {
    23  	data := []string{"a", "b", "c", "d", "e", "f"}
    24  	splitCount := 2
    25  	splitter := NewSplitter(splitCount, data)
    26  	if splitter.splitCount != splitCount {
    27  		t.Errorf("Expected splitCount %d, got %d", splitCount, splitter.splitCount)
    28  	}
    29  	if !reflect.DeepEqual(splitter.data, data) {
    30  		t.Errorf("Expected data %v, got %v", data, splitter.data)
    31  	}
    32  }
    33  
    34  func TestGetSplitResult_EvenSplit(t *testing.T) {
    35  	data := []string{"a", "b", "c", "d"}
    36  	splitCount := 2
    37  	splitter := NewSplitter(splitCount, data)
    38  	expected := []*SplitResult{
    39  		{Item: []string{"a", "b"}},
    40  		{Item: []string{"c", "d"}},
    41  	}
    42  	result := splitter.GetSplitResult()
    43  	if !reflect.DeepEqual(result, expected) {
    44  		t.Errorf("Expected result %v, got %v", expected, result)
    45  	}
    46  }
    47  
    48  func TestGetSplitResult_OddSplit(t *testing.T) {
    49  	data := []string{"a", "b", "c", "d", "e"}
    50  	splitCount := 2
    51  	splitter := NewSplitter(splitCount, data)
    52  	expected := []*SplitResult{
    53  		{Item: []string{"a", "b"}},
    54  		{Item: []string{"c", "d"}},
    55  		{Item: []string{"e"}},
    56  	}
    57  	result := splitter.GetSplitResult()
    58  	if !reflect.DeepEqual(result, expected) {
    59  		t.Errorf("Expected result %v, got %v", expected, result)
    60  	}
    61  }
    62  
    63  func TestGetSplitResult_SingleElement(t *testing.T) {
    64  	data := []string{"a"}
    65  	splitCount := 2
    66  	splitter := NewSplitter(splitCount, data)
    67  	expected := []*SplitResult{{Item: []string{"a"}}}
    68  	result := splitter.GetSplitResult()
    69  	if !reflect.DeepEqual(result, expected) {
    70  		t.Errorf("Expected result %v, got %v", expected, result)
    71  	}
    72  }