kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/go/util/vnameutil/order_test.go (about)

     1  /*
     2   * Copyright 2017 The Kythe Authors. All rights reserved.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *   http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package vnameutil
    18  
    19  import (
    20  	"testing"
    21  
    22  	spb "kythe.io/kythe/proto/storage_go_proto"
    23  )
    24  
    25  func TestOrder(t *testing.T) {
    26  	tests := []struct {
    27  		a, b VString
    28  		want int
    29  	}{
    30  		{"_____", "_____", 0},
    31  		{"AB_DE", "AB_DE", 0},
    32  		{"_____", "A____", -1},
    33  		{"ABCDE", "ABCDF", -1},
    34  		{"ABCDE", "ABCDD", 1},
    35  		{"B____", "A____", 1},
    36  		{"AAAA_", "AAAAA", -1},
    37  		{"_ABCD", "_ACCD", -1},
    38  	}
    39  
    40  	for _, test := range tests {
    41  		got := Compare(test.a.vname(), test.b.vname())
    42  		if got != test.want {
    43  			t.Errorf("Compare(%q, %q): got %d, want %d", test.a, test.b, got, test.want)
    44  		}
    45  	}
    46  }
    47  
    48  // VString is a compact representation of a vname for test construction.
    49  //
    50  // Valid values have 1 character per vname field: corpus, lang, path, root,
    51  // sig.  The field value is that character, except underscore (_) which is
    52  // turned into an empty string. Missing letters on the right are blanks.
    53  type VString string
    54  
    55  func (v VString) vname() *spb.VName {
    56  	var out spb.VName
    57  	ptrs := [...]*string{&out.Corpus, &out.Language, &out.Path, &out.Root, &out.Signature}
    58  
    59  	for i, ch := range v {
    60  		ptr := ptrs[i]
    61  		if ch == '_' {
    62  			*ptr = ""
    63  		} else {
    64  			*ptr = string(ch)
    65  		}
    66  	}
    67  	return &out
    68  }