Initial commit
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
package arr
|
||||
|
||||
import "sync"
|
||||
|
||||
type ArrayType interface {
|
||||
Add(value interface{})
|
||||
Check(value interface{}) bool
|
||||
Len() int
|
||||
Values() map[interface{}]bool
|
||||
}
|
||||
|
||||
// CheckType type for check array data
|
||||
type CheckArrayType struct {
|
||||
values map[interface{}]bool
|
||||
sm sync.Mutex
|
||||
len int
|
||||
}
|
||||
|
||||
// NewCheckArrayType
|
||||
func NewCheckArrayType(len int) *CheckArrayType {
|
||||
return &CheckArrayType{values: make(map[interface{}]bool, len)}
|
||||
}
|
||||
|
||||
// Add
|
||||
func (ct *CheckArrayType) Add(value interface{}) {
|
||||
defer ct.sm.Unlock()
|
||||
ct.sm.Lock()
|
||||
ct.values[value] = true
|
||||
ct.len++
|
||||
}
|
||||
|
||||
// AddMutil
|
||||
func (ct *CheckArrayType) AddMutil(values ...interface{}) {
|
||||
for _, v := range values {
|
||||
v := v
|
||||
ct.Add(v)
|
||||
}
|
||||
}
|
||||
|
||||
// Check
|
||||
func (ct *CheckArrayType) Check(value interface{}) bool {
|
||||
defer ct.sm.Unlock()
|
||||
ct.sm.Lock()
|
||||
if b, ok := ct.values[value]; ok && b {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Len
|
||||
func (ct *CheckArrayType) Len() int {
|
||||
defer ct.sm.Unlock()
|
||||
ct.sm.Lock()
|
||||
return ct.len
|
||||
}
|
||||
|
||||
// Values
|
||||
func (ct *CheckArrayType) Values() map[interface{}]bool {
|
||||
defer ct.sm.Unlock()
|
||||
ct.sm.Lock()
|
||||
return ct.values
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
package arr
|
||||
|
||||
import (
|
||||
"log"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCheckArrayType(t *testing.T) {
|
||||
t.Run("check array type test with string", func(t *testing.T) {
|
||||
arrayType := NewCheckArrayType(10)
|
||||
arrayType.Add("1")
|
||||
if !arrayType.Check("1") {
|
||||
t.Errorf("1 should be in array type,but it is not in.")
|
||||
}
|
||||
if arrayType.Check("2") {
|
||||
t.Errorf("2 should not be in array type,but it is in.")
|
||||
}
|
||||
if arrayType.Check(1) {
|
||||
t.Errorf("int 1 should not be in array type,but it is in.")
|
||||
}
|
||||
if arrayType.Len() != 1 {
|
||||
t.Errorf("array type len should be 1 ,but get array type len is %d.", arrayType.Len())
|
||||
}
|
||||
arrayType.AddMutil("2", "3", "4")
|
||||
if arrayType.Len() != 4 {
|
||||
t.Errorf("array type len should be 4 ,but get array type len is %d.", arrayType.Len())
|
||||
}
|
||||
for i, v := range arrayType.Values() {
|
||||
log.Println(v)
|
||||
if !arrayType.Check(i) {
|
||||
t.Errorf("%v should be in array type,but it is not in", i)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("check array type test with uint", func(t *testing.T) {
|
||||
arrayType := NewCheckArrayType(10)
|
||||
var one uint = 1
|
||||
var two uint = 2
|
||||
var three uint = 3
|
||||
var four uint = 4
|
||||
arrayType.Add(one)
|
||||
if !arrayType.Check(one) {
|
||||
t.Errorf("1 should be in array type,but it is not in.")
|
||||
}
|
||||
if arrayType.Check(two) {
|
||||
t.Errorf("2 should not be in array type,but it is in.")
|
||||
}
|
||||
if arrayType.Len() != 1 {
|
||||
t.Errorf("array type len should be 1 ,but get array type len is %d.", arrayType.Len())
|
||||
}
|
||||
arrayType.AddMutil(two, three, four)
|
||||
if arrayType.Len() != 4 {
|
||||
t.Errorf("array type len should be 4 ,but get array type len is %d.", arrayType.Len())
|
||||
}
|
||||
for i, v := range arrayType.Values() {
|
||||
log.Println(v)
|
||||
if !arrayType.Check(i) {
|
||||
t.Errorf("%v should be in array type,but it is not in", i)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("check array type test with int", func(t *testing.T) {
|
||||
arrayType := NewCheckArrayType(10)
|
||||
arrayType.Add(1)
|
||||
if !arrayType.Check(1) {
|
||||
t.Errorf("1 should be in array type,but it is not in.")
|
||||
}
|
||||
if arrayType.Check(2) {
|
||||
t.Errorf("2 should not be in array type,but it is in.")
|
||||
}
|
||||
if arrayType.Check("1") {
|
||||
t.Errorf("string 1 should not be in array type,but it is in.")
|
||||
}
|
||||
if arrayType.Len() != 1 {
|
||||
t.Errorf("array type len should be 1 ,but get array type len is %d.", arrayType.Len())
|
||||
}
|
||||
arrayType.AddMutil(2, 3, 4)
|
||||
if arrayType.Len() != 4 {
|
||||
t.Errorf("array type len should be 4 ,but get array type len is %d.", arrayType.Len())
|
||||
}
|
||||
for i, v := range arrayType.Values() {
|
||||
log.Println(v)
|
||||
if !arrayType.Check(i) {
|
||||
t.Errorf("%v should be in array type,but it is not in", i)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package arr
|
||||
|
||||
import "strconv"
|
||||
|
||||
// 连接 unit slice 为字符串
|
||||
func UnitJoin(ss []uint, sep string) string {
|
||||
var rs string
|
||||
for index, item := range ss {
|
||||
itemS := strconv.FormatUint(uint64(item), 10)
|
||||
if index < len(ss)-1 {
|
||||
rs += itemS + sep
|
||||
} else {
|
||||
rs += itemS
|
||||
}
|
||||
}
|
||||
return rs
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package arr
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestUnitJoin(t *testing.T) {
|
||||
type args struct {
|
||||
ss []uint
|
||||
sep string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "success",
|
||||
args: struct {
|
||||
ss []uint
|
||||
sep string
|
||||
}{ss: []uint{1, 2, 3, 4}, sep: ","},
|
||||
want: "1,2,3,4",
|
||||
},
|
||||
{
|
||||
name: "success",
|
||||
args: struct {
|
||||
ss []uint
|
||||
sep string
|
||||
}{ss: []uint{1, 2, 3, 4}, sep: "||"},
|
||||
want: "1||2||3||4",
|
||||
},
|
||||
{
|
||||
name: "success",
|
||||
args: struct {
|
||||
ss []uint
|
||||
sep string
|
||||
}{ss: []uint{1, 2, 3, 4}, sep: "-"},
|
||||
want: "1-2-3-4",
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := UnitJoin(tt.args.ss, tt.args.sep); got != tt.want {
|
||||
t.Errorf("UnitJoin() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user