github.com/richardwilkes/toolbox@v1.121.0/collection/slice/column_sort_test.go (about)

     1  // Copyright (c) 2016-2024 by Richard A. Wilkes. All rights reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the Mozilla Public
     4  // License, version 2.0. If a copy of the MPL was not distributed with
     5  // this file, You can obtain one at http://mozilla.org/MPL/2.0/.
     6  //
     7  // This Source Code Form is "Incompatible With Secondary Licenses", as
     8  // defined by the Mozilla Public License, version 2.0.
     9  
    10  package slice_test
    11  
    12  import (
    13  	"cmp"
    14  	"testing"
    15  
    16  	"github.com/richardwilkes/toolbox/check"
    17  	"github.com/richardwilkes/toolbox/collection/slice"
    18  )
    19  
    20  func TestColumnSort(t *testing.T) {
    21  	s := []int{0, 1, 2, 3, 4, 5, 6}
    22  	slice.ColumnSort(s, 2, cmp.Compare)
    23  	// 0 4
    24  	// 1 5
    25  	// 2 6
    26  	// 3
    27  	check.Equal(t, []int{0, 4, 1, 5, 2, 6, 3}, s)
    28  
    29  	slice.ColumnSort(s, 3, cmp.Compare)
    30  	// 0 3 5
    31  	// 1 4 6
    32  	// 2
    33  	check.Equal(t, []int{0, 3, 5, 1, 4, 6, 2}, s)
    34  
    35  	s = []int{0, 1, 2, 3, 4, 5}
    36  	slice.ColumnSort(s, 2, cmp.Compare)
    37  	// 0 3
    38  	// 1 4
    39  	// 2 5
    40  	check.Equal(t, []int{0, 3, 1, 4, 2, 5}, s)
    41  
    42  	slice.ColumnSort(s, 4, cmp.Compare)
    43  	// 0 2 4 5
    44  	// 1 3
    45  	check.Equal(t, []int{0, 2, 4, 5, 1, 3}, s)
    46  
    47  	slice.ColumnSort(s, 10, cmp.Compare)
    48  	// 0 1 2 3 4 5
    49  	check.Equal(t, []int{0, 1, 2, 3, 4, 5}, s)
    50  
    51  	s = []int{0, 1, 2, 3, 4, 5, 6, 7}
    52  	slice.ColumnSort(s, 3, cmp.Compare)
    53  	// 0 3 6
    54  	// 1 4 7
    55  	// 2 5
    56  	check.Equal(t, []int{0, 3, 6, 1, 4, 7, 2, 5}, s)
    57  }