1// Copyright 2021 Google Inc. 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
15package parser
16
17import "testing"
18
19func Test_numericStringLess(t *testing.T) {
20	type args struct {
21		a string
22		b string
23	}
24	tests := []struct {
25		a, b string
26	}{
27		{"a", "b"},
28		{"aa", "ab"},
29		{"aaa", "aba"},
30
31		{"1", "2"},
32		{"1", "11"},
33		{"2", "11"},
34		{"1", "12"},
35
36		{"12", "101"},
37		{"11", "102"},
38
39		{"0", "1"},
40		{"0", "01"},
41		{"1", "02"},
42		{"01", "002"},
43		{"001", "02"},
44	}
45
46	oneTest := func(a, b string, want bool) {
47		t.Helper()
48		if got := numericStringLess(a, b); got != want {
49			t.Errorf("want numericStringLess(%v, %v) = %v, got %v", a, b, want, got)
50		}
51	}
52
53	for _, tt := range tests {
54		t.Run(tt.a+"<"+tt.b, func(t *testing.T) {
55			// a should be less than b
56			oneTest(tt.a, tt.b, true)
57			// b should not be less than a
58			oneTest(tt.b, tt.a, false)
59			// a should not be less than a
60			oneTest(tt.a, tt.a, false)
61			// b should not be less than b
62			oneTest(tt.b, tt.b, false)
63
64			// The same should be true both strings are prefixed with an "a"
65			oneTest("a"+tt.a, "a"+tt.b, true)
66			oneTest("a"+tt.b, "a"+tt.a, false)
67			oneTest("a"+tt.a, "a"+tt.a, false)
68			oneTest("a"+tt.b, "a"+tt.b, false)
69
70			// The same should be true both strings are suffixed with an "a"
71			oneTest(tt.a+"a", tt.b+"a", true)
72			oneTest(tt.b+"a", tt.a+"a", false)
73			oneTest(tt.a+"a", tt.a+"a", false)
74			oneTest(tt.b+"a", tt.b+"a", false)
75
76			// The same should be true both strings are suffixed with a "1"
77			oneTest(tt.a+"1", tt.b+"1", true)
78			oneTest(tt.b+"1", tt.a+"1", false)
79			oneTest(tt.a+"1", tt.a+"1", false)
80			oneTest(tt.b+"1", tt.b+"1", false)
81
82			// The same should be true both strings are prefixed with a "0"
83			oneTest("0"+tt.a, "0"+tt.b, true)
84			oneTest("0"+tt.b, "0"+tt.a, false)
85			oneTest("0"+tt.a, "0"+tt.a, false)
86			oneTest("0"+tt.b, "0"+tt.b, false)
87
88			// The same should be true both strings are suffixed with a "0"
89			oneTest(tt.a+"0", tt.b+"0", true)
90			oneTest(tt.b+"0", tt.a+"0", false)
91			oneTest(tt.a+"0", tt.a+"0", false)
92			oneTest(tt.b+"0", tt.b+"0", false)
93
94		})
95	}
96}
97