mirror of
https://github.com/johnkerl/miller.git
synced 2026-01-23 10:15:36 +00:00
miller/types/mlrval_functions_test.go iterate
This commit is contained in:
parent
a49b0b69d2
commit
12fa4f63a1
5 changed files with 116 additions and 12 deletions
11
go/check
11
go/check
|
|
@ -8,6 +8,17 @@ fi
|
|||
|
||||
set -euo pipefail
|
||||
|
||||
# ----------------------------------------------------------------
|
||||
# GO-SOURCE TESTS
|
||||
|
||||
# go test -v ./... doesn't work since it triggers unready things in
|
||||
# src/github.com/goccmack
|
||||
|
||||
export GOPATH=$(pwd)
|
||||
go test miller/types
|
||||
|
||||
# ----------------------------------------------------------------
|
||||
# COMMAND-LINE TESTS
|
||||
$runner u/try-io > u/try-io.out
|
||||
$runner u/try-chain > u/try-chain.out
|
||||
$runner u/try-verbs > u/try-verbs.out
|
||||
|
|
|
|||
|
|
@ -242,7 +242,7 @@ func mainUsageDataFormatExamples(o *os.File, argv0 string) {
|
|||
}
|
||||
|
||||
func mainUsageDataFormatOptions(o *os.File, argv0 string) {
|
||||
fmt.Fprintf(o,
|
||||
fmt.Fprintln(o,
|
||||
`
|
||||
--idkvp --odkvp --dkvp Delimited key-value pairs, e.g "a=1,b=2"
|
||||
(this is Miller's default format).
|
||||
|
|
|
|||
|
|
@ -95,35 +95,35 @@ func mapperSortParseCLI(
|
|||
subList := lib.SplitString(args[argi+1], ",")
|
||||
for _, item := range subList {
|
||||
groupByFieldNameList = append(groupByFieldNameList, item)
|
||||
comparatorFuncs = append(comparatorFuncs, types.LexicalAscendingComparatorfunc)
|
||||
comparatorFuncs = append(comparatorFuncs, types.LexicalAscendingComparator)
|
||||
}
|
||||
argi += 2
|
||||
} else if args[argi] == "-r" {
|
||||
subList := lib.SplitString(args[argi+1], ",")
|
||||
for _, item := range subList {
|
||||
groupByFieldNameList = append(groupByFieldNameList, item)
|
||||
comparatorFuncs = append(comparatorFuncs, types.LexicalDescendingComparatorfunc)
|
||||
comparatorFuncs = append(comparatorFuncs, types.LexicalDescendingComparator)
|
||||
}
|
||||
argi += 2
|
||||
} else if args[argi] == "-n" {
|
||||
subList := lib.SplitString(args[argi+1], ",")
|
||||
for _, item := range subList {
|
||||
groupByFieldNameList = append(groupByFieldNameList, item)
|
||||
comparatorFuncs = append(comparatorFuncs, types.NumericAscendingComparatorfunc)
|
||||
comparatorFuncs = append(comparatorFuncs, types.NumericAscendingComparator)
|
||||
}
|
||||
argi += 2
|
||||
} else if args[argi] == "-nf" {
|
||||
subList := lib.SplitString(args[argi+1], ",")
|
||||
for _, item := range subList {
|
||||
groupByFieldNameList = append(groupByFieldNameList, item)
|
||||
comparatorFuncs = append(comparatorFuncs, types.NumericAscendingComparatorfunc)
|
||||
comparatorFuncs = append(comparatorFuncs, types.NumericAscendingComparator)
|
||||
}
|
||||
argi += 2
|
||||
} else if args[argi] == "-nr" {
|
||||
subList := lib.SplitString(args[argi+1], ",")
|
||||
for _, item := range subList {
|
||||
groupByFieldNameList = append(groupByFieldNameList, item)
|
||||
comparatorFuncs = append(comparatorFuncs, types.NumericDescendingComparatorfunc)
|
||||
comparatorFuncs = append(comparatorFuncs, types.NumericDescendingComparator)
|
||||
}
|
||||
argi += 2
|
||||
|
||||
|
|
|
|||
|
|
@ -1565,7 +1565,7 @@ func MlrvalVariadicMax(mlrvals []*Mlrval) Mlrval {
|
|||
// For sorting
|
||||
|
||||
// Lexical sort: just stringify everything.
|
||||
func LexicalAscendingComparatorfunc(ma *Mlrval, mb *Mlrval) int {
|
||||
func LexicalAscendingComparator(ma *Mlrval, mb *Mlrval) int {
|
||||
sa := ma.String()
|
||||
sb := mb.String()
|
||||
if sa < sb {
|
||||
|
|
@ -1576,8 +1576,8 @@ func LexicalAscendingComparatorfunc(ma *Mlrval, mb *Mlrval) int {
|
|||
return 0
|
||||
}
|
||||
}
|
||||
func LexicalDescendingComparatorfunc(ma *Mlrval, mb *Mlrval) int {
|
||||
return LexicalAscendingComparatorfunc(mb, ma)
|
||||
func LexicalDescendingComparator(ma *Mlrval, mb *Mlrval) int {
|
||||
return LexicalAscendingComparator(mb, ma)
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------
|
||||
|
|
@ -1694,9 +1694,9 @@ var num_cmp_dispositions = [MT_DIM][MT_DIM]ComparatorFunc{
|
|||
/*MAP */ {_zero, _zero, _zero, _zero, _zero, _zero, _zero, _zero, _zero},
|
||||
}
|
||||
|
||||
func NumericAscendingComparatorfunc(ma *Mlrval, mb *Mlrval) int {
|
||||
func NumericAscendingComparator(ma *Mlrval, mb *Mlrval) int {
|
||||
return num_cmp_dispositions[ma.mvtype][mb.mvtype](ma, mb)
|
||||
}
|
||||
func NumericDescendingComparatorfunc(ma *Mlrval, mb *Mlrval) int {
|
||||
return NumericAscendingComparatorfunc(mb, ma)
|
||||
func NumericDescendingComparator(ma *Mlrval, mb *Mlrval) int {
|
||||
return NumericAscendingComparator(mb, ma)
|
||||
}
|
||||
|
|
|
|||
93
go/src/miller/types/mlrval_functions_test.go
Normal file
93
go/src/miller/types/mlrval_functions_test.go
Normal file
|
|
@ -0,0 +1,93 @@
|
|||
package types
|
||||
|
||||
// ================================================================
|
||||
// Most Miller tests (thousands of them) are command-line-driven via
|
||||
// reg_test/run. Here are some cases needing special focus.
|
||||
// ================================================================
|
||||
|
||||
import (
|
||||
"testing"
|
||||
//"types"
|
||||
)
|
||||
|
||||
// ----------------------------------------------------------------
|
||||
func TestLexicalAscendingComparator(t *testing.T) {
|
||||
var a = MlrvalFromInt64(10)
|
||||
var b = MlrvalFromInt64(2)
|
||||
if LexicalAscendingComparator(&a, &b) != -1 {
|
||||
t.Fatal()
|
||||
}
|
||||
|
||||
a = MlrvalFromString("abc")
|
||||
b = MlrvalFromString("def")
|
||||
if LexicalAscendingComparator(&a, &b) != -1 {
|
||||
t.Fatal()
|
||||
}
|
||||
|
||||
a = MlrvalFromInt64(3)
|
||||
b = MlrvalFromBool(true)
|
||||
if LexicalAscendingComparator(&a, &b) != -1 {
|
||||
t.Fatal()
|
||||
}
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------
|
||||
// SORTING
|
||||
// Sort rules (same for min, max, and comparator):
|
||||
// * NUMERICS < BOOL < STRINGS < ERROR < ABSENT
|
||||
// * error == error (singleton type)
|
||||
// * absent == absent (singleton type)
|
||||
// * string compares on strings
|
||||
// * numeric compares on numbers
|
||||
// * false < true
|
||||
|
||||
func TestNumericAscendingComparator(t *testing.T) {
|
||||
i10 := MlrvalFromInt64(10)
|
||||
i2 := MlrvalFromInt64(2)
|
||||
|
||||
bfalse := MlrvalFromBool(false)
|
||||
btrue := MlrvalFromBool(true)
|
||||
|
||||
sabc := MlrvalFromString("abc")
|
||||
sdef := MlrvalFromString("def")
|
||||
|
||||
e := MlrvalFromError()
|
||||
|
||||
a := MlrvalFromAbsent()
|
||||
|
||||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||
// Within-type comparisons
|
||||
if NumericAscendingComparator(&i10, &i2) != 1 {
|
||||
t.Fatal()
|
||||
}
|
||||
if NumericAscendingComparator(&sabc, &sabc) != 0 {
|
||||
t.Fatal()
|
||||
}
|
||||
if NumericAscendingComparator(&sabc, &sdef) != -1 {
|
||||
t.Fatal()
|
||||
}
|
||||
|
||||
if NumericAscendingComparator(&btrue, &bfalse) != 1 {
|
||||
t.Fatal()
|
||||
}
|
||||
|
||||
if NumericAscendingComparator(&e, &e) != 0 {
|
||||
t.Fatal()
|
||||
}
|
||||
if NumericAscendingComparator(&a, &a) != 0 {
|
||||
t.Fatal()
|
||||
}
|
||||
// xxx more
|
||||
|
||||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||
// Across-type comparisons
|
||||
|
||||
if NumericAscendingComparator(&i10, &btrue) != -1 {
|
||||
t.Fatal()
|
||||
}
|
||||
if NumericAscendingComparator(&e, &a) != -1 {
|
||||
t.Fatal()
|
||||
}
|
||||
|
||||
// xxx more
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue