2022-09-08 02:54:37 +00:00
package helper
import (
"fmt"
"reflect"
"testing"
)
type x struct {
Id uint64
}
func c ( x [ ] * x ) ( r [ ] uint64 ) {
for i := 0 ; i < len ( x ) ; i ++ {
r = append ( r , x [ i ] . Id )
}
return
}
func getX ( ) ( r [ ] * x ) {
for i := 0 ; i < 10 ; i ++ {
r = append ( r , & x {
uint64 ( i ) ,
} )
}
return
}
func BenchmarkOr ( b * testing . B ) {
y := getX ( )
b . ResetTimer ( )
for i := 0 ; i < b . N ; i ++ {
c ( y )
}
}
func BenchmarkStructColumn ( b * testing . B ) {
y := getX ( )
fmt . Println ( y )
b . ResetTimer ( )
//b.N = 2
for i := 0 ; i < 1 ; i ++ {
StructColumn [ int , * x ] ( y , "Id" )
}
}
func TestStructColumn ( t * testing . T ) {
type args struct {
arr [ ] x
field string
}
tests := [ ] struct {
name string
args args
wantR [ ] uint64
} {
{ name : "test1" , args : args {
arr : [ ] x {
{ Id : 1 } ,
{ 2 } ,
{ 4 } ,
{ 6 } ,
} ,
field : "Id" ,
} , wantR : [ ] uint64 { 1 , 2 , 4 , 6 } } ,
}
for _ , tt := range tests {
t . Run ( tt . name , func ( t * testing . T ) {
if gotR := StructColumn [ uint64 , x ] ( tt . args . arr , tt . args . field ) ; ! reflect . DeepEqual ( gotR , tt . wantR ) {
t . Errorf ( "StructColumn() = %v, want %v" , gotR , tt . wantR )
}
} )
}
}
2022-09-15 14:35:32 +00:00
func TestRangeSlice ( t * testing . T ) {
type args struct {
start int
end int
step int
}
tests := [ ] struct {
name string
args args
want [ ] int
} {
{
name : "t1" ,
args : args {
start : 1 ,
end : 5 ,
step : 1 ,
} ,
want : [ ] int { 1 , 2 , 3 , 4 , 5 } ,
} ,
{
name : "t2" ,
args : args {
start : 0 ,
end : 5 ,
step : 2 ,
} ,
want : [ ] int { 0 , 2 , 4 } ,
} ,
2022-09-20 13:16:51 +00:00
{
name : "t3" ,
args : args {
start : 1 ,
end : 11 ,
step : 3 ,
} ,
want : [ ] int { 1 , 4 , 7 , 10 } ,
} ,
2022-09-24 14:55:39 +00:00
{
name : "t4" ,
args : args {
start : 0 ,
end : - 5 ,
step : - 1 ,
} ,
want : [ ] int { 0 , - 1 , - 2 , - 3 , - 4 , - 5 } ,
} ,
2022-09-15 14:35:32 +00:00
}
for _ , tt := range tests {
t . Run ( tt . name , func ( t * testing . T ) {
if got := RangeSlice ( tt . args . start , tt . args . end , tt . args . step ) ; ! reflect . DeepEqual ( got , tt . want ) {
t . Errorf ( "RangeSlice() = %v, want %v" , got , tt . want )
}
} )
}
}
2022-09-16 10:21:54 +00:00
func TestStrJoin ( t * testing . T ) {
type args struct {
s [ ] string
}
tests := [ ] struct {
name string
args args
wantStr string
} {
{ name : "t1" , args : args { s : [ ] string { "a" , "b" , "c" } } , wantStr : "abc" } ,
}
for _ , tt := range tests {
t . Run ( tt . name , func ( t * testing . T ) {
if gotStr := StrJoin ( tt . args . s ... ) ; gotStr != tt . wantStr {
t . Errorf ( "StrJoin() = %v, want %v" , gotStr , tt . wantStr )
}
} )
}
}
2022-09-20 13:16:51 +00:00
func TestSlicePagination ( t * testing . T ) {
arr := RangeSlice [ int ] ( 1 , 10 , 1 )
type args struct {
arr [ ] int
page int
pageSize int
}
tests := [ ] struct {
name string
args args
want [ ] int
} {
{
name : "t1" ,
args : args {
arr : arr ,
page : 1 ,
pageSize : 2 ,
} ,
want : RangeSlice [ int ] ( 1 , 2 , 1 ) ,
} , {
name : "t2" ,
args : args {
arr : arr ,
page : 2 ,
pageSize : 2 ,
} ,
want : RangeSlice [ int ] ( 3 , 4 , 1 ) ,
} , {
name : "t3" ,
args : args {
arr : arr ,
page : 4 ,
pageSize : 3 ,
} ,
want : [ ] int { 10 } ,
} , {
name : "t4" ,
args : args {
arr : arr ,
page : 5 ,
pageSize : 3 ,
} ,
want : [ ] int { } ,
} ,
}
for _ , tt := range tests {
t . Run ( tt . name , func ( t * testing . T ) {
if got := SlicePagination ( tt . args . arr , tt . args . page , tt . args . pageSize ) ; ! reflect . DeepEqual ( got , tt . want ) {
t . Errorf ( "SlicePagination() = %v, want %v" , got , tt . want )
}
} )
}
}
2022-09-23 13:46:34 +00:00
func TestStripTags ( t * testing . T ) {
type args struct {
str string
allowable string
}
tests := [ ] struct {
name string
args args
want string
} {
{
name : "t1" ,
args : args {
str : "<p>ppppp<span>ffff</span></p><img />" ,
allowable : "<p><img>" ,
} ,
want : "<p>pppppffff</p><img />" ,
} ,
}
for _ , tt := range tests {
t . Run ( tt . name , func ( t * testing . T ) {
if got := StripTags ( tt . args . str , tt . args . allowable ) ; got != tt . want {
t . Errorf ( "StripTags() = %v, want %v" , got , tt . want )
}
} )
}
}
2022-09-24 09:52:06 +00:00
func TestStripTagsX ( t * testing . T ) {
type args struct {
str string
allowable string
}
tests := [ ] struct {
name string
args args
want string
} {
{
name : "t1" ,
args : args {
str : "<p>ppppp<span>ffff</span></p><img />" ,
allowable : "<p><img>" ,
} ,
want : "<p>pppppffff</p><img />" ,
} ,
}
for _ , tt := range tests {
t . Run ( tt . name , func ( t * testing . T ) {
if got := StripTagsX ( tt . args . str , tt . args . allowable ) ; got != tt . want {
t . Errorf ( "StripTagsX() = %v, want %v" , got , tt . want )
}
} )
}
}
func BenchmarkStripTags ( b * testing . B ) {
for i := 0 ; i < b . N ; i ++ {
StripTags ( ` <p>ppppp<span>ffff</span></p><img /> ` , "<p><img>" )
}
}
func BenchmarkStripTagsX ( b * testing . B ) {
for i := 0 ; i < b . N ; i ++ {
StripTagsX ( ` <p>ppppp<span>ffff</span></p><img /> ` , "<p><img>" )
}
}
2022-09-24 13:42:55 +00:00
func TestCloseHtmlTag ( t * testing . T ) {
type args struct {
str string
}
tests := [ ] struct {
name string
args args
want string
} {
{
name : "t1" ,
args : args { str : ` <pre class="wp-block-preformatted">GRANT privileges ON databasename.tablename TO 'username'@'h...<p class="read-more"><a href="/p/305">继续阅读</a></p> ` } ,
want : "</pre>" ,
} ,
2022-09-24 17:44:45 +00:00
{
name : "t2" ,
args : args { str : ` <pre><div> ` } ,
want : "</div></pre>" ,
} ,
2022-09-24 13:42:55 +00:00
}
for _ , tt := range tests {
t . Run ( tt . name , func ( t * testing . T ) {
if got := CloseHtmlTag ( tt . args . str ) ; ! reflect . DeepEqual ( got , tt . want ) {
t . Errorf ( "CloseHtmlTag() = %v, want %v" , got , tt . want )
}
} )
}
}
func Test_clearTag ( t * testing . T ) {
type args struct {
s [ ] string
}
tests := [ ] struct {
name string
args args
want [ ] string
} {
{
name : "t1" ,
args : args { s : [ ] string { "<pre>" , "<p>" , "<span>" , "</span>" } } ,
want : [ ] string { "<pre>" , "<p>" } ,
} ,
{
name : "t2" ,
args : args { s : [ ] string { "<pre>" , "</pre>" , "<div>" , "<span>" , "</span>" } } ,
want : [ ] string { "<div>" } ,
} ,
{
name : "t3" ,
args : args { s : [ ] string { "<pre>" , "</pre>" } } ,
want : [ ] string { } ,
} ,
{
name : "t4" ,
args : args { s : [ ] string { "<pre>" , "<p>" } } ,
want : [ ] string { "<pre>" , "<p>" } ,
} ,
}
for _ , tt := range tests {
t . Run ( tt . name , func ( t * testing . T ) {
if got := ClearClosedTag ( tt . args . s ) ; ! reflect . DeepEqual ( got , tt . want ) {
t . Errorf ( "ClearClosedTag() = %v, want %v" , got , tt . want )
}
} )
}
}
2022-09-24 14:14:47 +00:00
func TestSliceReduce ( t * testing . T ) {
type args struct {
arr [ ] int
fn func ( int , int ) int
r int
}
tests := [ ] struct {
name string
args args
want int
} {
{
name : "t1" ,
args : args { arr : RangeSlice ( 1 , 10 , 1 ) , fn : func ( i int , i2 int ) int {
return i + i2
} } ,
want : 55 ,
} ,
}
for _ , tt := range tests {
t . Run ( tt . name , func ( t * testing . T ) {
if got := SliceReduce ( tt . args . arr , tt . args . fn , tt . args . r ) ; ! reflect . DeepEqual ( got , tt . want ) {
t . Errorf ( "SliceReduce() = %v, want %v" , got , tt . want )
}
} )
}
}
func TestSliceFilter ( t * testing . T ) {
type args struct {
arr [ ] int
fn func ( int ) bool
}
tests := [ ] struct {
name string
args args
want [ ] int
} {
{
name : "t1" ,
args : args { arr : RangeSlice ( 1 , 10 , 1 ) , fn : func ( i int ) bool {
return i > 4
} } ,
want : RangeSlice ( 5 , 10 , 1 ) ,
} ,
}
for _ , tt := range tests {
t . Run ( tt . name , func ( t * testing . T ) {
if got := SliceFilter ( tt . args . arr , tt . args . fn ) ; ! reflect . DeepEqual ( got , tt . want ) {
t . Errorf ( "SliceFilter() = %v, want %v" , got , tt . want )
}
} )
}
}
2022-09-24 14:31:03 +00:00
func TestSliceMap ( t * testing . T ) {
type args struct {
arr [ ] int8
fn func ( int8 ) int
}
tests := [ ] struct {
name string
args args
want [ ] int
} {
{
name : "t1" ,
args : args {
arr : RangeSlice [ int8 ] ( 1 , 10 , 1 ) ,
fn : func ( i int8 ) int {
return int ( i )
} ,
} ,
want : RangeSlice ( 1 , 10 , 1 ) ,
} ,
}
for _ , tt := range tests {
t . Run ( tt . name , func ( t * testing . T ) {
if got := SliceMap ( tt . args . arr , tt . args . fn ) ; ! reflect . DeepEqual ( got , tt . want ) {
t . Errorf ( "SliceMap() = %v, want %v" , got , tt . want )
}
} )
}
}
2022-09-24 14:55:39 +00:00
func TestSliceReverse ( t * testing . T ) {
type args struct {
arr [ ] int
}
tests := [ ] struct {
name string
args args
want [ ] int
} {
{
name : "t1" ,
args : args { arr : RangeSlice ( 1 , 10 , 1 ) } ,
want : RangeSlice ( 10 , 1 , - 1 ) ,
} ,
}
for _ , tt := range tests {
t . Run ( tt . name , func ( t * testing . T ) {
if got := SliceReverse ( tt . args . arr ) ; ! reflect . DeepEqual ( got , tt . want ) {
t . Errorf ( "SliceReverse() = %v, want %v" , got , tt . want )
}
} )
}
}
2022-09-26 13:25:41 +00:00
func TestToInterface ( t * testing . T ) {
type args struct {
v int
}
tests := [ ] struct {
name string
args args
want any
} {
{
name : "t1" ,
args : args { v : 1 } ,
want : any ( 1 ) ,
} ,
}
for _ , tt := range tests {
t . Run ( tt . name , func ( t * testing . T ) {
if got := ToAny ( tt . args . v ) ; ! reflect . DeepEqual ( got , tt . want ) {
t . Errorf ( "ToAny() = %v, want %v" , got , tt . want )
}
} )
}
}
2022-09-27 13:52:15 +00:00
func TestSliceSelfReverse ( t * testing . T ) {
type args struct {
arr [ ] int
}
tests := [ ] struct {
name string
args args
want [ ] int
} {
{
name : "t1" ,
args : args {
arr : RangeSlice ( 1 , 10 , 1 ) ,
} ,
want : RangeSlice ( 10 , 1 , - 1 ) ,
} , {
name : "t2" ,
args : args {
arr : RangeSlice ( 1 , 9 , 1 ) ,
} ,
want : RangeSlice ( 9 , 1 , - 1 ) ,
} ,
}
for _ , tt := range tests {
t . Run ( tt . name , func ( t * testing . T ) {
if got := SliceSelfReverse ( tt . args . arr ) ; ! reflect . DeepEqual ( got , tt . want ) {
t . Errorf ( "SliceSelfReverse() = %v, want %v" , got , tt . want )
}
} )
}
}
2022-10-08 06:01:05 +00:00
func TestSliceToMap ( t * testing . T ) {
type ss struct {
id int
v string
}
type args struct {
2022-10-10 13:07:39 +00:00
arr [ ] ss
2022-10-10 13:33:41 +00:00
fn func ( ss ) ( int , ss )
2022-10-10 13:07:39 +00:00
isCoverPrev bool
2022-10-08 06:01:05 +00:00
}
tests := [ ] struct {
name string
args args
want map [ int ] ss
} {
{
name : "t1" ,
args : args {
2022-10-10 13:07:39 +00:00
arr : [ ] ss { { 1 , "k1" } , { 2 , "v2" } , { 2 , "v3" } } ,
2022-10-10 13:33:41 +00:00
fn : func ( s ss ) ( int , ss ) {
return s . id , s
2022-10-08 06:01:05 +00:00
} ,
2022-10-10 13:07:39 +00:00
isCoverPrev : true ,
} ,
want : map [ int ] ss { 1 : { 1 , "k1" } , 2 : { 2 , "v3" } } ,
} , {
name : "t2" ,
args : args {
arr : [ ] ss { { 1 , "k1" } , { 2 , "v2" } , { 2 , "v3" } } ,
2022-10-10 13:33:41 +00:00
fn : func ( s ss ) ( int , ss ) {
return s . id , s
2022-10-10 13:07:39 +00:00
} ,
isCoverPrev : false ,
2022-10-08 06:01:05 +00:00
} ,
want : map [ int ] ss { 1 : { 1 , "k1" } , 2 : { 2 , "v2" } } ,
} ,
}
for _ , tt := range tests {
t . Run ( tt . name , func ( t * testing . T ) {
2022-10-10 13:07:39 +00:00
if got := SliceToMap ( tt . args . arr , tt . args . fn , tt . args . isCoverPrev ) ; ! reflect . DeepEqual ( got , tt . want ) {
2022-10-08 06:01:05 +00:00
t . Errorf ( "SliceToMap() = %v, want %v" , got , tt . want )
}
} )
}
}
2022-10-10 13:33:41 +00:00
func TestSimpleSliceToMap ( t * testing . T ) {
type args struct {
arr [ ] int
fn func ( int ) int
}
tests := [ ] struct {
name string
args args
want map [ int ] int
} {
{
name : "t1" ,
args : args { arr : [ ] int { 1 , 2 , 3 } , fn : func ( i int ) int {
return i
} } ,
want : map [ int ] int { 1 : 1 , 2 : 2 , 3 : 3 } ,
} ,
}
for _ , tt := range tests {
t . Run ( tt . name , func ( t * testing . T ) {
if got := SimpleSliceToMap ( tt . args . arr , tt . args . fn ) ; ! reflect . DeepEqual ( got , tt . want ) {
t . Errorf ( "SimpleSliceToMap() = %v, want %v" , got , tt . want )
}
} )
}
}
2022-10-13 03:09:52 +00:00
func TestRandNum ( t * testing . T ) {
type args struct {
start int
end int
}
tests := [ ] struct {
name string
args args
} {
{
name : "t1" ,
args : args {
start : 1 ,
end : 2 ,
} ,
} ,
}
for _ , tt := range tests {
t . Run ( tt . name , func ( t * testing . T ) {
for i := 0 ; i < 100 ; i ++ {
got := RandNum ( tt . args . start , tt . args . end )
if got > tt . args . end || got < tt . args . start {
t . Errorf ( "RandNum() = %v, range error" , got )
}
}
} )
}
}
2022-10-25 12:05:44 +00:00
2022-11-04 02:38:59 +00:00
func TestSimpleSort ( t * testing . T ) {
2022-10-27 02:02:01 +00:00
type xy struct {
x int
y int
}
2022-10-25 12:05:44 +00:00
type args struct {
2022-10-27 02:02:01 +00:00
arr [ ] xy
fn func ( i , j xy ) bool
2022-10-25 12:05:44 +00:00
}
tests := [ ] struct {
name string
args args
2022-10-27 02:02:01 +00:00
wantR [ ] xy
2022-10-25 12:05:44 +00:00
} {
{
name : "t1" ,
args : args {
2022-10-27 02:02:01 +00:00
arr : [ ] xy {
{ 1 , 2 } ,
{ 3 , 4 } ,
{ 1 , 3 } ,
{ 2 , 1 } ,
{ 1 , 6 } ,
2022-10-25 12:05:44 +00:00
} ,
2022-10-27 02:02:01 +00:00
fn : func ( i , j xy ) bool {
if i . x < j . x {
return true
}
if i . x == j . x && i . y > i . y {
return true
}
return false
} ,
} ,
wantR : [ ] xy {
{ 1 , 2 } ,
{ 1 , 3 } ,
{ 1 , 6 } ,
{ 2 , 1 } ,
{ 3 , 4 } ,
2022-10-25 12:05:44 +00:00
} ,
} ,
}
for _ , tt := range tests {
t . Run ( tt . name , func ( t * testing . T ) {
2022-11-04 02:38:59 +00:00
if SimpleSort ( tt . args . arr , tt . args . fn ) ; ! reflect . DeepEqual ( tt . args . arr , tt . wantR ) {
t . Errorf ( "SimpleSort() = %v, want %v" , tt . args . arr , tt . wantR )
2022-10-25 12:05:44 +00:00
}
} )
}
}
2022-11-05 12:59:49 +00:00
func TestMin ( t * testing . T ) {
type args struct {
a [ ] int
}
tests := [ ] struct {
name string
args args
want int
} {
{
name : "t1" ,
args : args { a : [ ] int { 1 , 2 , 3 } } ,
want : 1 ,
} ,
}
for _ , tt := range tests {
t . Run ( tt . name , func ( t * testing . T ) {
if got := Min ( tt . args . a ... ) ; got != tt . want {
t . Errorf ( "Min() = %v, want %v" , got , tt . want )
}
} )
}
}
func TestMax ( t * testing . T ) {
type args struct {
a [ ] int
}
tests := [ ] struct {
name string
args args
want int
} {
{
name : "t1" ,
args : args { a : [ ] int { 1 , 2 , 3 } } ,
want : 3 ,
} ,
}
for _ , tt := range tests {
t . Run ( tt . name , func ( t * testing . T ) {
if got := Max ( tt . args . a ... ) ; got != tt . want {
t . Errorf ( "Max() = %v, want %v" , got , tt . want )
}
} )
}
}
2022-11-06 04:04:41 +00:00
func TestSliceChunk ( t * testing . T ) {
type args struct {
arr [ ] int
size int
}
tests := [ ] struct {
name string
args args
want [ ] [ ] int
} {
{
name : "t1" ,
args : args {
arr : RangeSlice ( 1 , 7 , 1 ) ,
size : 2 ,
} ,
want : [ ] [ ] int { { 1 , 2 } , { 3 , 4 } , { 5 , 6 } , { 7 } } ,
} ,
{
name : "t2" ,
args : args {
arr : RangeSlice ( 1 , 8 , 1 ) ,
size : 2 ,
} ,
want : [ ] [ ] int { { 1 , 2 } , { 3 , 4 } , { 5 , 6 } , { 7 , 8 } } ,
} ,
}
for _ , tt := range tests {
t . Run ( tt . name , func ( t * testing . T ) {
if got := SliceChunk ( tt . args . arr , tt . args . size ) ; ! reflect . DeepEqual ( got , tt . want ) {
t . Errorf ( "SliceChunk() = %v, want %v" , got , tt . want )
}
} )
}
}
2022-11-06 04:12:57 +00:00
func TestSum ( t * testing . T ) {
type args struct {
a [ ] int
}
tests := [ ] struct {
name string
args args
want int
} {
{
name : "t1" ,
args : args { a : RangeSlice ( 1 , 10 , 1 ) } ,
want : 55 ,
} ,
}
for _ , tt := range tests {
t . Run ( tt . name , func ( t * testing . T ) {
if got := Sum ( tt . args . a ... ) ; got != tt . want {
t . Errorf ( "Sum() = %v, want %v" , got , tt . want )
}
} )
}
}