@@ -578,7 +578,6 @@ func TestUUIDFieldIndex_PrefixFromArgs(t *testing.T) {
578
578
if ! bytes .Equal (uuidBuf [:9 ], val ) {
579
579
t .Fatalf ("foo" )
580
580
}
581
-
582
581
}
583
582
584
583
func BenchmarkUUIDFieldIndex_parseString (b * testing.B ) {
@@ -609,16 +608,16 @@ func generateUUID() ([]byte, string) {
609
608
func TestIntFieldIndex_FromObject (t * testing.T ) {
610
609
obj := testObj ()
611
610
612
- eint := make ([]byte , 10 )
613
- eint8 := make ([]byte , 2 )
614
- eint16 := make ([]byte , 3 )
615
- eint32 := make ([]byte , 5 )
616
- eint64 := make ([]byte , 10 )
617
- binary .PutVarint (eint , int64 ( obj . Int ) )
618
- binary . PutVarint ( eint8 , int64 ( obj . Int8 ))
619
- binary .PutVarint (eint16 , int64 ( obj . Int16 ) )
620
- binary .PutVarint (eint32 , int64 ( obj . Int32 ) )
621
- binary .PutVarint (eint64 , obj . Int64 )
611
+ eint := make ([]byte , 8 )
612
+ eint8 := make ([]byte , 1 )
613
+ eint16 := make ([]byte , 2 )
614
+ eint32 := make ([]byte , 4 )
615
+ eint64 := make ([]byte , 8 )
616
+ binary .BigEndian . PutUint64 (eint , 1 << 63 + 1 )
617
+ eint8 [ 0 ] = 0
618
+ binary .BigEndian . PutUint16 (eint16 , 0 )
619
+ binary .BigEndian . PutUint32 (eint32 , 0 )
620
+ binary .BigEndian . PutUint64 (eint64 , 0 )
622
621
623
622
cases := []struct {
624
623
Field string
@@ -659,7 +658,6 @@ func TestIntFieldIndex_FromObject(t *testing.T) {
659
658
t .Run (c .Field , func (t * testing.T ) {
660
659
indexer := IntFieldIndex {c .Field }
661
660
ok , val , err := indexer .FromObject (obj )
662
-
663
661
if err != nil {
664
662
if ok {
665
663
t .Fatalf ("okay and error" )
@@ -679,7 +677,6 @@ func TestIntFieldIndex_FromObject(t *testing.T) {
679
677
if ! bytes .Equal (val , c .Expected ) {
680
678
t .Fatalf ("bad: %#v %#v" , val , c .Expected )
681
679
}
682
-
683
680
})
684
681
}
685
682
}
@@ -702,16 +699,16 @@ func TestIntFieldIndex_FromArgs(t *testing.T) {
702
699
}
703
700
704
701
obj := testObj ()
705
- eint := make ([]byte , 10 )
706
- eint8 := make ([]byte , 2 )
707
- eint16 := make ([]byte , 3 )
708
- eint32 := make ([]byte , 5 )
709
- eint64 := make ([]byte , 10 )
710
- binary .PutVarint (eint , int64 ( obj . Int ) )
711
- binary . PutVarint ( eint8 , int64 ( obj . Int8 ))
712
- binary .PutVarint (eint16 , int64 ( obj . Int16 ) )
713
- binary .PutVarint (eint32 , int64 ( obj . Int32 ) )
714
- binary .PutVarint (eint64 , obj . Int64 )
702
+ eint := make ([]byte , 8 )
703
+ eint8 := make ([]byte , 1 )
704
+ eint16 := make ([]byte , 2 )
705
+ eint32 := make ([]byte , 4 )
706
+ eint64 := make ([]byte , 8 )
707
+ binary .BigEndian . PutUint64 (eint , 1 << 63 + 1 )
708
+ eint8 [ 0 ] = 0
709
+ binary .BigEndian . PutUint16 (eint16 , 0 )
710
+ binary .BigEndian . PutUint32 (eint32 , 0 )
711
+ binary .BigEndian . PutUint64 (eint64 , 0 )
715
712
716
713
val , err := indexer .FromArgs (obj .Int )
717
714
if err != nil {
@@ -754,6 +751,46 @@ func TestIntFieldIndex_FromArgs(t *testing.T) {
754
751
}
755
752
}
756
753
754
+ func TestIntFieldIndexSortability (t * testing.T ) {
755
+ testCases := []struct {
756
+ i8l int8
757
+ i8r int8
758
+ i16l int16
759
+ i16r int16
760
+ i32l int32
761
+ i32r int32
762
+ i64l int64
763
+ i64r int64
764
+ il int
765
+ ir int
766
+ expected int
767
+ name string
768
+ }{
769
+ {0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , "zero" },
770
+ {1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 0 , "small eq" },
771
+ {0 , 1 , 0 , 1 , 0 , 1 , 0 , 1 , 0 , 1 , - 1 , "small lt" },
772
+ {2 , 1 , 2 , 1 , 2 , 1 , 2 , 1 , 2 , 1 , 1 , "small gt" },
773
+ {- 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , 0 , "small neg eq" },
774
+ {- 2 , - 1 , - 2 , - 1 , - 2 , - 1 , - 2 , - 1 , - 2 , - 1 , - 1 , "small neg lt" },
775
+ {- 1 , - 2 , - 1 , - 2 , - 1 , - 2 , - 1 , - 2 , - 1 , - 2 , 1 , "small neg gt" },
776
+ {- 1 , 1 , - 1 , 1 , - 1 , 1 , - 1 , 1 , - 1 , 1 , - 1 , "neg vs pos" },
777
+ {- 128 , 127 , - 32768 , 32767 , - 2147483648 , 2147483647 , - 9223372036854775808 , 9223372036854775807 , - 9223372036854775808 , 9223372036854775807 , - 1 , "max conditions" },
778
+ {100 , 127 , 1000 , 2000 , 1000000000 , 2000000000 , 10000000000 , 20000000000 , 1000000000 , 2000000000 , - 1 , "large lt" },
779
+ {100 , 99 , 1000 , 999 , 1000000000 , 999999999 , 10000000000 , 9999999999 , 1000000000 , 999999999 , 1 , "large gt" },
780
+ {126 , 127 , 255 , 256 , 65535 , 65536 , 4294967295 , 4294967296 , 65535 , 65536 , - 1 , "edge conditions" },
781
+ }
782
+
783
+ for _ , tc := range testCases {
784
+ t .Run (tc .name , func (t * testing.T ) {
785
+ compareEncoded (t , & IntFieldIndex {"Foo" }, tc .i8l , tc .i8r , tc .expected )
786
+ compareEncoded (t , & IntFieldIndex {"Foo" }, tc .i16l , tc .i16r , tc .expected )
787
+ compareEncoded (t , & IntFieldIndex {"Foo" }, tc .i32l , tc .i32r , tc .expected )
788
+ compareEncoded (t , & IntFieldIndex {"Foo" }, tc .i64l , tc .i64r , tc .expected )
789
+ compareEncoded (t , & IntFieldIndex {"Foo" }, tc .il , tc .ir , tc .expected )
790
+ })
791
+ }
792
+ }
793
+
757
794
func TestUintFieldIndex_FromObject (t * testing.T ) {
758
795
obj := testObj ()
759
796
@@ -807,7 +844,6 @@ func TestUintFieldIndex_FromObject(t *testing.T) {
807
844
t .Run (c .Field , func (t * testing.T ) {
808
845
indexer := UintFieldIndex {c .Field }
809
846
ok , val , err := indexer .FromObject (obj )
810
-
811
847
if err != nil {
812
848
if ok {
813
849
t .Fatalf ("okay and error" )
@@ -827,7 +863,6 @@ func TestUintFieldIndex_FromObject(t *testing.T) {
827
863
if ! bytes .Equal (val , c .Expected ) {
828
864
t .Fatalf ("bad: %#v %#v" , val , c .Expected )
829
865
}
830
-
831
866
})
832
867
}
833
868
}
@@ -928,25 +963,23 @@ func TestUIntFieldIndexSortability(t *testing.T) {
928
963
929
964
for _ , tc := range testCases {
930
965
t .Run (tc .name , func (t * testing.T ) {
931
- compareEncoded (t , tc .u8l , tc .u8r , tc .expected )
932
- compareEncoded (t , tc .u16l , tc .u16r , tc .expected )
933
- compareEncoded (t , tc .u32l , tc .u32r , tc .expected )
934
- compareEncoded (t , tc .u64l , tc .u64r , tc .expected )
935
- compareEncoded (t , tc .ul , tc .ur , tc .expected )
966
+ compareEncoded (t , & UintFieldIndex { "Foo" }, tc .u8l , tc .u8r , tc .expected )
967
+ compareEncoded (t , & UintFieldIndex { "Foo" }, tc .u16l , tc .u16r , tc .expected )
968
+ compareEncoded (t , & UintFieldIndex { "Foo" }, tc .u32l , tc .u32r , tc .expected )
969
+ compareEncoded (t , & UintFieldIndex { "Foo" }, tc .u64l , tc .u64r , tc .expected )
970
+ compareEncoded (t , & UintFieldIndex { "Foo" }, tc .ul , tc .ur , tc .expected )
936
971
})
937
972
}
938
973
}
939
974
940
- func compareEncoded (t * testing.T , l interface {}, r interface {}, expected int ) {
941
- indexer := UintFieldIndex {"Foo" }
942
-
975
+ func compareEncoded (t * testing.T , indexer Indexer , l interface {}, r interface {}, expected int ) {
943
976
lBytes , err := indexer .FromArgs (l )
944
977
if err != nil {
945
- t .Fatalf ("unable to encode: %d" , l )
978
+ t .Fatalf ("unable to encode %d: %s " , l , err )
946
979
}
947
980
rBytes , err := indexer .FromArgs (r )
948
981
if err != nil {
949
- t .Fatalf ("unable to encode: %d" , r )
982
+ t .Fatalf ("unable to encode %d: %s " , r , err )
950
983
}
951
984
952
985
if bytes .Compare (lBytes , rBytes ) != expected {
@@ -1113,21 +1146,19 @@ func TestFieldSetIndex_FromArgs(t *testing.T) {
1113
1146
}
1114
1147
}
1115
1148
1116
- var (
1117
- // A conditional that checks if TestObject.Bar == 42
1118
- conditional = func (obj interface {}) (bool , error ) {
1119
- test , ok := obj .(* TestObject )
1120
- if ! ok {
1121
- return false , fmt .Errorf ("Expect only TestObj types" )
1122
- }
1123
-
1124
- if test .Bar != 42 {
1125
- return false , nil
1126
- }
1149
+ // A conditional that checks if TestObject.Bar == 42
1150
+ var conditional = func (obj interface {}) (bool , error ) {
1151
+ test , ok := obj .(* TestObject )
1152
+ if ! ok {
1153
+ return false , fmt .Errorf ("Expect only TestObj types" )
1154
+ }
1127
1155
1128
- return true , nil
1156
+ if test .Bar != 42 {
1157
+ return false , nil
1129
1158
}
1130
- )
1159
+
1160
+ return true , nil
1161
+ }
1131
1162
1132
1163
func TestConditionalIndex_FromObject (t * testing.T ) {
1133
1164
obj := testObj ()
0 commit comments