mirror of
https://gitee.com/johng/gf
synced 2026-06-11 20:01:45 +08:00
Compare commits
73 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 4844eea0ab | |||
| 8ecd62d3de | |||
| 4c610b4f58 | |||
| 1932c4ec44 | |||
| bd2e51ddca | |||
| ddcb7121c1 | |||
| f1f575fd5c | |||
| 99adb7cdc4 | |||
| 6b7ea97777 | |||
| 495b5758ec | |||
| ba56eb87b1 | |||
| 4258a3bbc9 | |||
| 4912331ddc | |||
| e87b7ecf5d | |||
| 23bce7bde6 | |||
| 926b664615 | |||
| fa8257c85b | |||
| abb9c88c23 | |||
| f89976cad5 | |||
| 0389778725 | |||
| 3c36285126 | |||
| 75054ee109 | |||
| 8447b1a42b | |||
| e5265a1c46 | |||
| 6e8ef8d0b0 | |||
| 75c081afc9 | |||
| 060fd9eaba | |||
| 63e5a60344 | |||
| 75dc1d82c1 | |||
| a5e048eb5f | |||
| 65bc1d5eb8 | |||
| bfa64705b5 | |||
| 17aea8d7d4 | |||
| a6a01fd7f2 | |||
| 41a9e91b4c | |||
| e2c1e11f95 | |||
| c2966817ce | |||
| 16958413bb | |||
| c0a0913d4b | |||
| 6d47810782 | |||
| 7a9ea2e546 | |||
| 7881b2dee4 | |||
| 5f223ef049 | |||
| e57942b374 | |||
| f18e6f078c | |||
| f667cbc2a2 | |||
| 07e65c14a9 | |||
| 0b6d04485e | |||
| 36401a063d | |||
| 849e7370d1 | |||
| f01dca0895 | |||
| 55137b2aa3 | |||
| 22540921b3 | |||
| 6174097a07 | |||
| 52de11b1fe | |||
| 5a646179ad | |||
| 33ae93e050 | |||
| f3d859159d | |||
| 4fb01e68f7 | |||
| 2812a247aa | |||
| 2b46e765c4 | |||
| c7f911cae2 | |||
| 9f0548c03d | |||
| c578df06a3 | |||
| 8ed3cf9c97 | |||
| 2438f565e9 | |||
| 8230c72ec6 | |||
| d716037caa | |||
| 855a4ddb2c | |||
| 74be9fac18 | |||
| e9fba5a166 | |||
| 0a92df691b | |||
| e513cd10ed |
@ -9,7 +9,7 @@ import (
|
||||
|
||||
func main() {
|
||||
// 创建一个对象池,过期时间为1000毫秒
|
||||
p := gpool.New(1000, nil)
|
||||
p := gpool.New(1000*time.Millisecond, nil)
|
||||
|
||||
// 从池中取一个对象,返回nil及错误信息
|
||||
fmt.Println(p.Get())
|
||||
|
||||
@ -11,7 +11,7 @@ import (
|
||||
|
||||
func main() {
|
||||
// 创建对象复用池,对象过期时间为3000毫秒,并给定创建及销毁方法
|
||||
p := gpool.New(3000, func() (interface{}, error) {
|
||||
p := gpool.New(3000*time.Millisecond, func() (interface{}, error) {
|
||||
return gtcp.NewConn("www.baidu.com:80")
|
||||
}, func(i interface{}) {
|
||||
glog.Println("expired")
|
||||
|
||||
@ -8,112 +8,72 @@ package driver
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/database/gdb"
|
||||
"github.com/gogf/gf/internal/intlog"
|
||||
"github.com/gogf/gf/text/gstr"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/os/gtime"
|
||||
)
|
||||
|
||||
// MyDriver is a custom database driver, which is used for testing only.
|
||||
// For simplifying the unit testing case purpose, MyDriver struct inherits the mysql driver
|
||||
// gdb.DriverMysql and overwrites its functions DoQuery and DoExec.
|
||||
// So if there's any sql execution, it goes through MyDriver.DoQuery/MyDriver.DoExec firstly
|
||||
// and then gdb.DriverMysql.DoQuery/gdb.DriverMysql.DoExec.
|
||||
// You can call it sql "HOOK" or "HiJack" as your will.
|
||||
type MyDriver struct {
|
||||
*gdb.Core
|
||||
*gdb.DriverMysql
|
||||
}
|
||||
|
||||
// Open creates and returns a underlying sql.DB object for mysql.
|
||||
func (d *MyDriver) Open(config *gdb.ConfigNode) (*sql.DB, error) {
|
||||
var source string
|
||||
if config.LinkInfo != "" {
|
||||
source = config.LinkInfo
|
||||
} else {
|
||||
source = fmt.Sprintf(
|
||||
"%s:%s@tcp(%s:%s)/%s?charset=%s&multiStatements=true&parseTime=true&loc=Local",
|
||||
config.User, config.Pass, config.Host, config.Port, config.Name, config.Charset,
|
||||
)
|
||||
}
|
||||
intlog.Printf("Open: %s", source)
|
||||
if db, err := sql.Open("mysql", source); err == nil {
|
||||
return db, nil
|
||||
} else {
|
||||
return nil, err
|
||||
var (
|
||||
// customDriverName is my driver name, which is used for registering.
|
||||
customDriverName = "MyDriver"
|
||||
)
|
||||
|
||||
func init() {
|
||||
// It here registers my custom driver in package initialization function "init".
|
||||
// You can later use this type in the database configuration.
|
||||
if err := gdb.Register(customDriverName, &MyDriver{}); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
// getChars returns the security char for this type of database.
|
||||
func (d *MyDriver) GetChars() (charLeft string, charRight string) {
|
||||
return "`", "`"
|
||||
// New creates and returns a database object for mysql.
|
||||
// It implements the interface of gdb.Driver for extra database driver installation.
|
||||
func (d *MyDriver) New(core *gdb.Core, node *gdb.ConfigNode) (gdb.DB, error) {
|
||||
return &MyDriver{
|
||||
&gdb.DriverMysql{
|
||||
Core: core,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
// handleSqlBeforeExec handles the sql before posts it to database.
|
||||
func (d *MyDriver) HandleSqlBeforeExec(sql string) string {
|
||||
return sql
|
||||
}
|
||||
|
||||
// Tables retrieves and returns the tables of current schema.
|
||||
func (d *MyDriver) Tables(schema ...string) (tables []string, err error) {
|
||||
var result gdb.Result
|
||||
link, err := d.DB.GetSlave(schema...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result, err = d.DB.DoGetAll(link, `SHOW TABLES`)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
for _, m := range result {
|
||||
for _, v := range m {
|
||||
tables = append(tables, v.String())
|
||||
}
|
||||
// DoQuery commits the sql string and its arguments to underlying driver
|
||||
// through given link object and returns the execution result.
|
||||
func (d *MyDriver) DoQuery(link gdb.Link, sql string, args ...interface{}) (rows *sql.Rows, err error) {
|
||||
tsMilli := gtime.TimestampMilli()
|
||||
rows, err = d.DriverMysql.DoQuery(link, sql, args...)
|
||||
if _, err := d.DriverMysql.InsertIgnore("monitor", g.Map{
|
||||
"sql": gdb.FormatSqlWithArgs(sql, args),
|
||||
"cost": gtime.TimestampMilli() - tsMilli,
|
||||
"time": gtime.Now(),
|
||||
"error": err.Error(),
|
||||
}); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// gdb.TableFields retrieves and returns the fields information of specified table of current schema.
|
||||
//
|
||||
// Note that it returns a map containing the field name and its corresponding fields.
|
||||
// As a map is unsorted, the gdb.TableField struct has a "Index" field marks its sequence in the fields.
|
||||
//
|
||||
// It's using cache feature to enhance the performance, which is never expired util the process restarts.
|
||||
func (d *MyDriver) TableFields(table string, schema ...string) (fields map[string]*gdb.TableField, err error) {
|
||||
table = gstr.Trim(table)
|
||||
if gstr.Contains(table, " ") {
|
||||
panic("function gdb.TableFields supports only single table operations")
|
||||
}
|
||||
checkSchema := d.DB.GetSchema()
|
||||
if len(schema) > 0 && schema[0] != "" {
|
||||
checkSchema = schema[0]
|
||||
}
|
||||
v := d.DB.GetCache().GetOrSetFunc(
|
||||
fmt.Sprintf(`mysql_table_fields_%s_%s`, table, checkSchema),
|
||||
func() interface{} {
|
||||
var result gdb.Result
|
||||
var link *sql.DB
|
||||
link, err = d.DB.GetSlave(checkSchema)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
result, err = d.DB.DoGetAll(
|
||||
link,
|
||||
fmt.Sprintf(`SHOW FULL COLUMNS FROM %s`, d.DB.QuoteWord(table)),
|
||||
)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
fields = make(map[string]*gdb.TableField)
|
||||
for i, m := range result {
|
||||
fields[m["Field"].String()] = &gdb.TableField{
|
||||
Index: i,
|
||||
Name: m["Field"].String(),
|
||||
Type: m["Type"].String(),
|
||||
Null: m["Null"].Bool(),
|
||||
Key: m["Key"].String(),
|
||||
Default: m["Default"].Val(),
|
||||
Extra: m["Extra"].String(),
|
||||
Comment: m["Comment"].String(),
|
||||
}
|
||||
}
|
||||
return fields
|
||||
}, 0)
|
||||
if err == nil {
|
||||
fields = v.(map[string]*gdb.TableField)
|
||||
// DoExec commits the query string and its arguments to underlying driver
|
||||
// through given link object and returns the execution result.
|
||||
func (d *MyDriver) DoExec(link gdb.Link, sql string, args ...interface{}) (result sql.Result, err error) {
|
||||
tsMilli := gtime.TimestampMilli()
|
||||
result, err = d.DriverMysql.DoExec(link, sql, args...)
|
||||
if _, err := d.DriverMysql.InsertIgnore("monitor", g.Map{
|
||||
"sql": gdb.FormatSqlWithArgs(sql, args),
|
||||
"cost": gtime.TimestampMilli() - tsMilli,
|
||||
"time": gtime.Now(),
|
||||
"error": err.Error(),
|
||||
}); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@ -8,7 +8,6 @@ import (
|
||||
func main() {
|
||||
s := ghttp.GetServer()
|
||||
s.BindHandler("/", func(r *ghttp.Request) {
|
||||
r.Response.Write("Hello World")
|
||||
r.Response.WriteTpl("index.tpl", g.Map{
|
||||
"title": "Test",
|
||||
"name": "John",
|
||||
|
||||
@ -11,7 +11,7 @@ import (
|
||||
func main() {
|
||||
for {
|
||||
time.Sleep(time.Second)
|
||||
if f, err := gfpool.Open("/home/john/temp/log.log", os.O_RDONLY, 0666, 60000000*1000); err == nil {
|
||||
if f, err := gfpool.Open("/home/john/temp/log.log", os.O_RDONLY, 0666, time.Hour); err == nil {
|
||||
fmt.Println(f.Name())
|
||||
f.Close()
|
||||
} else {
|
||||
|
||||
11
.example/os/glog/glog_level_prefix.go
Normal file
11
.example/os/glog/glog_level_prefix.go
Normal file
@ -0,0 +1,11 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/os/glog"
|
||||
)
|
||||
|
||||
func main() {
|
||||
l := glog.New()
|
||||
l.SetLevelPrefix(glog.LEVEL_DEBU, "debug")
|
||||
l.Debug("test")
|
||||
}
|
||||
@ -1,16 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/os/gres"
|
||||
_ "github.com/gogf/gf/os/gres/testdata"
|
||||
)
|
||||
|
||||
func main() {
|
||||
gres.Dump()
|
||||
//file := gres.Get("www")
|
||||
//fmt.Println(file.Open())
|
||||
//g.Dump(gres.ScanDir("/root/image", "*"))
|
||||
//g.Dump(gres.Scan("/root/image/", "*", true))
|
||||
//g.Dump(gres.Scan("/template", "*"))
|
||||
//g.Dump(gres.Scan("/template/layout2", "*.html", true))
|
||||
}
|
||||
20
.example/os/gres/gres_example.go
Normal file
20
.example/os/gres/gres_example.go
Normal file
@ -0,0 +1,20 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
_ "github.com/gogf/gf/os/gres/testdata/example/boot"
|
||||
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
)
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.Group("/", func(group *ghttp.RouterGroup) {
|
||||
group.GET("/template", func(r *ghttp.Request) {
|
||||
r.Response.WriteTplDefault(g.Map{
|
||||
"name": "GoFrame",
|
||||
})
|
||||
})
|
||||
})
|
||||
s.Run()
|
||||
}
|
||||
@ -7,6 +7,12 @@
|
||||
default = "127.0.0.1:6379,0"
|
||||
cache = "127.0.0.1:6379,1"
|
||||
|
||||
# Logger.
|
||||
[logger]
|
||||
Path = "/tmp/log/gf-app"
|
||||
Level = "all"
|
||||
Stdout = true
|
||||
|
||||
[viewer]
|
||||
delimiters = ["${", "}"]
|
||||
autoencode = true
|
||||
@ -1,35 +1,25 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/os/glog"
|
||||
"github.com/gogf/gf/os/gtime"
|
||||
"github.com/gogf/gf/os/gtimer"
|
||||
"time"
|
||||
"github.com/gogf/gf/crypto/gaes"
|
||||
"github.com/gogf/gf/os/gfile"
|
||||
"github.com/gogf/gf/os/gres"
|
||||
)
|
||||
|
||||
func GetList() {
|
||||
START:
|
||||
for {
|
||||
res, err := g.Redis().DoVar("RPOP", "mill")
|
||||
if err != nil {
|
||||
glog.Debug("Rpop:", err)
|
||||
break
|
||||
}
|
||||
glog.Debug(res)
|
||||
if res.IsEmpty() {
|
||||
glog.Debug("nil")
|
||||
continue START
|
||||
}
|
||||
interval := 50 * time.Second
|
||||
gtimer.AddOnce(interval, func() {
|
||||
glog.Debug("end------:", res, gtime.Now().Format("Y-m-d H:i:s"))
|
||||
})
|
||||
}
|
||||
}
|
||||
var (
|
||||
CryptoKey = []byte("x76cgqt36i9c863bzmotuf8626dxiwu0")
|
||||
)
|
||||
|
||||
func main() {
|
||||
g.Redis().SetMaxActive(2)
|
||||
//g.Redis().SetMaxIdle(100)
|
||||
GetList()
|
||||
binContent, err := gres.Pack("public,config")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
binContent, err = gaes.Encrypt(binContent, CryptoKey)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if err := gfile.PutBytes("data.bin", binContent); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
@ -7,7 +7,7 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
s := ghttp.GetServer()
|
||||
s := g.Server()
|
||||
s.BindHandler("/page/demo", func(r *ghttp.Request) {
|
||||
page := r.GetPage(100, 10)
|
||||
buffer, _ := gview.ParseContent(`
|
||||
|
||||
@ -7,7 +7,7 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
s := ghttp.GetServer()
|
||||
s := g.Server()
|
||||
s.BindHandler("/page/ajax", func(r *ghttp.Request) {
|
||||
page := r.GetPage(100, 10)
|
||||
page.AjaxActionName = "DoAjax"
|
||||
|
||||
@ -8,7 +8,7 @@ import (
|
||||
"github.com/gogf/gf/util/gpage"
|
||||
)
|
||||
|
||||
// 分页标签使用li标签包裹
|
||||
// wrapContent wraps each of the page tag with html li and ul.
|
||||
func wrapContent(page *gpage.Page) string {
|
||||
content := page.GetContent(4)
|
||||
content = gstr.ReplaceByMap(content, map[string]string{
|
||||
@ -21,7 +21,7 @@ func wrapContent(page *gpage.Page) string {
|
||||
}
|
||||
|
||||
func main() {
|
||||
s := ghttp.GetServer()
|
||||
s := g.Server()
|
||||
s.BindHandler("/page/custom1/*page", func(r *ghttp.Request) {
|
||||
page := r.GetPage(100, 10)
|
||||
content := wrapContent(page)
|
||||
|
||||
@ -7,7 +7,7 @@ import (
|
||||
"github.com/gogf/gf/util/gpage"
|
||||
)
|
||||
|
||||
// 自定义分页名称
|
||||
// pageContent customizes the page tag name.
|
||||
func pageContent(page *gpage.Page) string {
|
||||
page.NextPageTag = "NextPage"
|
||||
page.PrevPageTag = "PrevPage"
|
||||
@ -22,7 +22,7 @@ func pageContent(page *gpage.Page) string {
|
||||
}
|
||||
|
||||
func main() {
|
||||
s := ghttp.GetServer()
|
||||
s := g.Server()
|
||||
s.BindHandler("/page/custom2/*page", func(r *ghttp.Request) {
|
||||
page := r.GetPage(100, 10)
|
||||
buffer, _ := gview.ParseContent(`
|
||||
|
||||
@ -1,8 +1,6 @@
|
||||
language: go
|
||||
|
||||
go:
|
||||
- "1.11.x"
|
||||
- "1.12.x"
|
||||
- "1.13.x"
|
||||
- "1.14.x"
|
||||
|
||||
|
||||
20
DONATOR.MD
20
DONATOR.MD
@ -40,7 +40,7 @@ We currently accept donation by Alipay/WechatPay, please note your github/gitee
|
||||
|R*s|wechat|¥18.88| 谢谢GF!辛苦了!
|
||||
|粟*e|wechat|¥50.00|
|
||||
|[李超](https://github.com/effortlee)|wechat|¥124.00|
|
||||
|张炳贤|wechat+qq|¥600.00|
|
||||
|soidea666|wechat+qq|¥800.00|
|
||||
|[王哈哈](https://gitee.com/develop1024)|wechat|¥6.66| 希望gf越来越好
|
||||
|夕景|alipay+qq|¥9.96+3.57|
|
||||
|struggler|alipay|¥18.80|
|
||||
@ -51,6 +51,24 @@ We currently accept donation by Alipay/WechatPay, please note your github/gitee
|
||||
|[Zeroing-ZY](https://gitee.com/yunjieg)|gitee|¥20.00| 感谢您的开源项目!
|
||||
|[katydid酱](https://gitee.com/katydid2005)|gitee|¥50.00| 感谢您的开源项目!框架给予了很大的帮助!谢谢大佬!
|
||||
|[李海峰](https://gitee.com/dlhf)|gitee|¥10.00| 希望GF越来越好,框架很牛逼!
|
||||
|陆昱天|alipay|¥100.00|
|
||||
|[Dockercore](https://github.com/dockercore)|wechat|¥200.00| 非常喜欢!简洁好用!文档超级全!
|
||||
|🚶|wechat|¥6.88| 喝杯冰阔落
|
||||
|a*l|wechat|¥10.00| gf
|
||||
|[wxkj](https://gitee.com/wxkj)|wechat|¥10.00|
|
||||
|*包|wechat|¥9.99|
|
||||
|重庆宝尔威科技|wechat|¥6.66|
|
||||
|琦玉-QPT|wechat|¥6.66|
|
||||
|sailsea|wechat|¥11.00|
|
||||
|[seny0929](https://gitee.com/seny0929)|wechat|¥99.90|
|
||||
|*华|wechat|¥6.66| 感谢郭强的热心
|
||||
|[Playhi](https://github.com/Playhi)|alipay|¥10.00|
|
||||
|北京京纬互动科技|alipay|¥200.00|
|
||||
|米司特包|wechat|¥99.99|
|
||||
|金毛|alipay|¥100.00|
|
||||
|1*1x|wechat|¥100.00|
|
||||
|[ywanbing](https://github.com/ywanbing)|wechat|¥66.66|
|
||||
|[侯哥](http://www.macnie.com)|wechat|¥10.00|
|
||||
|
||||
|
||||
<img src="https://goframe.org/images/donate.png"/>
|
||||
|
||||
@ -28,7 +28,7 @@ require github.com/gogf/gf latest
|
||||
|
||||
# Limitation
|
||||
```
|
||||
golang version >= 1.10
|
||||
golang version >= 1.13
|
||||
```
|
||||
|
||||
# Documentation
|
||||
|
||||
13
README_ZH.MD
13
README_ZH.MD
@ -8,11 +8,12 @@
|
||||
|
||||
[English](README.MD) | 简体中文
|
||||
|
||||
`GF(Go Frame)`是一款模块化、高性能、生产级的Go基础开发框架。实现了比较完善的基础设施建设,包括常用的核心开发组件,
|
||||
如:缓存、日志、文件、时间、队列、数组、集合、字符串、定时器、命令行、文件锁、内存锁、对象池、连接池、资源管理、数据校验、数据编码、文件监控、
|
||||
定时任务、数据库ORM、TCP/UDP组件、进程管理/通信、并发安全容器等等。
|
||||
并提供了Web服务开发的系列核心组件,如:Router、Cookie、Session、Middleware、服务注册、配置管理、模板引擎等等,
|
||||
支持热重启、热更新、多域名、多端口、多服务、HTTPS、Rewrite等特性。
|
||||
`GF(Go Frame)`是一款模块化、高性能、生产级的Go基础开发框架。
|
||||
实现了比较完善的基础设施建设以及开发工具链,提供了常用的基础开发模块,
|
||||
如:缓存、日志、队列、数组、集合、容器、定时器、命令行、内存锁、对象池、
|
||||
配置管理、资源管理、数据校验、数据编码、定时任务、数据库ORM、TCP/UDP组件、进程管理/通信等等。
|
||||
并提供了Web服务开发的系列核心组件,如:Router、Cookie、Session、Middleware、服务注册、模板引擎等等,
|
||||
支持热重启、热更新、域名绑定、TLS/HTTPS、Rewrite等特性。
|
||||
|
||||
|
||||
# 特点
|
||||
@ -40,7 +41,7 @@ require github.com/gogf/gf latest
|
||||
|
||||
# 限制
|
||||
```shell
|
||||
golang版本 >= 1.11
|
||||
golang版本 >= 1.13
|
||||
```
|
||||
|
||||
# 架构
|
||||
|
||||
@ -10,7 +10,6 @@
|
||||
1. gjson对大json数据的解析效率问题;
|
||||
1. ghttp增加route name特性,并同时支持backend和template(提供内置函数)引用,可以通过RedirectRoute方法给定route name和路由参数跳转到指定的路由地址上;
|
||||
1. gvalid校验支持当第一个规则失败后便不再校验后续的规则,最好做成链式操作;
|
||||
1. gvalid增加支持对[]rune的长度校验(一个中文占3个字节);
|
||||
1. ghttp.Request增加对输入参数的自动HtmlEncode机制;
|
||||
1. 常量命名风格根据golint进行修改;
|
||||
1. 开放rwmutex包,并将gjson的互斥锁使用自定义的mutex替换;
|
||||
@ -21,8 +20,6 @@
|
||||
- ghttp Server&Client basic auth、
|
||||
- glog分类&日志等级&链式操作、gdb debug自动输出调试信息、gmlock内存锁、
|
||||
1. 服务注册域名增加对泛域名的支持;
|
||||
1. Cookie设置中文失效问题;
|
||||
1. 使用gconv将slice映射到struct属性上,例如redis hscan的结果集;
|
||||
1. 项目参考:
|
||||
- https://github.com/namreg/godown
|
||||
- https://github.com/Masterminds/sprig
|
||||
@ -31,32 +28,36 @@
|
||||
1. 路由增加不区分大小写得匹配方式;
|
||||
1. 改进WebServer获取POST参数处理逻辑,当提交非form数据时,例如json数据,针对某些方法可以直接解析;
|
||||
1. WebServer增加可选择的路由覆盖配置,默认情况下不覆盖;
|
||||
1. grpool性能压测结果变慢的问题;
|
||||
1. 增加jumplist的数据结构容器;
|
||||
1. DelayQueue/PriorityQueue;
|
||||
1. 权限管理模块;
|
||||
1. 从ghttp中剥离SESSION功能构成单独的模块gsession;
|
||||
1. 改进gproc进程间通信处理逻辑,提高稳定性,以应对进程间大批量的数据发送/接收;
|
||||
1. ghttp的热重启的本地进程端口监听,在不使用该特性时默认关闭掉;
|
||||
1. gtcp增加对TLS加密通信的支持;
|
||||
1. 添加Save/Replace/BatchSave/BatchReplace方法对sqlite数据库的支持;
|
||||
1. 添加sqlite数据库的单元测试用例;
|
||||
1. gredis增加cluster支持;
|
||||
1. gset.Add/Remove/Contains方法增加批量操作支持;
|
||||
1. gmlock增加手动清理机制:当内存锁不再使用时,由调用端决定是否清理内存锁;
|
||||
1. gtimer增加DelayAdd*方法返回Entry对象,以便DelayAdd*的定时任务也能进行状态控制;gcron同理需要改进;
|
||||
1. 改进gdb对pgsql/mssql/oracle的支持,使用方法覆盖的方式改进操作,而不是完全依靠正则替换的方式;
|
||||
1. gdb的Cache缓存功能增加可自定义缓存接口,以便支持外部缓存功能,缓存接口可以通过io.ReadWriter接口实现;
|
||||
1. grpool增加支持阻塞添加任务接口;
|
||||
1. gdb.Model在链式安全的对象创建中增加sync.Pool的使用;
|
||||
1. 增加g.Table快捷方法以方便操作数据表,但是得考虑后续模型操作设计,特别是脚手架的模型管理;
|
||||
1. 改进ghttp分组路由中对hook的支持方式,以便格式与BindHookHandler统一;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
# DONE
|
||||
1. Cookie设置中文失效问题;
|
||||
1. gvalid增加支持对[]rune的长度校验(一个中文占3个字节);
|
||||
1. grpool性能压测结果变慢的问题;
|
||||
1. ghttp的热重启的本地进程端口监听,在不使用该特性时默认关闭掉;
|
||||
1. gtcp增加对TLS加密通信的支持;
|
||||
1. 改进gdb对pgsql/mssql/oracle的支持,使用方法覆盖的方式改进操作,而不是完全依靠正则替换的方式;
|
||||
1. gdb的Cache缓存功能增加可自定义缓存接口,以便支持外部缓存功能,缓存接口可以通过io.ReadWriter接口实现;
|
||||
1. 改进ghttp分组路由中对hook的支持方式,以便格式与BindHookHandler统一;
|
||||
1. 使用gconv将slice映射到struct属性上,例如redis hscan的结果集;
|
||||
1. gconv完善针对不同类型的判断,例如:尽量减少sprintf("%v", xxx)来执行string类型的转换;
|
||||
2. ghttp.Server请求执行中增加服务退出的方法,不再执行后续操作;
|
||||
3. ghttp.Response对象完善并改进数据返回方法(Write/WriteString);
|
||||
@ -237,6 +237,9 @@ func (a *Array) PopRands(size int) []interface{} {
|
||||
if size > len(a.array) {
|
||||
size = len(a.array)
|
||||
}
|
||||
if size == 0 {
|
||||
return nil
|
||||
}
|
||||
array := make([]interface{}, size)
|
||||
for i := 0; i < size; i++ {
|
||||
index := grand.Intn(len(a.array))
|
||||
@ -250,6 +253,9 @@ func (a *Array) PopRands(size int) []interface{} {
|
||||
func (a *Array) PopLeft() interface{} {
|
||||
a.mu.Lock()
|
||||
defer a.mu.Unlock()
|
||||
if len(a.array) == 0 {
|
||||
return nil
|
||||
}
|
||||
value := a.array[0]
|
||||
a.array = a.array[1:]
|
||||
return value
|
||||
@ -260,6 +266,9 @@ func (a *Array) PopRight() interface{} {
|
||||
a.mu.Lock()
|
||||
defer a.mu.Unlock()
|
||||
index := len(a.array) - 1
|
||||
if index <= 0 {
|
||||
return nil
|
||||
}
|
||||
value := a.array[index]
|
||||
a.array = a.array[:index]
|
||||
return value
|
||||
@ -270,6 +279,9 @@ func (a *Array) PopLefts(size int) []interface{} {
|
||||
a.mu.Lock()
|
||||
defer a.mu.Unlock()
|
||||
length := len(a.array)
|
||||
if length == 0 {
|
||||
return nil
|
||||
}
|
||||
if size > length {
|
||||
size = length
|
||||
}
|
||||
@ -282,6 +294,9 @@ func (a *Array) PopLefts(size int) []interface{} {
|
||||
func (a *Array) PopRights(size int) []interface{} {
|
||||
a.mu.Lock()
|
||||
defer a.mu.Unlock()
|
||||
if len(a.array) == 0 {
|
||||
return nil
|
||||
}
|
||||
index := len(a.array) - size
|
||||
if index < 0 {
|
||||
index = 0
|
||||
@ -765,3 +780,8 @@ func (a *Array) FilterEmpty() *Array {
|
||||
}
|
||||
return a
|
||||
}
|
||||
|
||||
// IsEmpty checks whether the array is empty.
|
||||
func (a *Array) IsEmpty() bool {
|
||||
return a.Len() == 0
|
||||
}
|
||||
|
||||
@ -176,6 +176,7 @@ func (a *IntArray) InsertAfter(index int, value int) *IntArray {
|
||||
}
|
||||
|
||||
// Remove removes an item by index.
|
||||
// Note that if the index is out of range of array, it returns 0.
|
||||
func (a *IntArray) Remove(index int) int {
|
||||
a.mu.Lock()
|
||||
defer a.mu.Unlock()
|
||||
@ -228,25 +229,39 @@ func (a *IntArray) PushRight(value ...int) *IntArray {
|
||||
}
|
||||
|
||||
// PopLeft pops and returns an item from the beginning of array.
|
||||
// Note that if the array is empty, it returns 0.
|
||||
// Be very careful when use this function in loop statement.
|
||||
// You can use IsEmpty() of Len() == 0 checks if this array empty.
|
||||
func (a *IntArray) PopLeft() int {
|
||||
a.mu.Lock()
|
||||
defer a.mu.Unlock()
|
||||
if len(a.array) == 0 {
|
||||
return 0
|
||||
}
|
||||
value := a.array[0]
|
||||
a.array = a.array[1:]
|
||||
return value
|
||||
}
|
||||
|
||||
// PopRight pops and returns an item from the end of array.
|
||||
// Note that if the array is empty, it returns 0.
|
||||
// Be very careful when use this function in loop statement.
|
||||
// You can use IsEmpty() of Len() == 0 checks if this array empty.
|
||||
func (a *IntArray) PopRight() int {
|
||||
a.mu.Lock()
|
||||
defer a.mu.Unlock()
|
||||
index := len(a.array) - 1
|
||||
if index <= 0 {
|
||||
return 0
|
||||
}
|
||||
value := a.array[index]
|
||||
a.array = a.array[:index]
|
||||
return value
|
||||
}
|
||||
|
||||
// PopRand randomly pops and return an item out of array.
|
||||
// Be very careful when use this function in loop statement.
|
||||
// You can use IsEmpty() of Len() == 0 checks if this array empty.
|
||||
func (a *IntArray) PopRand() int {
|
||||
return a.Remove(grand.Intn(len(a.array)))
|
||||
}
|
||||
@ -258,6 +273,9 @@ func (a *IntArray) PopRands(size int) []int {
|
||||
if size > len(a.array) {
|
||||
size = len(a.array)
|
||||
}
|
||||
if size == 0 {
|
||||
return nil
|
||||
}
|
||||
array := make([]int, size)
|
||||
for i := 0; i < size; i++ {
|
||||
index := grand.Intn(len(a.array))
|
||||
@ -272,6 +290,9 @@ func (a *IntArray) PopLefts(size int) []int {
|
||||
a.mu.Lock()
|
||||
defer a.mu.Unlock()
|
||||
length := len(a.array)
|
||||
if length == 0 {
|
||||
return nil
|
||||
}
|
||||
if size > length {
|
||||
size = length
|
||||
}
|
||||
@ -284,6 +305,9 @@ func (a *IntArray) PopLefts(size int) []int {
|
||||
func (a *IntArray) PopRights(size int) []int {
|
||||
a.mu.Lock()
|
||||
defer a.mu.Unlock()
|
||||
if len(a.array) == 0 {
|
||||
return nil
|
||||
}
|
||||
index := len(a.array) - size
|
||||
if index < 0 {
|
||||
index = 0
|
||||
@ -730,3 +754,8 @@ func (a *IntArray) FilterEmpty() *IntArray {
|
||||
}
|
||||
return a
|
||||
}
|
||||
|
||||
// IsEmpty checks whether the array is empty.
|
||||
func (a *IntArray) IsEmpty() bool {
|
||||
return a.Len() == 0
|
||||
}
|
||||
|
||||
@ -162,6 +162,7 @@ func (a *StrArray) InsertAfter(index int, value string) *StrArray {
|
||||
}
|
||||
|
||||
// Remove removes an item by index.
|
||||
// Note that if the index is out of range of array, it returns an empty string.
|
||||
func (a *StrArray) Remove(index int) string {
|
||||
a.mu.Lock()
|
||||
defer a.mu.Unlock()
|
||||
@ -214,25 +215,40 @@ func (a *StrArray) PushRight(value ...string) *StrArray {
|
||||
}
|
||||
|
||||
// PopLeft pops and returns an item from the beginning of array.
|
||||
// Note that if the array is empty, it returns an empty string.
|
||||
// Be very careful when use this function in loop statement.
|
||||
// You can use IsEmpty() of Len() == 0 checks if this array empty.
|
||||
func (a *StrArray) PopLeft() string {
|
||||
a.mu.Lock()
|
||||
defer a.mu.Unlock()
|
||||
if len(a.array) == 0 {
|
||||
return ""
|
||||
}
|
||||
value := a.array[0]
|
||||
a.array = a.array[1:]
|
||||
return value
|
||||
}
|
||||
|
||||
// PopRight pops and returns an item from the end of array.
|
||||
// Note that if the array is empty, it returns an empty string.
|
||||
// Be very careful when use this function in loop statement.
|
||||
// You can use IsEmpty() of Len() == 0 checks if this array empty.
|
||||
func (a *StrArray) PopRight() string {
|
||||
a.mu.Lock()
|
||||
defer a.mu.Unlock()
|
||||
index := len(a.array) - 1
|
||||
if index <= 0 {
|
||||
return ""
|
||||
}
|
||||
value := a.array[index]
|
||||
a.array = a.array[:index]
|
||||
return value
|
||||
}
|
||||
|
||||
// PopRand randomly pops and return an item out of array.
|
||||
// Note that if the array is empty, it returns an empty string.
|
||||
// Be very careful when use this function in loop statement.
|
||||
// You can use IsEmpty() of Len() == 0 checks if this array empty.
|
||||
func (a *StrArray) PopRand() string {
|
||||
return a.Remove(grand.Intn(len(a.array)))
|
||||
}
|
||||
@ -244,6 +260,9 @@ func (a *StrArray) PopRands(size int) []string {
|
||||
if size > len(a.array) {
|
||||
size = len(a.array)
|
||||
}
|
||||
if size == 0 {
|
||||
return nil
|
||||
}
|
||||
array := make([]string, size)
|
||||
for i := 0; i < size; i++ {
|
||||
index := grand.Intn(len(a.array))
|
||||
@ -261,6 +280,9 @@ func (a *StrArray) PopLefts(size int) []string {
|
||||
if size > length {
|
||||
size = length
|
||||
}
|
||||
if size == 0 {
|
||||
return nil
|
||||
}
|
||||
value := a.array[0:size]
|
||||
a.array = a.array[size:]
|
||||
return value
|
||||
@ -270,6 +292,9 @@ func (a *StrArray) PopLefts(size int) []string {
|
||||
func (a *StrArray) PopRights(size int) []string {
|
||||
a.mu.Lock()
|
||||
defer a.mu.Unlock()
|
||||
if len(a.array) == 0 {
|
||||
return nil
|
||||
}
|
||||
index := len(a.array) - size
|
||||
if index < 0 {
|
||||
index = 0
|
||||
@ -730,3 +755,8 @@ func (a *StrArray) FilterEmpty() *StrArray {
|
||||
}
|
||||
return a
|
||||
}
|
||||
|
||||
// IsEmpty checks whether the array is empty.
|
||||
func (a *StrArray) IsEmpty() bool {
|
||||
return a.Len() == 0
|
||||
}
|
||||
|
||||
@ -33,9 +33,9 @@ type SortedArray struct {
|
||||
// NewSortedArray creates and returns an empty sorted array.
|
||||
// The parameter <safe> is used to specify whether using array in concurrent-safety, which is false in default.
|
||||
// The parameter <comparator> used to compare values to sort in array,
|
||||
// if it returns value < 0, means v1 < v2;
|
||||
// if it returns value = 0, means v1 = v2;
|
||||
// if it returns value > 0, means v1 > v2;
|
||||
// if it returns value < 0, means v1 < v2; the v1 will be inserted before v2;
|
||||
// if it returns value = 0, means v1 = v2; the v1 will be replaced by v2;
|
||||
// if it returns value > 0, means v1 > v2; the v1 will be inserted after v2;
|
||||
func NewSortedArray(comparator func(a, b interface{}) int, safe ...bool) *SortedArray {
|
||||
return NewSortedArraySize(0, comparator, safe...)
|
||||
}
|
||||
@ -196,6 +196,9 @@ func (a *SortedArray) RemoveValue(value interface{}) bool {
|
||||
func (a *SortedArray) PopLeft() interface{} {
|
||||
a.mu.Lock()
|
||||
defer a.mu.Unlock()
|
||||
if len(a.array) == 0 {
|
||||
return nil
|
||||
}
|
||||
value := a.array[0]
|
||||
a.array = a.array[1:]
|
||||
return value
|
||||
@ -206,6 +209,9 @@ func (a *SortedArray) PopRight() interface{} {
|
||||
a.mu.Lock()
|
||||
defer a.mu.Unlock()
|
||||
index := len(a.array) - 1
|
||||
if index <= 0 {
|
||||
return nil
|
||||
}
|
||||
value := a.array[index]
|
||||
a.array = a.array[:index]
|
||||
return value
|
||||
@ -223,6 +229,9 @@ func (a *SortedArray) PopRands(size int) []interface{} {
|
||||
if size > len(a.array) {
|
||||
size = len(a.array)
|
||||
}
|
||||
if size == 0 {
|
||||
return nil
|
||||
}
|
||||
array := make([]interface{}, size)
|
||||
for i := 0; i < size; i++ {
|
||||
index := grand.Intn(len(a.array))
|
||||
@ -240,6 +249,9 @@ func (a *SortedArray) PopLefts(size int) []interface{} {
|
||||
if size > length {
|
||||
size = length
|
||||
}
|
||||
if size == 0 {
|
||||
return nil
|
||||
}
|
||||
value := a.array[0:size]
|
||||
a.array = a.array[size:]
|
||||
return value
|
||||
@ -249,6 +261,9 @@ func (a *SortedArray) PopLefts(size int) []interface{} {
|
||||
func (a *SortedArray) PopRights(size int) []interface{} {
|
||||
a.mu.Lock()
|
||||
defer a.mu.Unlock()
|
||||
if len(a.array) == 0 {
|
||||
return nil
|
||||
}
|
||||
index := len(a.array) - size
|
||||
if index < 0 {
|
||||
index = 0
|
||||
@ -733,3 +748,8 @@ func (a *SortedArray) FilterEmpty() *SortedArray {
|
||||
}
|
||||
return a
|
||||
}
|
||||
|
||||
// IsEmpty checks whether the array is empty.
|
||||
func (a *SortedArray) IsEmpty() bool {
|
||||
return a.Len() == 0
|
||||
}
|
||||
|
||||
@ -143,6 +143,7 @@ func (a *SortedIntArray) Get(index int) int {
|
||||
}
|
||||
|
||||
// Remove removes an item by index.
|
||||
// Note that if the index is out of range of array, it returns 0.
|
||||
func (a *SortedIntArray) Remove(index int) int {
|
||||
a.mu.Lock()
|
||||
defer a.mu.Unlock()
|
||||
@ -178,25 +179,40 @@ func (a *SortedIntArray) RemoveValue(value int) bool {
|
||||
}
|
||||
|
||||
// PopLeft pops and returns an item from the beginning of array.
|
||||
// Note that if the array is empty, it returns 0.
|
||||
// Be very careful when use this function in loop statement.
|
||||
// You can use IsEmpty() of Len() == 0 checks if this array empty.
|
||||
func (a *SortedIntArray) PopLeft() int {
|
||||
a.mu.Lock()
|
||||
defer a.mu.Unlock()
|
||||
if len(a.array) == 0 {
|
||||
return 0
|
||||
}
|
||||
value := a.array[0]
|
||||
a.array = a.array[1:]
|
||||
return value
|
||||
}
|
||||
|
||||
// PopRight pops and returns an item from the end of array.
|
||||
// Note that if the array is empty, it returns 0.
|
||||
// Be very careful when use this function in loop statement.
|
||||
// You can use IsEmpty() of Len() == 0 checks if this array empty.
|
||||
func (a *SortedIntArray) PopRight() int {
|
||||
a.mu.Lock()
|
||||
defer a.mu.Unlock()
|
||||
index := len(a.array) - 1
|
||||
if index <= 0 {
|
||||
return 0
|
||||
}
|
||||
value := a.array[index]
|
||||
a.array = a.array[:index]
|
||||
return value
|
||||
}
|
||||
|
||||
// PopRand randomly pops and return an item out of array.
|
||||
// Note that if the array is empty, it returns 0.
|
||||
// Be very careful when use this function in loop statement.
|
||||
// You can use IsEmpty() of Len() == 0 checks if this array empty.
|
||||
func (a *SortedIntArray) PopRand() int {
|
||||
return a.Remove(grand.Intn(len(a.array)))
|
||||
}
|
||||
@ -208,6 +224,9 @@ func (a *SortedIntArray) PopRands(size int) []int {
|
||||
if size > len(a.array) {
|
||||
size = len(a.array)
|
||||
}
|
||||
if size == 0 {
|
||||
return nil
|
||||
}
|
||||
array := make([]int, size)
|
||||
for i := 0; i < size; i++ {
|
||||
index := grand.Intn(len(a.array))
|
||||
@ -225,6 +244,9 @@ func (a *SortedIntArray) PopLefts(size int) []int {
|
||||
if size > length {
|
||||
size = length
|
||||
}
|
||||
if size == 0 {
|
||||
return nil
|
||||
}
|
||||
value := a.array[0:size]
|
||||
a.array = a.array[size:]
|
||||
return value
|
||||
@ -234,6 +256,9 @@ func (a *SortedIntArray) PopLefts(size int) []int {
|
||||
func (a *SortedIntArray) PopRights(size int) []int {
|
||||
a.mu.Lock()
|
||||
defer a.mu.Unlock()
|
||||
if len(a.array) == 0 {
|
||||
return nil
|
||||
}
|
||||
index := len(a.array) - size
|
||||
if index < 0 {
|
||||
index = 0
|
||||
@ -680,3 +705,8 @@ func (a *SortedIntArray) FilterEmpty() *SortedIntArray {
|
||||
}
|
||||
return a
|
||||
}
|
||||
|
||||
// IsEmpty checks whether the array is empty.
|
||||
func (a *SortedIntArray) IsEmpty() bool {
|
||||
return a.Len() == 0
|
||||
}
|
||||
|
||||
@ -128,6 +128,7 @@ func (a *SortedStrArray) Get(index int) string {
|
||||
}
|
||||
|
||||
// Remove removes an item by index.
|
||||
// Note that if the index is out of range of array, it returns an empty string.
|
||||
func (a *SortedStrArray) Remove(index int) string {
|
||||
a.mu.Lock()
|
||||
defer a.mu.Unlock()
|
||||
@ -163,25 +164,40 @@ func (a *SortedStrArray) RemoveValue(value string) bool {
|
||||
}
|
||||
|
||||
// PopLeft pops and returns an item from the beginning of array.
|
||||
// Note that if the array is empty, it returns an empty string.
|
||||
// Be very careful when use this function in loop statement.
|
||||
// You can use IsEmpty() of Len() == 0 checks if this array empty.
|
||||
func (a *SortedStrArray) PopLeft() string {
|
||||
a.mu.Lock()
|
||||
defer a.mu.Unlock()
|
||||
if len(a.array) == 0 {
|
||||
return ""
|
||||
}
|
||||
value := a.array[0]
|
||||
a.array = a.array[1:]
|
||||
return value
|
||||
}
|
||||
|
||||
// PopRight pops and returns an item from the end of array.
|
||||
// Note that if the array is empty, it returns an empty string.
|
||||
// Be very careful when use this function in loop statement.
|
||||
// You can use IsEmpty() of Len() == 0 checks if this array empty.
|
||||
func (a *SortedStrArray) PopRight() string {
|
||||
a.mu.Lock()
|
||||
defer a.mu.Unlock()
|
||||
index := len(a.array) - 1
|
||||
if index <= 0 {
|
||||
return ""
|
||||
}
|
||||
value := a.array[index]
|
||||
a.array = a.array[:index]
|
||||
return value
|
||||
}
|
||||
|
||||
// PopRand randomly pops and return an item out of array.
|
||||
// Note that if the array is empty, it returns an empty string.
|
||||
// Be very careful when use this function in loop statement.
|
||||
// You can use IsEmpty() of Len() == 0 checks if this array empty.
|
||||
func (a *SortedStrArray) PopRand() string {
|
||||
return a.Remove(grand.Intn(len(a.array)))
|
||||
}
|
||||
@ -193,6 +209,9 @@ func (a *SortedStrArray) PopRands(size int) []string {
|
||||
if size > len(a.array) {
|
||||
size = len(a.array)
|
||||
}
|
||||
if size == 0 {
|
||||
return nil
|
||||
}
|
||||
array := make([]string, size)
|
||||
for i := 0; i < size; i++ {
|
||||
index := grand.Intn(len(a.array))
|
||||
@ -210,6 +229,9 @@ func (a *SortedStrArray) PopLefts(size int) []string {
|
||||
if size > length {
|
||||
size = length
|
||||
}
|
||||
if size == 0 {
|
||||
return nil
|
||||
}
|
||||
value := a.array[0:size]
|
||||
a.array = a.array[size:]
|
||||
return value
|
||||
@ -219,6 +241,9 @@ func (a *SortedStrArray) PopLefts(size int) []string {
|
||||
func (a *SortedStrArray) PopRights(size int) []string {
|
||||
a.mu.Lock()
|
||||
defer a.mu.Unlock()
|
||||
if len(a.array) == 0 {
|
||||
return nil
|
||||
}
|
||||
index := len(a.array) - size
|
||||
if index < 0 {
|
||||
index = 0
|
||||
@ -676,3 +701,8 @@ func (a *SortedStrArray) FilterEmpty() *SortedStrArray {
|
||||
}
|
||||
return a
|
||||
}
|
||||
|
||||
// IsEmpty checks whether the array is empty.
|
||||
func (a *SortedStrArray) IsEmpty() bool {
|
||||
return a.Len() == 0
|
||||
}
|
||||
|
||||
@ -8,51 +8,52 @@ package garray_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
|
||||
"github.com/gogf/gf/container/garray"
|
||||
)
|
||||
|
||||
func Example_basic() {
|
||||
// 创建普通的数组
|
||||
// A normal array.
|
||||
a := garray.New()
|
||||
|
||||
// 添加数据项
|
||||
// Adding items.
|
||||
for i := 0; i < 10; i++ {
|
||||
a.Append(i)
|
||||
}
|
||||
|
||||
// 获取当前数组长度
|
||||
// Print the array length.
|
||||
fmt.Println(a.Len())
|
||||
|
||||
// 获取当前数据项列表
|
||||
// Print the array items.
|
||||
fmt.Println(a.Slice())
|
||||
|
||||
// 获取指定索引项
|
||||
// Retrieve item by index.
|
||||
fmt.Println(a.Get(6))
|
||||
|
||||
// 查找指定数据项是否存在
|
||||
// Check item existence.
|
||||
fmt.Println(a.Contains(6))
|
||||
fmt.Println(a.Contains(100))
|
||||
|
||||
// 在指定索引前插入数据项
|
||||
// Insert item before specified index.
|
||||
a.InsertAfter(9, 11)
|
||||
// 在指定索引后插入数据项
|
||||
// Insert item after specified index.
|
||||
a.InsertBefore(10, 10)
|
||||
|
||||
fmt.Println(a.Slice())
|
||||
|
||||
// 修改指定索引的数据项
|
||||
// Modify item by index.
|
||||
a.Set(0, 100)
|
||||
fmt.Println(a.Slice())
|
||||
|
||||
// 搜索数据项,返回搜索到的索引位置
|
||||
// Search item and return its index.
|
||||
fmt.Println(a.Search(5))
|
||||
|
||||
// 删除指定索引的数据项
|
||||
// Remove item by index.
|
||||
a.Remove(0)
|
||||
fmt.Println(a.Slice())
|
||||
|
||||
// 清空数组
|
||||
// Empty the array, removes all items of it.
|
||||
fmt.Println(a.Slice())
|
||||
a.Clear()
|
||||
fmt.Println(a.Slice())
|
||||
@ -73,13 +74,21 @@ func Example_basic() {
|
||||
|
||||
func Example_rand() {
|
||||
array := garray.NewFrom([]interface{}{1, 2, 3, 4, 5, 6, 7, 8, 9})
|
||||
// 随机返回两个数据项(不删除)
|
||||
|
||||
// Randomly retrieve and return 2 items from the array.
|
||||
// It does not delete the items from array.
|
||||
fmt.Println(array.Rands(2))
|
||||
|
||||
// Randomly pick and return one item from the array.
|
||||
// It deletes the picked up item from array.
|
||||
fmt.Println(array.PopRand())
|
||||
}
|
||||
|
||||
func Example_pop() {
|
||||
func Example_popItem() {
|
||||
array := garray.NewFrom([]interface{}{1, 2, 3, 4, 5, 6, 7, 8, 9})
|
||||
|
||||
// Any Pop* functions pick, delete and return the item from array.
|
||||
|
||||
fmt.Println(array.PopLeft())
|
||||
fmt.Println(array.PopLefts(2))
|
||||
fmt.Println(array.PopRight())
|
||||
@ -92,7 +101,7 @@ func Example_pop() {
|
||||
// [7 8]
|
||||
}
|
||||
|
||||
func Example_merge() {
|
||||
func Example_mergeArray() {
|
||||
array1 := garray.NewFrom([]interface{}{1, 2})
|
||||
array2 := garray.NewFrom([]interface{}{3, 4})
|
||||
slice1 := []interface{}{5, 6}
|
||||
@ -110,3 +119,14 @@ func Example_merge() {
|
||||
// [1 2]
|
||||
// [1 2 1 2 3 4 5 6 7 8 9 0]
|
||||
}
|
||||
|
||||
func Example_filter() {
|
||||
array1 := garray.NewFrom(g.Slice{0, 1, 2, nil, "", g.Slice{}, "john"})
|
||||
array2 := garray.NewFrom(g.Slice{0, 1, 2, nil, "", g.Slice{}, "john"})
|
||||
fmt.Printf("%#v\n", array1.FilterNil().Slice())
|
||||
fmt.Printf("%#v\n", array2.FilterEmpty().Slice())
|
||||
|
||||
// Output:
|
||||
// []interface {}{0, 1, 2, "", []interface {}{}, "john"}
|
||||
// []interface {}{1, 2, "john"}
|
||||
}
|
||||
|
||||
@ -18,89 +18,103 @@ import (
|
||||
)
|
||||
|
||||
func Test_IntArray_Unique(t *testing.T) {
|
||||
expect := []int{1, 2, 3, 4, 5, 6}
|
||||
array := garray.NewIntArray()
|
||||
array.Append(1, 1, 2, 3, 3, 4, 4, 5, 5, 6, 6)
|
||||
array.Unique()
|
||||
gtest.Assert(array.Slice(), expect)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
expect := []int{1, 2, 3, 4, 5, 6}
|
||||
array := garray.NewIntArray()
|
||||
array.Append(1, 1, 2, 3, 3, 4, 4, 5, 5, 6, 6)
|
||||
array.Unique()
|
||||
t.Assert(array.Slice(), expect)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_SortedIntArray1(t *testing.T) {
|
||||
expect := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
|
||||
array := garray.NewSortedIntArray()
|
||||
for i := 10; i > -1; i-- {
|
||||
array.Add(i)
|
||||
}
|
||||
gtest.Assert(array.Slice(), expect)
|
||||
gtest.Assert(array.Add().Slice(), expect)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
expect := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
|
||||
array := garray.NewSortedIntArray()
|
||||
for i := 10; i > -1; i-- {
|
||||
array.Add(i)
|
||||
}
|
||||
t.Assert(array.Slice(), expect)
|
||||
t.Assert(array.Add().Slice(), expect)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_SortedIntArray2(t *testing.T) {
|
||||
expect := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
|
||||
array := garray.NewSortedIntArray()
|
||||
for i := 0; i <= 10; i++ {
|
||||
array.Add(i)
|
||||
}
|
||||
gtest.Assert(array.Slice(), expect)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
expect := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
|
||||
array := garray.NewSortedIntArray()
|
||||
for i := 0; i <= 10; i++ {
|
||||
array.Add(i)
|
||||
}
|
||||
t.Assert(array.Slice(), expect)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_SortedStrArray1(t *testing.T) {
|
||||
expect := []string{"0", "1", "10", "2", "3", "4", "5", "6", "7", "8", "9"}
|
||||
array1 := garray.NewSortedStrArray()
|
||||
array2 := garray.NewSortedStrArray(true)
|
||||
for i := 10; i > -1; i-- {
|
||||
array1.Add(gconv.String(i))
|
||||
array2.Add(gconv.String(i))
|
||||
}
|
||||
gtest.Assert(array1.Slice(), expect)
|
||||
gtest.Assert(array2.Slice(), expect)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
expect := []string{"0", "1", "10", "2", "3", "4", "5", "6", "7", "8", "9"}
|
||||
array1 := garray.NewSortedStrArray()
|
||||
array2 := garray.NewSortedStrArray(true)
|
||||
for i := 10; i > -1; i-- {
|
||||
array1.Add(gconv.String(i))
|
||||
array2.Add(gconv.String(i))
|
||||
}
|
||||
t.Assert(array1.Slice(), expect)
|
||||
t.Assert(array2.Slice(), expect)
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
func Test_SortedStrArray2(t *testing.T) {
|
||||
expect := []string{"0", "1", "10", "2", "3", "4", "5", "6", "7", "8", "9"}
|
||||
array := garray.NewSortedStrArray()
|
||||
for i := 0; i <= 10; i++ {
|
||||
array.Add(gconv.String(i))
|
||||
}
|
||||
gtest.Assert(array.Slice(), expect)
|
||||
array.Add()
|
||||
gtest.Assert(array.Slice(), expect)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
expect := []string{"0", "1", "10", "2", "3", "4", "5", "6", "7", "8", "9"}
|
||||
array := garray.NewSortedStrArray()
|
||||
for i := 0; i <= 10; i++ {
|
||||
array.Add(gconv.String(i))
|
||||
}
|
||||
t.Assert(array.Slice(), expect)
|
||||
array.Add()
|
||||
t.Assert(array.Slice(), expect)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_SortedArray1(t *testing.T) {
|
||||
expect := []string{"0", "1", "10", "2", "3", "4", "5", "6", "7", "8", "9"}
|
||||
array := garray.NewSortedArray(func(v1, v2 interface{}) int {
|
||||
return strings.Compare(gconv.String(v1), gconv.String(v2))
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
expect := []string{"0", "1", "10", "2", "3", "4", "5", "6", "7", "8", "9"}
|
||||
array := garray.NewSortedArray(func(v1, v2 interface{}) int {
|
||||
return strings.Compare(gconv.String(v1), gconv.String(v2))
|
||||
})
|
||||
for i := 10; i > -1; i-- {
|
||||
array.Add(gconv.String(i))
|
||||
}
|
||||
t.Assert(array.Slice(), expect)
|
||||
})
|
||||
for i := 10; i > -1; i-- {
|
||||
array.Add(gconv.String(i))
|
||||
}
|
||||
gtest.Assert(array.Slice(), expect)
|
||||
}
|
||||
|
||||
func Test_SortedArray2(t *testing.T) {
|
||||
expect := []string{"0", "1", "10", "2", "3", "4", "5", "6", "7", "8", "9"}
|
||||
func1 := func(v1, v2 interface{}) int {
|
||||
return strings.Compare(gconv.String(v1), gconv.String(v2))
|
||||
}
|
||||
array := garray.NewSortedArray(func1)
|
||||
array2 := garray.NewSortedArray(func1, true)
|
||||
for i := 0; i <= 10; i++ {
|
||||
array.Add(gconv.String(i))
|
||||
array2.Add(gconv.String(i))
|
||||
}
|
||||
gtest.Assert(array.Slice(), expect)
|
||||
gtest.Assert(array.Add().Slice(), expect)
|
||||
gtest.Assert(array2.Slice(), expect)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
expect := []string{"0", "1", "10", "2", "3", "4", "5", "6", "7", "8", "9"}
|
||||
func1 := func(v1, v2 interface{}) int {
|
||||
return strings.Compare(gconv.String(v1), gconv.String(v2))
|
||||
}
|
||||
array := garray.NewSortedArray(func1)
|
||||
array2 := garray.NewSortedArray(func1, true)
|
||||
for i := 0; i <= 10; i++ {
|
||||
array.Add(gconv.String(i))
|
||||
array2.Add(gconv.String(i))
|
||||
}
|
||||
t.Assert(array.Slice(), expect)
|
||||
t.Assert(array.Add().Slice(), expect)
|
||||
t.Assert(array2.Slice(), expect)
|
||||
})
|
||||
}
|
||||
|
||||
func TestNewFromCopy(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []interface{}{"100", "200", "300", "400", "500", "600"}
|
||||
array1 := garray.NewFromCopy(a1)
|
||||
gtest.AssertIN(array1.PopRands(2), a1)
|
||||
gtest.Assert(len(array1.PopRands(1)), 1)
|
||||
gtest.Assert(len(array1.PopRands(9)), 3)
|
||||
t.AssertIN(array1.PopRands(2), a1)
|
||||
t.Assert(len(array1.PopRands(1)), 1)
|
||||
t.Assert(len(array1.PopRands(9)), 3)
|
||||
})
|
||||
}
|
||||
|
||||
@ -20,41 +20,41 @@ import (
|
||||
)
|
||||
|
||||
func Test_Array_Basic(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
expect := []interface{}{0, 1, 2, 3}
|
||||
array := garray.NewArrayFrom(expect)
|
||||
array2 := garray.NewArrayFrom(expect)
|
||||
array3 := garray.NewArrayFrom([]interface{}{})
|
||||
gtest.Assert(array.Slice(), expect)
|
||||
gtest.Assert(array.Interfaces(), expect)
|
||||
t.Assert(array.Slice(), expect)
|
||||
t.Assert(array.Interfaces(), expect)
|
||||
array.Set(0, 100)
|
||||
gtest.Assert(array.Get(0), 100)
|
||||
gtest.Assert(array.Get(1), 1)
|
||||
gtest.Assert(array.Search(100), 0)
|
||||
gtest.Assert(array3.Search(100), -1)
|
||||
gtest.Assert(array.Contains(100), true)
|
||||
gtest.Assert(array.Remove(0), 100)
|
||||
gtest.Assert(array.Remove(-1), nil)
|
||||
gtest.Assert(array.Remove(100000), nil)
|
||||
t.Assert(array.Get(0), 100)
|
||||
t.Assert(array.Get(1), 1)
|
||||
t.Assert(array.Search(100), 0)
|
||||
t.Assert(array3.Search(100), -1)
|
||||
t.Assert(array.Contains(100), true)
|
||||
t.Assert(array.Remove(0), 100)
|
||||
t.Assert(array.Remove(-1), nil)
|
||||
t.Assert(array.Remove(100000), nil)
|
||||
|
||||
gtest.Assert(array2.Remove(3), 3)
|
||||
gtest.Assert(array2.Remove(1), 1)
|
||||
t.Assert(array2.Remove(3), 3)
|
||||
t.Assert(array2.Remove(1), 1)
|
||||
|
||||
gtest.Assert(array.Contains(100), false)
|
||||
t.Assert(array.Contains(100), false)
|
||||
array.Append(4)
|
||||
gtest.Assert(array.Len(), 4)
|
||||
t.Assert(array.Len(), 4)
|
||||
array.InsertBefore(0, 100)
|
||||
array.InsertAfter(0, 200)
|
||||
gtest.Assert(array.Slice(), []interface{}{100, 200, 2, 2, 3, 4})
|
||||
t.Assert(array.Slice(), []interface{}{100, 200, 2, 2, 3, 4})
|
||||
array.InsertBefore(5, 300)
|
||||
array.InsertAfter(6, 400)
|
||||
gtest.Assert(array.Slice(), []interface{}{100, 200, 2, 2, 3, 300, 4, 400})
|
||||
gtest.Assert(array.Clear().Len(), 0)
|
||||
t.Assert(array.Slice(), []interface{}{100, 200, 2, 2, 3, 300, 4, 400})
|
||||
t.Assert(array.Clear().Len(), 0)
|
||||
})
|
||||
}
|
||||
|
||||
func TestArray_Sort(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
expect1 := []interface{}{0, 1, 2, 3}
|
||||
expect2 := []interface{}{3, 2, 1, 0}
|
||||
array := garray.NewArray()
|
||||
@ -64,78 +64,88 @@ func TestArray_Sort(t *testing.T) {
|
||||
array.SortFunc(func(v1, v2 interface{}) bool {
|
||||
return v1.(int) < v2.(int)
|
||||
})
|
||||
gtest.Assert(array.Slice(), expect1)
|
||||
t.Assert(array.Slice(), expect1)
|
||||
array.SortFunc(func(v1, v2 interface{}) bool {
|
||||
return v1.(int) > v2.(int)
|
||||
})
|
||||
gtest.Assert(array.Slice(), expect2)
|
||||
t.Assert(array.Slice(), expect2)
|
||||
})
|
||||
}
|
||||
|
||||
func TestArray_Unique(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
expect := []interface{}{1, 1, 2, 3}
|
||||
array := garray.NewArrayFrom(expect)
|
||||
gtest.Assert(array.Unique().Slice(), []interface{}{1, 2, 3})
|
||||
t.Assert(array.Unique().Slice(), []interface{}{1, 2, 3})
|
||||
})
|
||||
}
|
||||
|
||||
func TestArray_PushAndPop(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
expect := []interface{}{0, 1, 2, 3}
|
||||
array := garray.NewArrayFrom(expect)
|
||||
gtest.Assert(array.Slice(), expect)
|
||||
gtest.Assert(array.PopLeft(), 0)
|
||||
gtest.Assert(array.PopRight(), 3)
|
||||
gtest.AssertIN(array.PopRand(), []interface{}{1, 2})
|
||||
gtest.AssertIN(array.PopRand(), []interface{}{1, 2})
|
||||
gtest.Assert(array.Len(), 0)
|
||||
t.Assert(array.Slice(), expect)
|
||||
t.Assert(array.PopLeft(), 0)
|
||||
t.Assert(array.PopRight(), 3)
|
||||
t.AssertIN(array.PopRand(), []interface{}{1, 2})
|
||||
t.AssertIN(array.PopRand(), []interface{}{1, 2})
|
||||
t.Assert(array.Len(), 0)
|
||||
array.PushLeft(1).PushRight(2)
|
||||
gtest.Assert(array.Slice(), []interface{}{1, 2})
|
||||
t.Assert(array.Slice(), []interface{}{1, 2})
|
||||
})
|
||||
}
|
||||
|
||||
func TestArray_PopRands(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []interface{}{100, 200, 300, 400, 500, 600}
|
||||
array := garray.NewFromCopy(a1)
|
||||
gtest.AssertIN(array.PopRands(2), []interface{}{100, 200, 300, 400, 500, 600})
|
||||
t.AssertIN(array.PopRands(2), []interface{}{100, 200, 300, 400, 500, 600})
|
||||
})
|
||||
}
|
||||
|
||||
func TestArray_PopLeftsAndPopRights(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
array := garray.New()
|
||||
t.Assert(array.PopLeft(), nil)
|
||||
t.Assert(array.PopLefts(10), nil)
|
||||
t.Assert(array.PopRight(), nil)
|
||||
t.Assert(array.PopRights(10), nil)
|
||||
t.Assert(array.PopRand(), nil)
|
||||
t.Assert(array.PopRands(10), nil)
|
||||
})
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
value1 := []interface{}{0, 1, 2, 3, 4, 5, 6}
|
||||
value2 := []interface{}{0, 1, 2, 3, 4, 5, 6}
|
||||
array1 := garray.NewArrayFrom(value1)
|
||||
array2 := garray.NewArrayFrom(value2)
|
||||
gtest.Assert(array1.PopLefts(2), []interface{}{0, 1})
|
||||
gtest.Assert(array1.Slice(), []interface{}{2, 3, 4, 5, 6})
|
||||
gtest.Assert(array1.PopRights(2), []interface{}{5, 6})
|
||||
gtest.Assert(array1.Slice(), []interface{}{2, 3, 4})
|
||||
gtest.Assert(array1.PopRights(20), []interface{}{2, 3, 4})
|
||||
gtest.Assert(array1.Slice(), []interface{}{})
|
||||
gtest.Assert(array2.PopLefts(20), []interface{}{0, 1, 2, 3, 4, 5, 6})
|
||||
gtest.Assert(array2.Slice(), []interface{}{})
|
||||
t.Assert(array1.PopLefts(2), []interface{}{0, 1})
|
||||
t.Assert(array1.Slice(), []interface{}{2, 3, 4, 5, 6})
|
||||
t.Assert(array1.PopRights(2), []interface{}{5, 6})
|
||||
t.Assert(array1.Slice(), []interface{}{2, 3, 4})
|
||||
t.Assert(array1.PopRights(20), []interface{}{2, 3, 4})
|
||||
t.Assert(array1.Slice(), []interface{}{})
|
||||
t.Assert(array2.PopLefts(20), []interface{}{0, 1, 2, 3, 4, 5, 6})
|
||||
t.Assert(array2.Slice(), []interface{}{})
|
||||
})
|
||||
}
|
||||
|
||||
func TestArray_Range(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
value1 := []interface{}{0, 1, 2, 3, 4, 5, 6}
|
||||
array1 := garray.NewArrayFrom(value1)
|
||||
array2 := garray.NewArrayFrom(value1, true)
|
||||
gtest.Assert(array1.Range(0, 1), []interface{}{0})
|
||||
gtest.Assert(array1.Range(1, 2), []interface{}{1})
|
||||
gtest.Assert(array1.Range(0, 2), []interface{}{0, 1})
|
||||
gtest.Assert(array1.Range(-1, 10), value1)
|
||||
gtest.Assert(array1.Range(10, 2), nil)
|
||||
gtest.Assert(array2.Range(1, 3), []interface{}{1, 2})
|
||||
t.Assert(array1.Range(0, 1), []interface{}{0})
|
||||
t.Assert(array1.Range(1, 2), []interface{}{1})
|
||||
t.Assert(array1.Range(0, 2), []interface{}{0, 1})
|
||||
t.Assert(array1.Range(-1, 10), value1)
|
||||
t.Assert(array1.Range(10, 2), nil)
|
||||
t.Assert(array2.Range(1, 3), []interface{}{1, 2})
|
||||
})
|
||||
}
|
||||
|
||||
func TestArray_Merge(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
func1 := func(v1, v2 interface{}) int {
|
||||
if gconv.Int(v1) < gconv.Int(v2) {
|
||||
return 0
|
||||
@ -147,7 +157,7 @@ func TestArray_Merge(t *testing.T) {
|
||||
i2 := []interface{}{4, 5, 6, 7}
|
||||
array1 := garray.NewArrayFrom(i1)
|
||||
array2 := garray.NewArrayFrom(i2)
|
||||
gtest.Assert(array1.Merge(array2).Slice(), []interface{}{0, 1, 2, 3, 4, 5, 6, 7})
|
||||
t.Assert(array1.Merge(array2).Slice(), []interface{}{0, 1, 2, 3, 4, 5, 6, 7})
|
||||
|
||||
//s1 := []string{"a", "b", "c", "d"}
|
||||
s2 := []string{"e", "f"}
|
||||
@ -159,185 +169,185 @@ func TestArray_Merge(t *testing.T) {
|
||||
s6 := garray.NewSortedIntArrayFrom([]int{1, 2, 3})
|
||||
a1 := garray.NewArrayFrom(i1)
|
||||
|
||||
gtest.Assert(a1.Merge(s2).Len(), 6)
|
||||
gtest.Assert(a1.Merge(i3).Len(), 9)
|
||||
gtest.Assert(a1.Merge(i4).Len(), 10)
|
||||
gtest.Assert(a1.Merge(s3).Len(), 12)
|
||||
gtest.Assert(a1.Merge(s4).Len(), 14)
|
||||
gtest.Assert(a1.Merge(s5).Len(), 16)
|
||||
gtest.Assert(a1.Merge(s6).Len(), 19)
|
||||
t.Assert(a1.Merge(s2).Len(), 6)
|
||||
t.Assert(a1.Merge(i3).Len(), 9)
|
||||
t.Assert(a1.Merge(i4).Len(), 10)
|
||||
t.Assert(a1.Merge(s3).Len(), 12)
|
||||
t.Assert(a1.Merge(s4).Len(), 14)
|
||||
t.Assert(a1.Merge(s5).Len(), 16)
|
||||
t.Assert(a1.Merge(s6).Len(), 19)
|
||||
})
|
||||
}
|
||||
|
||||
func TestArray_Fill(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []interface{}{0}
|
||||
a2 := []interface{}{0}
|
||||
array1 := garray.NewArrayFrom(a1)
|
||||
array2 := garray.NewArrayFrom(a2, true)
|
||||
gtest.Assert(array1.Fill(1, 2, 100).Slice(), []interface{}{0, 100, 100})
|
||||
gtest.Assert(array2.Fill(0, 2, 100).Slice(), []interface{}{100, 100})
|
||||
gtest.Assert(array2.Fill(-1, 2, 100).Slice(), []interface{}{100, 100})
|
||||
t.Assert(array1.Fill(1, 2, 100).Slice(), []interface{}{0, 100, 100})
|
||||
t.Assert(array2.Fill(0, 2, 100).Slice(), []interface{}{100, 100})
|
||||
t.Assert(array2.Fill(-1, 2, 100).Slice(), []interface{}{100, 100})
|
||||
})
|
||||
}
|
||||
|
||||
func TestArray_Chunk(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []interface{}{1, 2, 3, 4, 5}
|
||||
array1 := garray.NewArrayFrom(a1)
|
||||
chunks := array1.Chunk(2)
|
||||
gtest.Assert(len(chunks), 3)
|
||||
gtest.Assert(chunks[0], []interface{}{1, 2})
|
||||
gtest.Assert(chunks[1], []interface{}{3, 4})
|
||||
gtest.Assert(chunks[2], []interface{}{5})
|
||||
gtest.Assert(array1.Chunk(0), nil)
|
||||
t.Assert(len(chunks), 3)
|
||||
t.Assert(chunks[0], []interface{}{1, 2})
|
||||
t.Assert(chunks[1], []interface{}{3, 4})
|
||||
t.Assert(chunks[2], []interface{}{5})
|
||||
t.Assert(array1.Chunk(0), nil)
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []interface{}{1, 2, 3, 4, 5}
|
||||
array1 := garray.NewArrayFrom(a1)
|
||||
chunks := array1.Chunk(3)
|
||||
gtest.Assert(len(chunks), 2)
|
||||
gtest.Assert(chunks[0], []interface{}{1, 2, 3})
|
||||
gtest.Assert(chunks[1], []interface{}{4, 5})
|
||||
gtest.Assert(array1.Chunk(0), nil)
|
||||
t.Assert(len(chunks), 2)
|
||||
t.Assert(chunks[0], []interface{}{1, 2, 3})
|
||||
t.Assert(chunks[1], []interface{}{4, 5})
|
||||
t.Assert(array1.Chunk(0), nil)
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []interface{}{1, 2, 3, 4, 5, 6}
|
||||
array1 := garray.NewArrayFrom(a1)
|
||||
chunks := array1.Chunk(2)
|
||||
gtest.Assert(len(chunks), 3)
|
||||
gtest.Assert(chunks[0], []interface{}{1, 2})
|
||||
gtest.Assert(chunks[1], []interface{}{3, 4})
|
||||
gtest.Assert(chunks[2], []interface{}{5, 6})
|
||||
gtest.Assert(array1.Chunk(0), nil)
|
||||
t.Assert(len(chunks), 3)
|
||||
t.Assert(chunks[0], []interface{}{1, 2})
|
||||
t.Assert(chunks[1], []interface{}{3, 4})
|
||||
t.Assert(chunks[2], []interface{}{5, 6})
|
||||
t.Assert(array1.Chunk(0), nil)
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []interface{}{1, 2, 3, 4, 5, 6}
|
||||
array1 := garray.NewArrayFrom(a1)
|
||||
chunks := array1.Chunk(3)
|
||||
gtest.Assert(len(chunks), 2)
|
||||
gtest.Assert(chunks[0], []interface{}{1, 2, 3})
|
||||
gtest.Assert(chunks[1], []interface{}{4, 5, 6})
|
||||
gtest.Assert(array1.Chunk(0), nil)
|
||||
t.Assert(len(chunks), 2)
|
||||
t.Assert(chunks[0], []interface{}{1, 2, 3})
|
||||
t.Assert(chunks[1], []interface{}{4, 5, 6})
|
||||
t.Assert(array1.Chunk(0), nil)
|
||||
})
|
||||
}
|
||||
|
||||
func TestArray_Pad(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []interface{}{0}
|
||||
array1 := garray.NewArrayFrom(a1)
|
||||
gtest.Assert(array1.Pad(3, 1).Slice(), []interface{}{0, 1, 1})
|
||||
gtest.Assert(array1.Pad(-4, 1).Slice(), []interface{}{1, 0, 1, 1})
|
||||
gtest.Assert(array1.Pad(3, 1).Slice(), []interface{}{1, 0, 1, 1})
|
||||
t.Assert(array1.Pad(3, 1).Slice(), []interface{}{0, 1, 1})
|
||||
t.Assert(array1.Pad(-4, 1).Slice(), []interface{}{1, 0, 1, 1})
|
||||
t.Assert(array1.Pad(3, 1).Slice(), []interface{}{1, 0, 1, 1})
|
||||
})
|
||||
}
|
||||
|
||||
func TestArray_SubSlice(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []interface{}{0, 1, 2, 3, 4, 5, 6}
|
||||
array1 := garray.NewArrayFrom(a1)
|
||||
array2 := garray.NewArrayFrom(a1, true)
|
||||
gtest.Assert(array1.SubSlice(0, 2), []interface{}{0, 1})
|
||||
gtest.Assert(array1.SubSlice(2, 2), []interface{}{2, 3})
|
||||
gtest.Assert(array1.SubSlice(5, 8), []interface{}{5, 6})
|
||||
gtest.Assert(array1.SubSlice(9, 1), nil)
|
||||
gtest.Assert(array1.SubSlice(-2, 2), []interface{}{5, 6})
|
||||
gtest.Assert(array1.SubSlice(-9, 2), nil)
|
||||
gtest.Assert(array1.SubSlice(1, -2), nil)
|
||||
gtest.Assert(array2.SubSlice(0, 2), []interface{}{0, 1})
|
||||
t.Assert(array1.SubSlice(0, 2), []interface{}{0, 1})
|
||||
t.Assert(array1.SubSlice(2, 2), []interface{}{2, 3})
|
||||
t.Assert(array1.SubSlice(5, 8), []interface{}{5, 6})
|
||||
t.Assert(array1.SubSlice(9, 1), nil)
|
||||
t.Assert(array1.SubSlice(-2, 2), []interface{}{5, 6})
|
||||
t.Assert(array1.SubSlice(-9, 2), nil)
|
||||
t.Assert(array1.SubSlice(1, -2), nil)
|
||||
t.Assert(array2.SubSlice(0, 2), []interface{}{0, 1})
|
||||
})
|
||||
}
|
||||
|
||||
func TestArray_Rand(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []interface{}{0, 1, 2, 3, 4, 5, 6}
|
||||
array1 := garray.NewArrayFrom(a1)
|
||||
gtest.Assert(len(array1.Rands(2)), 2)
|
||||
gtest.Assert(len(array1.Rands(10)), 7)
|
||||
gtest.AssertIN(array1.Rands(1)[0], a1)
|
||||
t.Assert(len(array1.Rands(2)), 2)
|
||||
t.Assert(len(array1.Rands(10)), 7)
|
||||
t.AssertIN(array1.Rands(1)[0], a1)
|
||||
})
|
||||
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s1 := []interface{}{"a", "b", "c", "d"}
|
||||
a1 := garray.NewArrayFrom(s1)
|
||||
i1 := a1.Rand()
|
||||
gtest.Assert(a1.Contains(i1), true)
|
||||
gtest.Assert(a1.Len(), 4)
|
||||
t.Assert(a1.Contains(i1), true)
|
||||
t.Assert(a1.Len(), 4)
|
||||
})
|
||||
}
|
||||
|
||||
func TestArray_Shuffle(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []interface{}{0, 1, 2, 3, 4, 5, 6}
|
||||
array1 := garray.NewArrayFrom(a1)
|
||||
gtest.Assert(array1.Shuffle().Len(), 7)
|
||||
t.Assert(array1.Shuffle().Len(), 7)
|
||||
})
|
||||
}
|
||||
|
||||
func TestArray_Reverse(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []interface{}{0, 1, 2, 3, 4, 5, 6}
|
||||
array1 := garray.NewArrayFrom(a1)
|
||||
gtest.Assert(array1.Reverse().Slice(), []interface{}{6, 5, 4, 3, 2, 1, 0})
|
||||
t.Assert(array1.Reverse().Slice(), []interface{}{6, 5, 4, 3, 2, 1, 0})
|
||||
})
|
||||
}
|
||||
|
||||
func TestArray_Join(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []interface{}{0, 1, 2, 3, 4, 5, 6}
|
||||
array1 := garray.NewArrayFrom(a1)
|
||||
gtest.Assert(array1.Join("."), `0.1.2.3.4.5.6`)
|
||||
t.Assert(array1.Join("."), `0.1.2.3.4.5.6`)
|
||||
})
|
||||
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []interface{}{0, 1, `"a"`, `\a`}
|
||||
array1 := garray.NewArrayFrom(a1)
|
||||
gtest.Assert(array1.Join("."), `0.1."a".\a`)
|
||||
t.Assert(array1.Join("."), `0.1."a".\a`)
|
||||
})
|
||||
}
|
||||
|
||||
func TestArray_String(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []interface{}{0, 1, 2, 3, 4, 5, 6}
|
||||
array1 := garray.NewArrayFrom(a1)
|
||||
gtest.Assert(array1.String(), `[0,1,2,3,4,5,6]`)
|
||||
t.Assert(array1.String(), `[0,1,2,3,4,5,6]`)
|
||||
})
|
||||
}
|
||||
|
||||
func TestArray_Replace(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []interface{}{0, 1, 2, 3, 4, 5, 6}
|
||||
a2 := []interface{}{"a", "b", "c"}
|
||||
a3 := []interface{}{"m", "n", "p", "z", "x", "y", "d", "u"}
|
||||
array1 := garray.NewArrayFrom(a1)
|
||||
array2 := array1.Replace(a2)
|
||||
gtest.Assert(array2.Len(), 7)
|
||||
gtest.Assert(array2.Contains("b"), true)
|
||||
gtest.Assert(array2.Contains(4), true)
|
||||
gtest.Assert(array2.Contains("v"), false)
|
||||
t.Assert(array2.Len(), 7)
|
||||
t.Assert(array2.Contains("b"), true)
|
||||
t.Assert(array2.Contains(4), true)
|
||||
t.Assert(array2.Contains("v"), false)
|
||||
array3 := array1.Replace(a3)
|
||||
gtest.Assert(array3.Len(), 7)
|
||||
gtest.Assert(array3.Contains(4), false)
|
||||
gtest.Assert(array3.Contains("p"), true)
|
||||
gtest.Assert(array3.Contains("u"), false)
|
||||
t.Assert(array3.Len(), 7)
|
||||
t.Assert(array3.Contains(4), false)
|
||||
t.Assert(array3.Contains("p"), true)
|
||||
t.Assert(array3.Contains("u"), false)
|
||||
})
|
||||
}
|
||||
|
||||
func TestArray_SetArray(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []interface{}{0, 1, 2, 3, 4, 5, 6}
|
||||
a2 := []interface{}{"a", "b", "c"}
|
||||
|
||||
array1 := garray.NewArrayFrom(a1)
|
||||
array1 = array1.SetArray(a2)
|
||||
gtest.Assert(array1.Len(), 3)
|
||||
gtest.Assert(array1.Contains("b"), true)
|
||||
gtest.Assert(array1.Contains("5"), false)
|
||||
t.Assert(array1.Len(), 3)
|
||||
t.Assert(array1.Contains("b"), true)
|
||||
t.Assert(array1.Contains("5"), false)
|
||||
})
|
||||
}
|
||||
|
||||
func TestArray_Sum(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []interface{}{0, 1, 2, 3}
|
||||
a2 := []interface{}{"a", "b", "c"}
|
||||
a3 := []interface{}{"a", "1", "2"}
|
||||
@ -346,39 +356,39 @@ func TestArray_Sum(t *testing.T) {
|
||||
array2 := garray.NewArrayFrom(a2)
|
||||
array3 := garray.NewArrayFrom(a3)
|
||||
|
||||
gtest.Assert(array1.Sum(), 6)
|
||||
gtest.Assert(array2.Sum(), 0)
|
||||
gtest.Assert(array3.Sum(), 3)
|
||||
t.Assert(array1.Sum(), 6)
|
||||
t.Assert(array2.Sum(), 0)
|
||||
t.Assert(array3.Sum(), 3)
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
func TestArray_Clone(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []interface{}{0, 1, 2, 3}
|
||||
array1 := garray.NewArrayFrom(a1)
|
||||
array2 := array1.Clone()
|
||||
|
||||
gtest.Assert(array1.Len(), 4)
|
||||
gtest.Assert(array2.Sum(), 6)
|
||||
gtest.AssertEQ(array1, array2)
|
||||
t.Assert(array1.Len(), 4)
|
||||
t.Assert(array2.Sum(), 6)
|
||||
t.AssertEQ(array1, array2)
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
func TestArray_CountValues(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []interface{}{"a", "b", "c", "d", "e", "d"}
|
||||
array1 := garray.NewArrayFrom(a1)
|
||||
array2 := array1.CountValues()
|
||||
gtest.Assert(len(array2), 5)
|
||||
gtest.Assert(array2["b"], 1)
|
||||
gtest.Assert(array2["d"], 2)
|
||||
t.Assert(len(array2), 5)
|
||||
t.Assert(array2["b"], 1)
|
||||
t.Assert(array2["d"], 2)
|
||||
})
|
||||
}
|
||||
|
||||
func TestArray_LockFunc(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s1 := []interface{}{"a", "b", "c", "d"}
|
||||
a1 := garray.NewArrayFrom(s1, true)
|
||||
|
||||
@ -404,13 +414,13 @@ func TestArray_LockFunc(t *testing.T) {
|
||||
<-ch2 //等待go1完成
|
||||
|
||||
// 防止ci抖动,以豪秒为单位
|
||||
gtest.AssertGT(t2-t1, 20) //go1加的读写互斥锁,所go2读的时候被阻塞。
|
||||
gtest.Assert(a1.Contains("g"), true)
|
||||
t.AssertGT(t2-t1, 20) //go1加的读写互斥锁,所go2读的时候被阻塞。
|
||||
t.Assert(a1.Contains("g"), true)
|
||||
})
|
||||
}
|
||||
|
||||
func TestArray_RLockFunc(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s1 := []interface{}{"a", "b", "c", "d"}
|
||||
a1 := garray.NewArrayFrom(s1, true)
|
||||
|
||||
@ -436,32 +446,32 @@ func TestArray_RLockFunc(t *testing.T) {
|
||||
<-ch2 //等待go1完成
|
||||
|
||||
// 防止ci抖动,以豪秒为单位
|
||||
gtest.AssertLT(t2-t1, 20) //go1加的读锁,所go2读的时候,并没有阻塞。
|
||||
gtest.Assert(a1.Contains("g"), true)
|
||||
t.AssertLT(t2-t1, 20) //go1加的读锁,所go2读的时候,并没有阻塞。
|
||||
t.Assert(a1.Contains("g"), true)
|
||||
})
|
||||
}
|
||||
|
||||
func TestArray_Json(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s1 := []interface{}{"a", "b", "d", "c"}
|
||||
a1 := garray.NewArrayFrom(s1)
|
||||
b1, err1 := json.Marshal(a1)
|
||||
b2, err2 := json.Marshal(s1)
|
||||
gtest.Assert(b1, b2)
|
||||
gtest.Assert(err1, err2)
|
||||
t.Assert(b1, b2)
|
||||
t.Assert(err1, err2)
|
||||
|
||||
a2 := garray.New()
|
||||
err2 = json.Unmarshal(b2, &a2)
|
||||
gtest.Assert(err2, nil)
|
||||
gtest.Assert(a2.Slice(), s1)
|
||||
t.Assert(err2, nil)
|
||||
t.Assert(a2.Slice(), s1)
|
||||
|
||||
var a3 garray.Array
|
||||
err := json.Unmarshal(b2, &a3)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(a3.Slice(), s1)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(a3.Slice(), s1)
|
||||
})
|
||||
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type User struct {
|
||||
Name string
|
||||
Scores *garray.Array
|
||||
@ -471,123 +481,123 @@ func TestArray_Json(t *testing.T) {
|
||||
"Scores": []int{99, 100, 98},
|
||||
}
|
||||
b, err := json.Marshal(data)
|
||||
gtest.Assert(err, nil)
|
||||
t.Assert(err, nil)
|
||||
|
||||
user := new(User)
|
||||
err = json.Unmarshal(b, user)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(user.Name, data["Name"])
|
||||
gtest.Assert(user.Scores, data["Scores"])
|
||||
t.Assert(err, nil)
|
||||
t.Assert(user.Name, data["Name"])
|
||||
t.Assert(user.Scores, data["Scores"])
|
||||
})
|
||||
}
|
||||
|
||||
func TestArray_Iterator(t *testing.T) {
|
||||
slice := g.Slice{"a", "b", "d", "c"}
|
||||
array := garray.NewArrayFrom(slice)
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
array.Iterator(func(k int, v interface{}) bool {
|
||||
gtest.Assert(v, slice[k])
|
||||
t.Assert(v, slice[k])
|
||||
return true
|
||||
})
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
array.IteratorAsc(func(k int, v interface{}) bool {
|
||||
gtest.Assert(v, slice[k])
|
||||
t.Assert(v, slice[k])
|
||||
return true
|
||||
})
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
array.IteratorDesc(func(k int, v interface{}) bool {
|
||||
gtest.Assert(v, slice[k])
|
||||
t.Assert(v, slice[k])
|
||||
return true
|
||||
})
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
index := 0
|
||||
array.Iterator(func(k int, v interface{}) bool {
|
||||
index++
|
||||
return false
|
||||
})
|
||||
gtest.Assert(index, 1)
|
||||
t.Assert(index, 1)
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
index := 0
|
||||
array.IteratorAsc(func(k int, v interface{}) bool {
|
||||
index++
|
||||
return false
|
||||
})
|
||||
gtest.Assert(index, 1)
|
||||
t.Assert(index, 1)
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
index := 0
|
||||
array.IteratorDesc(func(k int, v interface{}) bool {
|
||||
index++
|
||||
return false
|
||||
})
|
||||
gtest.Assert(index, 1)
|
||||
t.Assert(index, 1)
|
||||
})
|
||||
}
|
||||
|
||||
func TestArray_RemoveValue(t *testing.T) {
|
||||
slice := g.Slice{"a", "b", "d", "c"}
|
||||
array := garray.NewArrayFrom(slice)
|
||||
gtest.Case(t, func() {
|
||||
gtest.Assert(array.RemoveValue("e"), false)
|
||||
gtest.Assert(array.RemoveValue("b"), true)
|
||||
gtest.Assert(array.RemoveValue("a"), true)
|
||||
gtest.Assert(array.RemoveValue("c"), true)
|
||||
gtest.Assert(array.RemoveValue("f"), false)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
t.Assert(array.RemoveValue("e"), false)
|
||||
t.Assert(array.RemoveValue("b"), true)
|
||||
t.Assert(array.RemoveValue("a"), true)
|
||||
t.Assert(array.RemoveValue("c"), true)
|
||||
t.Assert(array.RemoveValue("f"), false)
|
||||
})
|
||||
}
|
||||
|
||||
func TestArray_UnmarshalValue(t *testing.T) {
|
||||
type T struct {
|
||||
type V struct {
|
||||
Name string
|
||||
Array *garray.Array
|
||||
}
|
||||
// JSON
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var v *V
|
||||
err := gconv.Struct(g.Map{
|
||||
"name": "john",
|
||||
"array": []byte(`[1,2,3]`),
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Array.Slice(), g.Slice{1, 2, 3})
|
||||
}, &v)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(v.Name, "john")
|
||||
t.Assert(v.Array.Slice(), g.Slice{1, 2, 3})
|
||||
})
|
||||
// Map
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var v *V
|
||||
err := gconv.Struct(g.Map{
|
||||
"name": "john",
|
||||
"array": g.Slice{1, 2, 3},
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Array.Slice(), g.Slice{1, 2, 3})
|
||||
}, &v)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(v.Name, "john")
|
||||
t.Assert(v.Array.Slice(), g.Slice{1, 2, 3})
|
||||
})
|
||||
}
|
||||
|
||||
func TestArray_FilterNil(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
values := g.Slice{0, 1, 2, 3, 4, "", g.Slice{}}
|
||||
array := garray.NewArrayFromCopy(values)
|
||||
gtest.Assert(array.FilterNil().Slice(), values)
|
||||
t.Assert(array.FilterNil().Slice(), values)
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
array := garray.NewArrayFromCopy(g.Slice{nil, 1, 2, 3, 4, nil})
|
||||
gtest.Assert(array.FilterNil(), g.Slice{1, 2, 3, 4})
|
||||
t.Assert(array.FilterNil(), g.Slice{1, 2, 3, 4})
|
||||
})
|
||||
}
|
||||
|
||||
func TestArray_FilterEmpty(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
array := garray.NewArrayFrom(g.Slice{0, 1, 2, 3, 4, "", g.Slice{}})
|
||||
gtest.Assert(array.FilterEmpty(), g.Slice{1, 2, 3, 4})
|
||||
t.Assert(array.FilterEmpty(), g.Slice{1, 2, 3, 4})
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
array := garray.NewArrayFrom(g.Slice{1, 2, 3, 4})
|
||||
gtest.Assert(array.FilterEmpty(), g.Slice{1, 2, 3, 4})
|
||||
t.Assert(array.FilterEmpty(), g.Slice{1, 2, 3, 4})
|
||||
})
|
||||
}
|
||||
|
||||
@ -21,37 +21,37 @@ import (
|
||||
)
|
||||
|
||||
func Test_IntArray_Basic(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
expect := []int{0, 1, 2, 3}
|
||||
expect2 := []int{}
|
||||
array := garray.NewIntArrayFrom(expect)
|
||||
array2 := garray.NewIntArrayFrom(expect2)
|
||||
gtest.Assert(array.Slice(), expect)
|
||||
gtest.Assert(array.Interfaces(), expect)
|
||||
t.Assert(array.Slice(), expect)
|
||||
t.Assert(array.Interfaces(), expect)
|
||||
array.Set(0, 100)
|
||||
gtest.Assert(array.Get(0), 100)
|
||||
gtest.Assert(array.Get(1), 1)
|
||||
gtest.Assert(array.Search(100), 0)
|
||||
gtest.Assert(array2.Search(100), -1)
|
||||
gtest.Assert(array.Contains(100), true)
|
||||
gtest.Assert(array.Remove(0), 100)
|
||||
gtest.Assert(array.Remove(-1), 0)
|
||||
gtest.Assert(array.Remove(100000), 0)
|
||||
gtest.Assert(array.Contains(100), false)
|
||||
t.Assert(array.Get(0), 100)
|
||||
t.Assert(array.Get(1), 1)
|
||||
t.Assert(array.Search(100), 0)
|
||||
t.Assert(array2.Search(100), -1)
|
||||
t.Assert(array.Contains(100), true)
|
||||
t.Assert(array.Remove(0), 100)
|
||||
t.Assert(array.Remove(-1), 0)
|
||||
t.Assert(array.Remove(100000), 0)
|
||||
t.Assert(array.Contains(100), false)
|
||||
array.Append(4)
|
||||
gtest.Assert(array.Len(), 4)
|
||||
t.Assert(array.Len(), 4)
|
||||
array.InsertBefore(0, 100)
|
||||
array.InsertAfter(0, 200)
|
||||
gtest.Assert(array.Slice(), []int{100, 200, 1, 2, 3, 4})
|
||||
t.Assert(array.Slice(), []int{100, 200, 1, 2, 3, 4})
|
||||
array.InsertBefore(5, 300)
|
||||
array.InsertAfter(6, 400)
|
||||
gtest.Assert(array.Slice(), []int{100, 200, 1, 2, 3, 300, 4, 400})
|
||||
gtest.Assert(array.Clear().Len(), 0)
|
||||
t.Assert(array.Slice(), []int{100, 200, 1, 2, 3, 300, 4, 400})
|
||||
t.Assert(array.Clear().Len(), 0)
|
||||
})
|
||||
}
|
||||
|
||||
func TestIntArray_Sort(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
expect1 := []int{0, 1, 2, 3}
|
||||
expect2 := []int{3, 2, 1, 0}
|
||||
array := garray.NewIntArray()
|
||||
@ -61,69 +61,79 @@ func TestIntArray_Sort(t *testing.T) {
|
||||
array2.Append(i)
|
||||
}
|
||||
array.Sort()
|
||||
gtest.Assert(array.Slice(), expect1)
|
||||
t.Assert(array.Slice(), expect1)
|
||||
array.Sort(true)
|
||||
gtest.Assert(array.Slice(), expect2)
|
||||
gtest.Assert(array2.Slice(), expect2)
|
||||
t.Assert(array.Slice(), expect2)
|
||||
t.Assert(array2.Slice(), expect2)
|
||||
})
|
||||
}
|
||||
|
||||
func TestIntArray_Unique(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
expect := []int{1, 1, 2, 3}
|
||||
array := garray.NewIntArrayFrom(expect)
|
||||
gtest.Assert(array.Unique().Slice(), []int{1, 2, 3})
|
||||
t.Assert(array.Unique().Slice(), []int{1, 2, 3})
|
||||
})
|
||||
}
|
||||
|
||||
func TestIntArray_PushAndPop(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
expect := []int{0, 1, 2, 3}
|
||||
array := garray.NewIntArrayFrom(expect)
|
||||
gtest.Assert(array.Slice(), expect)
|
||||
gtest.Assert(array.PopLeft(), 0)
|
||||
gtest.Assert(array.PopRight(), 3)
|
||||
gtest.AssertIN(array.PopRand(), []int{1, 2})
|
||||
gtest.AssertIN(array.PopRand(), []int{1, 2})
|
||||
gtest.Assert(array.Len(), 0)
|
||||
t.Assert(array.Slice(), expect)
|
||||
t.Assert(array.PopLeft(), 0)
|
||||
t.Assert(array.PopRight(), 3)
|
||||
t.AssertIN(array.PopRand(), []int{1, 2})
|
||||
t.AssertIN(array.PopRand(), []int{1, 2})
|
||||
t.Assert(array.Len(), 0)
|
||||
array.PushLeft(1).PushRight(2)
|
||||
gtest.Assert(array.Slice(), []int{1, 2})
|
||||
t.Assert(array.Slice(), []int{1, 2})
|
||||
})
|
||||
}
|
||||
|
||||
func TestIntArray_PopLeftsAndPopRights(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
array := garray.NewIntArray()
|
||||
t.Assert(array.PopLeft(), 0)
|
||||
t.Assert(array.PopLefts(10), nil)
|
||||
t.Assert(array.PopRight(), 0)
|
||||
t.Assert(array.PopRights(10), nil)
|
||||
t.Assert(array.PopRand(), 0)
|
||||
t.Assert(array.PopRands(10), nil)
|
||||
})
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
value1 := []int{0, 1, 2, 3, 4, 5, 6}
|
||||
value2 := []int{0, 1, 2, 3, 4, 5, 6}
|
||||
array1 := garray.NewIntArrayFrom(value1)
|
||||
array2 := garray.NewIntArrayFrom(value2)
|
||||
gtest.Assert(array1.PopLefts(2), []int{0, 1})
|
||||
gtest.Assert(array1.Slice(), []int{2, 3, 4, 5, 6})
|
||||
gtest.Assert(array1.PopRights(2), []int{5, 6})
|
||||
gtest.Assert(array1.Slice(), []int{2, 3, 4})
|
||||
gtest.Assert(array1.PopRights(20), []int{2, 3, 4})
|
||||
gtest.Assert(array1.Slice(), []int{})
|
||||
gtest.Assert(array2.PopLefts(20), []int{0, 1, 2, 3, 4, 5, 6})
|
||||
gtest.Assert(array2.Slice(), []int{})
|
||||
t.Assert(array1.PopLefts(2), []int{0, 1})
|
||||
t.Assert(array1.Slice(), []int{2, 3, 4, 5, 6})
|
||||
t.Assert(array1.PopRights(2), []int{5, 6})
|
||||
t.Assert(array1.Slice(), []int{2, 3, 4})
|
||||
t.Assert(array1.PopRights(20), []int{2, 3, 4})
|
||||
t.Assert(array1.Slice(), []int{})
|
||||
t.Assert(array2.PopLefts(20), []int{0, 1, 2, 3, 4, 5, 6})
|
||||
t.Assert(array2.Slice(), []int{})
|
||||
})
|
||||
}
|
||||
|
||||
func TestIntArray_Range(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
value1 := []int{0, 1, 2, 3, 4, 5, 6}
|
||||
array1 := garray.NewIntArrayFrom(value1)
|
||||
array2 := garray.NewIntArrayFrom(value1, true)
|
||||
gtest.Assert(array1.Range(0, 1), []int{0})
|
||||
gtest.Assert(array1.Range(1, 2), []int{1})
|
||||
gtest.Assert(array1.Range(0, 2), []int{0, 1})
|
||||
gtest.Assert(array1.Range(10, 2), nil)
|
||||
gtest.Assert(array1.Range(-1, 10), value1)
|
||||
gtest.Assert(array2.Range(1, 2), []int{1})
|
||||
t.Assert(array1.Range(0, 1), []int{0})
|
||||
t.Assert(array1.Range(1, 2), []int{1})
|
||||
t.Assert(array1.Range(0, 2), []int{0, 1})
|
||||
t.Assert(array1.Range(10, 2), nil)
|
||||
t.Assert(array1.Range(-1, 10), value1)
|
||||
t.Assert(array2.Range(1, 2), []int{1})
|
||||
})
|
||||
}
|
||||
|
||||
func TestIntArray_Merge(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
func1 := func(v1, v2 interface{}) int {
|
||||
if gconv.Int(v1) < gconv.Int(v2) {
|
||||
return 0
|
||||
@ -147,260 +157,260 @@ func TestIntArray_Merge(t *testing.T) {
|
||||
a7 := garray.NewSortedStrArrayFrom(s1)
|
||||
a8 := garray.NewSortedArrayFrom([]interface{}{4, 5}, func1)
|
||||
|
||||
gtest.Assert(a1.Merge(a2).Slice(), []int{0, 1, 2, 3, 4, 5, 6, 7})
|
||||
gtest.Assert(a1.Merge(a3).Len(), 10)
|
||||
gtest.Assert(a1.Merge(a4).Len(), 13)
|
||||
gtest.Assert(a1.Merge(a5).Len(), 15)
|
||||
gtest.Assert(a1.Merge(a6).Len(), 18)
|
||||
gtest.Assert(a1.Merge(a7).Len(), 21)
|
||||
gtest.Assert(a1.Merge(a8).Len(), 23)
|
||||
t.Assert(a1.Merge(a2).Slice(), []int{0, 1, 2, 3, 4, 5, 6, 7})
|
||||
t.Assert(a1.Merge(a3).Len(), 10)
|
||||
t.Assert(a1.Merge(a4).Len(), 13)
|
||||
t.Assert(a1.Merge(a5).Len(), 15)
|
||||
t.Assert(a1.Merge(a6).Len(), 18)
|
||||
t.Assert(a1.Merge(a7).Len(), 21)
|
||||
t.Assert(a1.Merge(a8).Len(), 23)
|
||||
})
|
||||
}
|
||||
|
||||
func TestIntArray_Fill(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []int{0}
|
||||
a2 := []int{0}
|
||||
array1 := garray.NewIntArrayFrom(a1)
|
||||
array2 := garray.NewIntArrayFrom(a2)
|
||||
gtest.Assert(array1.Fill(1, 2, 100).Slice(), []int{0, 100, 100})
|
||||
gtest.Assert(array2.Fill(0, 2, 100).Slice(), []int{100, 100})
|
||||
gtest.Assert(array2.Fill(-1, 2, 100).Slice(), []int{100, 100})
|
||||
t.Assert(array1.Fill(1, 2, 100).Slice(), []int{0, 100, 100})
|
||||
t.Assert(array2.Fill(0, 2, 100).Slice(), []int{100, 100})
|
||||
t.Assert(array2.Fill(-1, 2, 100).Slice(), []int{100, 100})
|
||||
})
|
||||
}
|
||||
|
||||
func TestIntArray_Chunk(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []int{1, 2, 3, 4, 5}
|
||||
array1 := garray.NewIntArrayFrom(a1)
|
||||
chunks := array1.Chunk(2)
|
||||
gtest.Assert(len(chunks), 3)
|
||||
gtest.Assert(chunks[0], []int{1, 2})
|
||||
gtest.Assert(chunks[1], []int{3, 4})
|
||||
gtest.Assert(chunks[2], []int{5})
|
||||
gtest.Assert(array1.Chunk(0), nil)
|
||||
t.Assert(len(chunks), 3)
|
||||
t.Assert(chunks[0], []int{1, 2})
|
||||
t.Assert(chunks[1], []int{3, 4})
|
||||
t.Assert(chunks[2], []int{5})
|
||||
t.Assert(array1.Chunk(0), nil)
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []int{1, 2, 3, 4, 5}
|
||||
array1 := garray.NewIntArrayFrom(a1)
|
||||
chunks := array1.Chunk(3)
|
||||
gtest.Assert(len(chunks), 2)
|
||||
gtest.Assert(chunks[0], []int{1, 2, 3})
|
||||
gtest.Assert(chunks[1], []int{4, 5})
|
||||
gtest.Assert(array1.Chunk(0), nil)
|
||||
t.Assert(len(chunks), 2)
|
||||
t.Assert(chunks[0], []int{1, 2, 3})
|
||||
t.Assert(chunks[1], []int{4, 5})
|
||||
t.Assert(array1.Chunk(0), nil)
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []int{1, 2, 3, 4, 5, 6}
|
||||
array1 := garray.NewIntArrayFrom(a1)
|
||||
chunks := array1.Chunk(2)
|
||||
gtest.Assert(len(chunks), 3)
|
||||
gtest.Assert(chunks[0], []int{1, 2})
|
||||
gtest.Assert(chunks[1], []int{3, 4})
|
||||
gtest.Assert(chunks[2], []int{5, 6})
|
||||
gtest.Assert(array1.Chunk(0), nil)
|
||||
t.Assert(len(chunks), 3)
|
||||
t.Assert(chunks[0], []int{1, 2})
|
||||
t.Assert(chunks[1], []int{3, 4})
|
||||
t.Assert(chunks[2], []int{5, 6})
|
||||
t.Assert(array1.Chunk(0), nil)
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []int{1, 2, 3, 4, 5, 6}
|
||||
array1 := garray.NewIntArrayFrom(a1)
|
||||
chunks := array1.Chunk(3)
|
||||
gtest.Assert(len(chunks), 2)
|
||||
gtest.Assert(chunks[0], []int{1, 2, 3})
|
||||
gtest.Assert(chunks[1], []int{4, 5, 6})
|
||||
gtest.Assert(array1.Chunk(0), nil)
|
||||
t.Assert(len(chunks), 2)
|
||||
t.Assert(chunks[0], []int{1, 2, 3})
|
||||
t.Assert(chunks[1], []int{4, 5, 6})
|
||||
t.Assert(array1.Chunk(0), nil)
|
||||
})
|
||||
}
|
||||
|
||||
func TestIntArray_Pad(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []int{0}
|
||||
array1 := garray.NewIntArrayFrom(a1)
|
||||
gtest.Assert(array1.Pad(3, 1).Slice(), []int{0, 1, 1})
|
||||
gtest.Assert(array1.Pad(-4, 1).Slice(), []int{1, 0, 1, 1})
|
||||
gtest.Assert(array1.Pad(3, 1).Slice(), []int{1, 0, 1, 1})
|
||||
t.Assert(array1.Pad(3, 1).Slice(), []int{0, 1, 1})
|
||||
t.Assert(array1.Pad(-4, 1).Slice(), []int{1, 0, 1, 1})
|
||||
t.Assert(array1.Pad(3, 1).Slice(), []int{1, 0, 1, 1})
|
||||
})
|
||||
}
|
||||
|
||||
func TestIntArray_SubSlice(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []int{0, 1, 2, 3, 4, 5, 6}
|
||||
array1 := garray.NewIntArrayFrom(a1)
|
||||
array2 := garray.NewIntArrayFrom(a1, true)
|
||||
gtest.Assert(array1.SubSlice(6), []int{6})
|
||||
gtest.Assert(array1.SubSlice(5), []int{5, 6})
|
||||
gtest.Assert(array1.SubSlice(8), nil)
|
||||
gtest.Assert(array1.SubSlice(0, 2), []int{0, 1})
|
||||
gtest.Assert(array1.SubSlice(2, 2), []int{2, 3})
|
||||
gtest.Assert(array1.SubSlice(5, 8), []int{5, 6})
|
||||
gtest.Assert(array1.SubSlice(-1, 1), []int{6})
|
||||
gtest.Assert(array1.SubSlice(-1, 9), []int{6})
|
||||
gtest.Assert(array1.SubSlice(-2, 3), []int{5, 6})
|
||||
gtest.Assert(array1.SubSlice(-7, 3), []int{0, 1, 2})
|
||||
gtest.Assert(array1.SubSlice(-8, 3), nil)
|
||||
gtest.Assert(array1.SubSlice(-1, -3), []int{3, 4, 5})
|
||||
gtest.Assert(array1.SubSlice(-9, 3), nil)
|
||||
gtest.Assert(array1.SubSlice(1, -1), []int{0})
|
||||
gtest.Assert(array1.SubSlice(1, -3), nil)
|
||||
gtest.Assert(array2.SubSlice(0, 2), []int{0, 1})
|
||||
t.Assert(array1.SubSlice(6), []int{6})
|
||||
t.Assert(array1.SubSlice(5), []int{5, 6})
|
||||
t.Assert(array1.SubSlice(8), nil)
|
||||
t.Assert(array1.SubSlice(0, 2), []int{0, 1})
|
||||
t.Assert(array1.SubSlice(2, 2), []int{2, 3})
|
||||
t.Assert(array1.SubSlice(5, 8), []int{5, 6})
|
||||
t.Assert(array1.SubSlice(-1, 1), []int{6})
|
||||
t.Assert(array1.SubSlice(-1, 9), []int{6})
|
||||
t.Assert(array1.SubSlice(-2, 3), []int{5, 6})
|
||||
t.Assert(array1.SubSlice(-7, 3), []int{0, 1, 2})
|
||||
t.Assert(array1.SubSlice(-8, 3), nil)
|
||||
t.Assert(array1.SubSlice(-1, -3), []int{3, 4, 5})
|
||||
t.Assert(array1.SubSlice(-9, 3), nil)
|
||||
t.Assert(array1.SubSlice(1, -1), []int{0})
|
||||
t.Assert(array1.SubSlice(1, -3), nil)
|
||||
t.Assert(array2.SubSlice(0, 2), []int{0, 1})
|
||||
})
|
||||
}
|
||||
|
||||
func TestIntArray_Rand(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []int{0, 1, 2, 3, 4, 5, 6}
|
||||
array1 := garray.NewIntArrayFrom(a1)
|
||||
gtest.Assert(len(array1.Rands(2)), 2)
|
||||
gtest.Assert(len(array1.Rands(10)), 7)
|
||||
gtest.AssertIN(array1.Rands(1)[0], a1)
|
||||
gtest.AssertIN(array1.Rand(), a1)
|
||||
t.Assert(len(array1.Rands(2)), 2)
|
||||
t.Assert(len(array1.Rands(10)), 7)
|
||||
t.AssertIN(array1.Rands(1)[0], a1)
|
||||
t.AssertIN(array1.Rand(), a1)
|
||||
})
|
||||
}
|
||||
|
||||
func TestIntArray_PopRands(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []int{100, 200, 300, 400, 500, 600}
|
||||
array := garray.NewIntArrayFrom(a1)
|
||||
ns1 := array.PopRands(2)
|
||||
gtest.AssertIN(ns1, []int{100, 200, 300, 400, 500, 600})
|
||||
gtest.Assert(len(ns1), 2)
|
||||
t.AssertIN(ns1, []int{100, 200, 300, 400, 500, 600})
|
||||
t.Assert(len(ns1), 2)
|
||||
|
||||
ns2 := array.PopRands(7)
|
||||
gtest.Assert(len(ns2), 4)
|
||||
gtest.AssertIN(ns2, []int{100, 200, 300, 400, 500, 600})
|
||||
t.Assert(len(ns2), 4)
|
||||
t.AssertIN(ns2, []int{100, 200, 300, 400, 500, 600})
|
||||
})
|
||||
}
|
||||
|
||||
func TestIntArray_Shuffle(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []int{0, 1, 2, 3, 4, 5, 6}
|
||||
array1 := garray.NewIntArrayFrom(a1)
|
||||
gtest.Assert(array1.Shuffle().Len(), 7)
|
||||
t.Assert(array1.Shuffle().Len(), 7)
|
||||
})
|
||||
}
|
||||
|
||||
func TestIntArray_Reverse(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []int{0, 1, 2, 3, 4, 5, 6}
|
||||
array1 := garray.NewIntArrayFrom(a1)
|
||||
gtest.Assert(array1.Reverse().Slice(), []int{6, 5, 4, 3, 2, 1, 0})
|
||||
t.Assert(array1.Reverse().Slice(), []int{6, 5, 4, 3, 2, 1, 0})
|
||||
})
|
||||
}
|
||||
|
||||
func TestIntArray_Join(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []int{0, 1, 2, 3, 4, 5, 6}
|
||||
array1 := garray.NewIntArrayFrom(a1)
|
||||
gtest.Assert(array1.Join("."), "0.1.2.3.4.5.6")
|
||||
t.Assert(array1.Join("."), "0.1.2.3.4.5.6")
|
||||
})
|
||||
}
|
||||
|
||||
func TestIntArray_String(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []int{0, 1, 2, 3, 4, 5, 6}
|
||||
array1 := garray.NewIntArrayFrom(a1)
|
||||
gtest.Assert(array1.String(), "[0,1,2,3,4,5,6]")
|
||||
t.Assert(array1.String(), "[0,1,2,3,4,5,6]")
|
||||
})
|
||||
}
|
||||
|
||||
func TestIntArray_SetArray(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []int{1, 2, 3, 5}
|
||||
a2 := []int{6, 7}
|
||||
array1 := garray.NewIntArrayFrom(a1)
|
||||
array1.SetArray(a2)
|
||||
gtest.Assert(array1.Len(), 2)
|
||||
gtest.Assert(array1, []int{6, 7})
|
||||
t.Assert(array1.Len(), 2)
|
||||
t.Assert(array1, []int{6, 7})
|
||||
})
|
||||
}
|
||||
|
||||
func TestIntArray_Replace(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []int{1, 2, 3, 5}
|
||||
a2 := []int{6, 7}
|
||||
a3 := []int{9, 10, 11, 12, 13}
|
||||
array1 := garray.NewIntArrayFrom(a1)
|
||||
array1.Replace(a2)
|
||||
gtest.Assert(array1, []int{6, 7, 3, 5})
|
||||
t.Assert(array1, []int{6, 7, 3, 5})
|
||||
|
||||
array1.Replace(a3)
|
||||
gtest.Assert(array1, []int{9, 10, 11, 12})
|
||||
t.Assert(array1, []int{9, 10, 11, 12})
|
||||
})
|
||||
}
|
||||
|
||||
func TestIntArray_Clear(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []int{1, 2, 3, 5}
|
||||
array1 := garray.NewIntArrayFrom(a1)
|
||||
array1.Clear()
|
||||
gtest.Assert(array1.Len(), 0)
|
||||
t.Assert(array1.Len(), 0)
|
||||
})
|
||||
}
|
||||
|
||||
func TestIntArray_Clone(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []int{1, 2, 3, 5}
|
||||
array1 := garray.NewIntArrayFrom(a1)
|
||||
array2 := array1.Clone()
|
||||
gtest.Assert(array1, array2)
|
||||
t.Assert(array1, array2)
|
||||
})
|
||||
}
|
||||
|
||||
func TestArray_Get(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []int{1, 2, 3, 5}
|
||||
array1 := garray.NewIntArrayFrom(a1)
|
||||
gtest.Assert(array1.Get(2), 3)
|
||||
gtest.Assert(array1.Len(), 4)
|
||||
t.Assert(array1.Get(2), 3)
|
||||
t.Assert(array1.Len(), 4)
|
||||
})
|
||||
}
|
||||
|
||||
func TestIntArray_Sum(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []int{1, 2, 3, 5}
|
||||
array1 := garray.NewIntArrayFrom(a1)
|
||||
gtest.Assert(array1.Sum(), 11)
|
||||
t.Assert(array1.Sum(), 11)
|
||||
})
|
||||
}
|
||||
|
||||
func TestIntArray_CountValues(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []int{1, 2, 3, 5, 3}
|
||||
array1 := garray.NewIntArrayFrom(a1)
|
||||
m1 := array1.CountValues()
|
||||
gtest.Assert(len(m1), 4)
|
||||
gtest.Assert(m1[1], 1)
|
||||
gtest.Assert(m1[3], 2)
|
||||
t.Assert(len(m1), 4)
|
||||
t.Assert(m1[1], 1)
|
||||
t.Assert(m1[3], 2)
|
||||
})
|
||||
}
|
||||
|
||||
func TestNewIntArrayFromCopy(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []int{1, 2, 3, 5, 3}
|
||||
array1 := garray.NewIntArrayFromCopy(a1)
|
||||
gtest.Assert(array1.Len(), 5)
|
||||
gtest.Assert(array1, a1)
|
||||
t.Assert(array1.Len(), 5)
|
||||
t.Assert(array1, a1)
|
||||
})
|
||||
}
|
||||
|
||||
func TestIntArray_Remove(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []int{1, 2, 3, 5, 4}
|
||||
array1 := garray.NewIntArrayFrom(a1)
|
||||
n1 := array1.Remove(1)
|
||||
gtest.Assert(n1, 2)
|
||||
gtest.Assert(array1.Len(), 4)
|
||||
t.Assert(n1, 2)
|
||||
t.Assert(array1.Len(), 4)
|
||||
|
||||
n1 = array1.Remove(0)
|
||||
gtest.Assert(n1, 1)
|
||||
gtest.Assert(array1.Len(), 3)
|
||||
t.Assert(n1, 1)
|
||||
t.Assert(array1.Len(), 3)
|
||||
|
||||
n1 = array1.Remove(2)
|
||||
gtest.Assert(n1, 4)
|
||||
gtest.Assert(array1.Len(), 2)
|
||||
t.Assert(n1, 4)
|
||||
t.Assert(array1.Len(), 2)
|
||||
})
|
||||
}
|
||||
|
||||
func TestIntArray_LockFunc(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s1 := []int{1, 2, 3, 4}
|
||||
a1 := garray.NewIntArrayFrom(s1, true)
|
||||
|
||||
@ -426,26 +436,26 @@ func TestIntArray_LockFunc(t *testing.T) {
|
||||
<-ch2 //等待go1完成
|
||||
|
||||
// 防止ci抖动,以豪秒为单位
|
||||
gtest.AssertGT(t2-t1, 20) //go1加的读写互斥锁,所go2读的时候被阻塞。
|
||||
gtest.Assert(a1.Contains(6), true)
|
||||
t.AssertGT(t2-t1, 20) //go1加的读写互斥锁,所go2读的时候被阻塞。
|
||||
t.Assert(a1.Contains(6), true)
|
||||
})
|
||||
}
|
||||
|
||||
func TestIntArray_SortFunc(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s1 := []int{1, 4, 3, 2}
|
||||
a1 := garray.NewIntArrayFrom(s1)
|
||||
func1 := func(v1, v2 int) bool {
|
||||
return v1 < v2
|
||||
}
|
||||
a11 := a1.SortFunc(func1)
|
||||
gtest.Assert(a11, []int{1, 2, 3, 4})
|
||||
t.Assert(a11, []int{1, 2, 3, 4})
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
func TestIntArray_RLockFunc(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s1 := []int{1, 2, 3, 4}
|
||||
a1 := garray.NewIntArrayFrom(s1, true)
|
||||
|
||||
@ -471,31 +481,31 @@ func TestIntArray_RLockFunc(t *testing.T) {
|
||||
<-ch2 //等待go1完成
|
||||
|
||||
// 防止ci抖动,以豪秒为单位
|
||||
gtest.AssertLT(t2-t1, 20) //go1加的读锁,所go2读的时候,并没有阻塞。
|
||||
gtest.Assert(a1.Contains(6), true)
|
||||
t.AssertLT(t2-t1, 20) //go1加的读锁,所go2读的时候,并没有阻塞。
|
||||
t.Assert(a1.Contains(6), true)
|
||||
})
|
||||
}
|
||||
|
||||
func TestIntArray_Json(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s1 := []int{1, 4, 3, 2}
|
||||
a1 := garray.NewIntArrayFrom(s1)
|
||||
b1, err1 := json.Marshal(a1)
|
||||
b2, err2 := json.Marshal(s1)
|
||||
gtest.Assert(b1, b2)
|
||||
gtest.Assert(err1, err2)
|
||||
t.Assert(b1, b2)
|
||||
t.Assert(err1, err2)
|
||||
|
||||
a2 := garray.NewIntArray()
|
||||
err1 = json.Unmarshal(b2, &a2)
|
||||
gtest.Assert(a2.Slice(), s1)
|
||||
t.Assert(a2.Slice(), s1)
|
||||
|
||||
var a3 garray.IntArray
|
||||
err := json.Unmarshal(b2, &a3)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(a3.Slice(), s1)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(a3.Slice(), s1)
|
||||
})
|
||||
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type User struct {
|
||||
Name string
|
||||
Scores *garray.IntArray
|
||||
@ -505,112 +515,112 @@ func TestIntArray_Json(t *testing.T) {
|
||||
"Scores": []int{99, 100, 98},
|
||||
}
|
||||
b, err := json.Marshal(data)
|
||||
gtest.Assert(err, nil)
|
||||
t.Assert(err, nil)
|
||||
|
||||
user := new(User)
|
||||
err = json.Unmarshal(b, user)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(user.Name, data["Name"])
|
||||
gtest.Assert(user.Scores, data["Scores"])
|
||||
t.Assert(err, nil)
|
||||
t.Assert(user.Name, data["Name"])
|
||||
t.Assert(user.Scores, data["Scores"])
|
||||
})
|
||||
}
|
||||
|
||||
func TestIntArray_Iterator(t *testing.T) {
|
||||
slice := g.SliceInt{10, 20, 30, 40}
|
||||
array := garray.NewIntArrayFrom(slice)
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
array.Iterator(func(k int, v int) bool {
|
||||
gtest.Assert(v, slice[k])
|
||||
t.Assert(v, slice[k])
|
||||
return true
|
||||
})
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
array.IteratorAsc(func(k int, v int) bool {
|
||||
gtest.Assert(v, slice[k])
|
||||
t.Assert(v, slice[k])
|
||||
return true
|
||||
})
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
array.IteratorDesc(func(k int, v int) bool {
|
||||
gtest.Assert(v, slice[k])
|
||||
t.Assert(v, slice[k])
|
||||
return true
|
||||
})
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
index := 0
|
||||
array.Iterator(func(k int, v int) bool {
|
||||
index++
|
||||
return false
|
||||
})
|
||||
gtest.Assert(index, 1)
|
||||
t.Assert(index, 1)
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
index := 0
|
||||
array.IteratorAsc(func(k int, v int) bool {
|
||||
index++
|
||||
return false
|
||||
})
|
||||
gtest.Assert(index, 1)
|
||||
t.Assert(index, 1)
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
index := 0
|
||||
array.IteratorDesc(func(k int, v int) bool {
|
||||
index++
|
||||
return false
|
||||
})
|
||||
gtest.Assert(index, 1)
|
||||
t.Assert(index, 1)
|
||||
})
|
||||
}
|
||||
|
||||
func TestIntArray_RemoveValue(t *testing.T) {
|
||||
slice := g.SliceInt{10, 20, 30, 40}
|
||||
array := garray.NewIntArrayFrom(slice)
|
||||
gtest.Case(t, func() {
|
||||
gtest.Assert(array.RemoveValue(99), false)
|
||||
gtest.Assert(array.RemoveValue(20), true)
|
||||
gtest.Assert(array.RemoveValue(10), true)
|
||||
gtest.Assert(array.RemoveValue(20), false)
|
||||
gtest.Assert(array.RemoveValue(88), false)
|
||||
gtest.Assert(array.Len(), 2)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
t.Assert(array.RemoveValue(99), false)
|
||||
t.Assert(array.RemoveValue(20), true)
|
||||
t.Assert(array.RemoveValue(10), true)
|
||||
t.Assert(array.RemoveValue(20), false)
|
||||
t.Assert(array.RemoveValue(88), false)
|
||||
t.Assert(array.Len(), 2)
|
||||
})
|
||||
}
|
||||
|
||||
func TestIntArray_UnmarshalValue(t *testing.T) {
|
||||
type T struct {
|
||||
type V struct {
|
||||
Name string
|
||||
Array *garray.IntArray
|
||||
}
|
||||
// JSON
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var v *V
|
||||
err := gconv.Struct(g.Map{
|
||||
"name": "john",
|
||||
"array": []byte(`[1,2,3]`),
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Array.Slice(), g.Slice{1, 2, 3})
|
||||
}, &v)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(v.Name, "john")
|
||||
t.Assert(v.Array.Slice(), g.Slice{1, 2, 3})
|
||||
})
|
||||
// Map
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var v *V
|
||||
err := gconv.Struct(g.Map{
|
||||
"name": "john",
|
||||
"array": g.Slice{1, 2, 3},
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Array.Slice(), g.Slice{1, 2, 3})
|
||||
}, &v)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(v.Name, "john")
|
||||
t.Assert(v.Array.Slice(), g.Slice{1, 2, 3})
|
||||
})
|
||||
}
|
||||
|
||||
func TestIntArray_FilterEmpty(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
array := garray.NewIntArrayFrom(g.SliceInt{0, 1, 2, 3, 4, 0})
|
||||
gtest.Assert(array.FilterEmpty(), g.SliceInt{1, 2, 3, 4})
|
||||
t.Assert(array.FilterEmpty(), g.SliceInt{1, 2, 3, 4})
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
array := garray.NewIntArrayFrom(g.SliceInt{1, 2, 3, 4})
|
||||
gtest.Assert(array.FilterEmpty(), g.SliceInt{1, 2, 3, 4})
|
||||
t.Assert(array.FilterEmpty(), g.SliceInt{1, 2, 3, 4})
|
||||
})
|
||||
}
|
||||
|
||||
@ -21,38 +21,38 @@ import (
|
||||
)
|
||||
|
||||
func Test_StrArray_Basic(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
expect := []string{"0", "1", "2", "3"}
|
||||
array := garray.NewStrArrayFrom(expect)
|
||||
array2 := garray.NewStrArrayFrom(expect, true)
|
||||
array3 := garray.NewStrArrayFrom([]string{})
|
||||
gtest.Assert(array.Slice(), expect)
|
||||
gtest.Assert(array.Interfaces(), expect)
|
||||
t.Assert(array.Slice(), expect)
|
||||
t.Assert(array.Interfaces(), expect)
|
||||
array.Set(0, "100")
|
||||
gtest.Assert(array.Get(0), 100)
|
||||
gtest.Assert(array.Get(1), 1)
|
||||
gtest.Assert(array.Search("100"), 0)
|
||||
gtest.Assert(array.Contains("100"), true)
|
||||
gtest.Assert(array.Remove(0), 100)
|
||||
gtest.Assert(array.Remove(-1), "")
|
||||
gtest.Assert(array.Remove(100000), "")
|
||||
gtest.Assert(array.Contains("100"), false)
|
||||
t.Assert(array.Get(0), 100)
|
||||
t.Assert(array.Get(1), 1)
|
||||
t.Assert(array.Search("100"), 0)
|
||||
t.Assert(array.Contains("100"), true)
|
||||
t.Assert(array.Remove(0), 100)
|
||||
t.Assert(array.Remove(-1), "")
|
||||
t.Assert(array.Remove(100000), "")
|
||||
t.Assert(array.Contains("100"), false)
|
||||
array.Append("4")
|
||||
gtest.Assert(array.Len(), 4)
|
||||
t.Assert(array.Len(), 4)
|
||||
array.InsertBefore(0, "100")
|
||||
array.InsertAfter(0, "200")
|
||||
gtest.Assert(array.Slice(), []string{"100", "200", "1", "2", "3", "4"})
|
||||
t.Assert(array.Slice(), []string{"100", "200", "1", "2", "3", "4"})
|
||||
array.InsertBefore(5, "300")
|
||||
array.InsertAfter(6, "400")
|
||||
gtest.Assert(array.Slice(), []string{"100", "200", "1", "2", "3", "300", "4", "400"})
|
||||
gtest.Assert(array.Clear().Len(), 0)
|
||||
gtest.Assert(array2.Slice(), expect)
|
||||
gtest.Assert(array3.Search("100"), -1)
|
||||
t.Assert(array.Slice(), []string{"100", "200", "1", "2", "3", "300", "4", "400"})
|
||||
t.Assert(array.Clear().Len(), 0)
|
||||
t.Assert(array2.Slice(), expect)
|
||||
t.Assert(array3.Search("100"), -1)
|
||||
})
|
||||
}
|
||||
|
||||
func TestStrArray_Sort(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
expect1 := []string{"0", "1", "2", "3"}
|
||||
expect2 := []string{"3", "2", "1", "0"}
|
||||
array := garray.NewStrArray()
|
||||
@ -60,73 +60,83 @@ func TestStrArray_Sort(t *testing.T) {
|
||||
array.Append(gconv.String(i))
|
||||
}
|
||||
array.Sort()
|
||||
gtest.Assert(array.Slice(), expect1)
|
||||
t.Assert(array.Slice(), expect1)
|
||||
array.Sort(true)
|
||||
gtest.Assert(array.Slice(), expect2)
|
||||
t.Assert(array.Slice(), expect2)
|
||||
})
|
||||
}
|
||||
|
||||
func TestStrArray_Unique(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
expect := []string{"1", "1", "2", "3"}
|
||||
array := garray.NewStrArrayFrom(expect)
|
||||
gtest.Assert(array.Unique().Slice(), []string{"1", "2", "3"})
|
||||
t.Assert(array.Unique().Slice(), []string{"1", "2", "3"})
|
||||
})
|
||||
}
|
||||
|
||||
func TestStrArray_PushAndPop(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
expect := []string{"0", "1", "2", "3"}
|
||||
array := garray.NewStrArrayFrom(expect)
|
||||
gtest.Assert(array.Slice(), expect)
|
||||
gtest.Assert(array.PopLeft(), "0")
|
||||
gtest.Assert(array.PopRight(), "3")
|
||||
gtest.AssertIN(array.PopRand(), []string{"1", "2"})
|
||||
gtest.AssertIN(array.PopRand(), []string{"1", "2"})
|
||||
gtest.Assert(array.Len(), 0)
|
||||
t.Assert(array.Slice(), expect)
|
||||
t.Assert(array.PopLeft(), "0")
|
||||
t.Assert(array.PopRight(), "3")
|
||||
t.AssertIN(array.PopRand(), []string{"1", "2"})
|
||||
t.AssertIN(array.PopRand(), []string{"1", "2"})
|
||||
t.Assert(array.Len(), 0)
|
||||
array.PushLeft("1").PushRight("2")
|
||||
gtest.Assert(array.Slice(), []string{"1", "2"})
|
||||
t.Assert(array.Slice(), []string{"1", "2"})
|
||||
})
|
||||
}
|
||||
|
||||
func TestStrArray_PopLeftsAndPopRights(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
array := garray.NewStrArray()
|
||||
t.Assert(array.PopLeft(), nil)
|
||||
t.Assert(array.PopLefts(10), nil)
|
||||
t.Assert(array.PopRight(), nil)
|
||||
t.Assert(array.PopRights(10), nil)
|
||||
t.Assert(array.PopRand(), nil)
|
||||
t.Assert(array.PopRands(10), nil)
|
||||
})
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
value1 := []string{"0", "1", "2", "3", "4", "5", "6"}
|
||||
value2 := []string{"0", "1", "2", "3", "4", "5", "6"}
|
||||
array1 := garray.NewStrArrayFrom(value1)
|
||||
array2 := garray.NewStrArrayFrom(value2)
|
||||
gtest.Assert(array1.PopLefts(2), []interface{}{"0", "1"})
|
||||
gtest.Assert(array1.Slice(), []interface{}{"2", "3", "4", "5", "6"})
|
||||
gtest.Assert(array1.PopRights(2), []interface{}{"5", "6"})
|
||||
gtest.Assert(array1.Slice(), []interface{}{"2", "3", "4"})
|
||||
gtest.Assert(array1.PopRights(20), []interface{}{"2", "3", "4"})
|
||||
gtest.Assert(array1.Slice(), []interface{}{})
|
||||
gtest.Assert(array2.PopLefts(20), []interface{}{"0", "1", "2", "3", "4", "5", "6"})
|
||||
gtest.Assert(array2.Slice(), []interface{}{})
|
||||
t.Assert(array1.PopLefts(2), []interface{}{"0", "1"})
|
||||
t.Assert(array1.Slice(), []interface{}{"2", "3", "4", "5", "6"})
|
||||
t.Assert(array1.PopRights(2), []interface{}{"5", "6"})
|
||||
t.Assert(array1.Slice(), []interface{}{"2", "3", "4"})
|
||||
t.Assert(array1.PopRights(20), []interface{}{"2", "3", "4"})
|
||||
t.Assert(array1.Slice(), []interface{}{})
|
||||
t.Assert(array2.PopLefts(20), []interface{}{"0", "1", "2", "3", "4", "5", "6"})
|
||||
t.Assert(array2.Slice(), []interface{}{})
|
||||
})
|
||||
}
|
||||
|
||||
func TestString_Range(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
value1 := []string{"0", "1", "2", "3", "4", "5", "6"}
|
||||
array1 := garray.NewStrArrayFrom(value1)
|
||||
array2 := garray.NewStrArrayFrom(value1, true)
|
||||
gtest.Assert(array1.Range(0, 1), []interface{}{"0"})
|
||||
gtest.Assert(array1.Range(1, 2), []interface{}{"1"})
|
||||
gtest.Assert(array1.Range(0, 2), []interface{}{"0", "1"})
|
||||
gtest.Assert(array1.Range(-1, 10), value1)
|
||||
gtest.Assert(array1.Range(10, 1), nil)
|
||||
gtest.Assert(array2.Range(0, 1), []interface{}{"0"})
|
||||
t.Assert(array1.Range(0, 1), []interface{}{"0"})
|
||||
t.Assert(array1.Range(1, 2), []interface{}{"1"})
|
||||
t.Assert(array1.Range(0, 2), []interface{}{"0", "1"})
|
||||
t.Assert(array1.Range(-1, 10), value1)
|
||||
t.Assert(array1.Range(10, 1), nil)
|
||||
t.Assert(array2.Range(0, 1), []interface{}{"0"})
|
||||
})
|
||||
}
|
||||
|
||||
func TestStrArray_Merge(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a11 := []string{"0", "1", "2", "3"}
|
||||
a21 := []string{"4", "5", "6", "7"}
|
||||
array1 := garray.NewStrArrayFrom(a11)
|
||||
array2 := garray.NewStrArrayFrom(a21)
|
||||
gtest.Assert(array1.Merge(array2).Slice(), []string{"0", "1", "2", "3", "4", "5", "6", "7"})
|
||||
t.Assert(array1.Merge(array2).Slice(), []string{"0", "1", "2", "3", "4", "5", "6", "7"})
|
||||
|
||||
func1 := func(v1, v2 interface{}) int {
|
||||
if gconv.Int(v1) < gconv.Int(v2) {
|
||||
@ -145,267 +155,267 @@ func TestStrArray_Merge(t *testing.T) {
|
||||
s6 := garray.NewSortedIntArrayFrom([]int{1, 2, 3})
|
||||
a1 := garray.NewStrArrayFrom(s1)
|
||||
|
||||
gtest.Assert(a1.Merge(s2).Len(), 6)
|
||||
gtest.Assert(a1.Merge(i1).Len(), 9)
|
||||
gtest.Assert(a1.Merge(i2).Len(), 10)
|
||||
gtest.Assert(a1.Merge(s3).Len(), 12)
|
||||
gtest.Assert(a1.Merge(s4).Len(), 14)
|
||||
gtest.Assert(a1.Merge(s5).Len(), 16)
|
||||
gtest.Assert(a1.Merge(s6).Len(), 19)
|
||||
t.Assert(a1.Merge(s2).Len(), 6)
|
||||
t.Assert(a1.Merge(i1).Len(), 9)
|
||||
t.Assert(a1.Merge(i2).Len(), 10)
|
||||
t.Assert(a1.Merge(s3).Len(), 12)
|
||||
t.Assert(a1.Merge(s4).Len(), 14)
|
||||
t.Assert(a1.Merge(s5).Len(), 16)
|
||||
t.Assert(a1.Merge(s6).Len(), 19)
|
||||
})
|
||||
}
|
||||
|
||||
func TestStrArray_Fill(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []string{"0"}
|
||||
a2 := []string{"0"}
|
||||
array1 := garray.NewStrArrayFrom(a1)
|
||||
array2 := garray.NewStrArrayFrom(a2)
|
||||
gtest.Assert(array1.Fill(1, 2, "100").Slice(), []string{"0", "100", "100"})
|
||||
gtest.Assert(array2.Fill(0, 2, "100").Slice(), []string{"100", "100"})
|
||||
t.Assert(array1.Fill(1, 2, "100").Slice(), []string{"0", "100", "100"})
|
||||
t.Assert(array2.Fill(0, 2, "100").Slice(), []string{"100", "100"})
|
||||
s1 := array2.Fill(-1, 2, "100")
|
||||
gtest.Assert(s1.Len(), 2)
|
||||
t.Assert(s1.Len(), 2)
|
||||
})
|
||||
}
|
||||
|
||||
func TestStrArray_Chunk(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []string{"1", "2", "3", "4", "5"}
|
||||
array1 := garray.NewStrArrayFrom(a1)
|
||||
chunks := array1.Chunk(2)
|
||||
gtest.Assert(len(chunks), 3)
|
||||
gtest.Assert(chunks[0], []string{"1", "2"})
|
||||
gtest.Assert(chunks[1], []string{"3", "4"})
|
||||
gtest.Assert(chunks[2], []string{"5"})
|
||||
gtest.Assert(len(array1.Chunk(0)), 0)
|
||||
t.Assert(len(chunks), 3)
|
||||
t.Assert(chunks[0], []string{"1", "2"})
|
||||
t.Assert(chunks[1], []string{"3", "4"})
|
||||
t.Assert(chunks[2], []string{"5"})
|
||||
t.Assert(len(array1.Chunk(0)), 0)
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []string{"1", "2", "3", "4", "5"}
|
||||
array1 := garray.NewStrArrayFrom(a1)
|
||||
chunks := array1.Chunk(3)
|
||||
gtest.Assert(len(chunks), 2)
|
||||
gtest.Assert(chunks[0], []string{"1", "2", "3"})
|
||||
gtest.Assert(chunks[1], []string{"4", "5"})
|
||||
gtest.Assert(array1.Chunk(0), nil)
|
||||
t.Assert(len(chunks), 2)
|
||||
t.Assert(chunks[0], []string{"1", "2", "3"})
|
||||
t.Assert(chunks[1], []string{"4", "5"})
|
||||
t.Assert(array1.Chunk(0), nil)
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []string{"1", "2", "3", "4", "5", "6"}
|
||||
array1 := garray.NewStrArrayFrom(a1)
|
||||
chunks := array1.Chunk(2)
|
||||
gtest.Assert(len(chunks), 3)
|
||||
gtest.Assert(chunks[0], []string{"1", "2"})
|
||||
gtest.Assert(chunks[1], []string{"3", "4"})
|
||||
gtest.Assert(chunks[2], []string{"5", "6"})
|
||||
gtest.Assert(array1.Chunk(0), nil)
|
||||
t.Assert(len(chunks), 3)
|
||||
t.Assert(chunks[0], []string{"1", "2"})
|
||||
t.Assert(chunks[1], []string{"3", "4"})
|
||||
t.Assert(chunks[2], []string{"5", "6"})
|
||||
t.Assert(array1.Chunk(0), nil)
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []string{"1", "2", "3", "4", "5", "6"}
|
||||
array1 := garray.NewStrArrayFrom(a1)
|
||||
chunks := array1.Chunk(3)
|
||||
gtest.Assert(len(chunks), 2)
|
||||
gtest.Assert(chunks[0], []string{"1", "2", "3"})
|
||||
gtest.Assert(chunks[1], []string{"4", "5", "6"})
|
||||
gtest.Assert(array1.Chunk(0), nil)
|
||||
t.Assert(len(chunks), 2)
|
||||
t.Assert(chunks[0], []string{"1", "2", "3"})
|
||||
t.Assert(chunks[1], []string{"4", "5", "6"})
|
||||
t.Assert(array1.Chunk(0), nil)
|
||||
})
|
||||
}
|
||||
|
||||
func TestStrArray_Pad(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []string{"0"}
|
||||
array1 := garray.NewStrArrayFrom(a1)
|
||||
gtest.Assert(array1.Pad(3, "1").Slice(), []string{"0", "1", "1"})
|
||||
gtest.Assert(array1.Pad(-4, "1").Slice(), []string{"1", "0", "1", "1"})
|
||||
gtest.Assert(array1.Pad(3, "1").Slice(), []string{"1", "0", "1", "1"})
|
||||
t.Assert(array1.Pad(3, "1").Slice(), []string{"0", "1", "1"})
|
||||
t.Assert(array1.Pad(-4, "1").Slice(), []string{"1", "0", "1", "1"})
|
||||
t.Assert(array1.Pad(3, "1").Slice(), []string{"1", "0", "1", "1"})
|
||||
})
|
||||
}
|
||||
|
||||
func TestStrArray_SubSlice(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []string{"0", "1", "2", "3", "4", "5", "6"}
|
||||
array1 := garray.NewStrArrayFrom(a1)
|
||||
array2 := garray.NewStrArrayFrom(a1, true)
|
||||
gtest.Assert(array1.SubSlice(0, 2), []string{"0", "1"})
|
||||
gtest.Assert(array1.SubSlice(2, 2), []string{"2", "3"})
|
||||
gtest.Assert(array1.SubSlice(5, 8), []string{"5", "6"})
|
||||
gtest.Assert(array1.SubSlice(8, 2), nil)
|
||||
gtest.Assert(array1.SubSlice(1, -2), nil)
|
||||
gtest.Assert(array1.SubSlice(-5, 2), []string{"2", "3"})
|
||||
gtest.Assert(array1.SubSlice(-10, 1), nil)
|
||||
gtest.Assert(array2.SubSlice(0, 2), []string{"0", "1"})
|
||||
t.Assert(array1.SubSlice(0, 2), []string{"0", "1"})
|
||||
t.Assert(array1.SubSlice(2, 2), []string{"2", "3"})
|
||||
t.Assert(array1.SubSlice(5, 8), []string{"5", "6"})
|
||||
t.Assert(array1.SubSlice(8, 2), nil)
|
||||
t.Assert(array1.SubSlice(1, -2), nil)
|
||||
t.Assert(array1.SubSlice(-5, 2), []string{"2", "3"})
|
||||
t.Assert(array1.SubSlice(-10, 1), nil)
|
||||
t.Assert(array2.SubSlice(0, 2), []string{"0", "1"})
|
||||
})
|
||||
}
|
||||
|
||||
func TestStrArray_Rand(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []string{"0", "1", "2", "3", "4", "5", "6"}
|
||||
array1 := garray.NewStrArrayFrom(a1)
|
||||
gtest.Assert(len(array1.Rands(2)), "2")
|
||||
gtest.Assert(len(array1.Rands(10)), "7")
|
||||
gtest.AssertIN(array1.Rands(1)[0], a1)
|
||||
gtest.Assert(len(array1.Rand()), 1)
|
||||
gtest.AssertIN(array1.Rand(), a1)
|
||||
t.Assert(len(array1.Rands(2)), "2")
|
||||
t.Assert(len(array1.Rands(10)), "7")
|
||||
t.AssertIN(array1.Rands(1)[0], a1)
|
||||
t.Assert(len(array1.Rand()), 1)
|
||||
t.AssertIN(array1.Rand(), a1)
|
||||
})
|
||||
}
|
||||
|
||||
func TestStrArray_PopRands(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []string{"a", "b", "c", "d", "e", "f", "g"}
|
||||
array1 := garray.NewStrArrayFrom(a1)
|
||||
gtest.AssertIN(array1.PopRands(1), []string{"a", "b", "c", "d", "e", "f", "g"})
|
||||
gtest.AssertIN(array1.PopRands(1), []string{"a", "b", "c", "d", "e", "f", "g"})
|
||||
gtest.AssertNI(array1.PopRands(1), array1.Slice())
|
||||
gtest.AssertNI(array1.PopRands(1), array1.Slice())
|
||||
gtest.Assert(len(array1.PopRands(10)), 3)
|
||||
t.AssertIN(array1.PopRands(1), []string{"a", "b", "c", "d", "e", "f", "g"})
|
||||
t.AssertIN(array1.PopRands(1), []string{"a", "b", "c", "d", "e", "f", "g"})
|
||||
t.AssertNI(array1.PopRands(1), array1.Slice())
|
||||
t.AssertNI(array1.PopRands(1), array1.Slice())
|
||||
t.Assert(len(array1.PopRands(10)), 3)
|
||||
})
|
||||
}
|
||||
|
||||
func TestStrArray_Shuffle(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []string{"0", "1", "2", "3", "4", "5", "6"}
|
||||
array1 := garray.NewStrArrayFrom(a1)
|
||||
gtest.Assert(array1.Shuffle().Len(), 7)
|
||||
t.Assert(array1.Shuffle().Len(), 7)
|
||||
})
|
||||
}
|
||||
|
||||
func TestStrArray_Reverse(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []string{"0", "1", "2", "3", "4", "5", "6"}
|
||||
array1 := garray.NewStrArrayFrom(a1)
|
||||
gtest.Assert(array1.Reverse().Slice(), []string{"6", "5", "4", "3", "2", "1", "0"})
|
||||
t.Assert(array1.Reverse().Slice(), []string{"6", "5", "4", "3", "2", "1", "0"})
|
||||
})
|
||||
}
|
||||
|
||||
func TestStrArray_Join(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []string{"0", "1", "2", "3", "4", "5", "6"}
|
||||
array1 := garray.NewStrArrayFrom(a1)
|
||||
gtest.Assert(array1.Join("."), `0.1.2.3.4.5.6`)
|
||||
t.Assert(array1.Join("."), `0.1.2.3.4.5.6`)
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []string{"0", "1", `"a"`, `\a`}
|
||||
array1 := garray.NewStrArrayFrom(a1)
|
||||
gtest.Assert(array1.Join("."), `0.1."a".\a`)
|
||||
t.Assert(array1.Join("."), `0.1."a".\a`)
|
||||
})
|
||||
}
|
||||
|
||||
func TestStrArray_String(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []string{"0", "1", "2", "3", "4", "5", "6"}
|
||||
array1 := garray.NewStrArrayFrom(a1)
|
||||
gtest.Assert(array1.String(), `["0","1","2","3","4","5","6"]`)
|
||||
t.Assert(array1.String(), `["0","1","2","3","4","5","6"]`)
|
||||
})
|
||||
}
|
||||
|
||||
func TestNewStrArrayFromCopy(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []string{"0", "1", "2", "3", "4", "5", "6"}
|
||||
a2 := garray.NewStrArrayFromCopy(a1)
|
||||
a3 := garray.NewStrArrayFromCopy(a1, true)
|
||||
gtest.Assert(a2.Contains("1"), true)
|
||||
gtest.Assert(a2.Len(), 7)
|
||||
gtest.Assert(a2, a3)
|
||||
t.Assert(a2.Contains("1"), true)
|
||||
t.Assert(a2.Len(), 7)
|
||||
t.Assert(a2, a3)
|
||||
})
|
||||
}
|
||||
|
||||
func TestStrArray_SetArray(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []string{"0", "1", "2", "3", "4", "5", "6"}
|
||||
a2 := []string{"a", "b", "c", "d"}
|
||||
array1 := garray.NewStrArrayFrom(a1)
|
||||
gtest.Assert(array1.Contains("2"), true)
|
||||
gtest.Assert(array1.Len(), 7)
|
||||
t.Assert(array1.Contains("2"), true)
|
||||
t.Assert(array1.Len(), 7)
|
||||
|
||||
array1 = array1.SetArray(a2)
|
||||
gtest.Assert(array1.Contains("2"), false)
|
||||
gtest.Assert(array1.Contains("c"), true)
|
||||
gtest.Assert(array1.Len(), 4)
|
||||
t.Assert(array1.Contains("2"), false)
|
||||
t.Assert(array1.Contains("c"), true)
|
||||
t.Assert(array1.Len(), 4)
|
||||
})
|
||||
}
|
||||
|
||||
func TestStrArray_Replace(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []string{"0", "1", "2", "3", "4", "5", "6"}
|
||||
a2 := []string{"a", "b", "c", "d"}
|
||||
a3 := []string{"o", "p", "q", "x", "y", "z", "w", "r", "v"}
|
||||
array1 := garray.NewStrArrayFrom(a1)
|
||||
gtest.Assert(array1.Contains("2"), true)
|
||||
gtest.Assert(array1.Len(), 7)
|
||||
t.Assert(array1.Contains("2"), true)
|
||||
t.Assert(array1.Len(), 7)
|
||||
|
||||
array1 = array1.Replace(a2)
|
||||
gtest.Assert(array1.Contains("2"), false)
|
||||
gtest.Assert(array1.Contains("c"), true)
|
||||
gtest.Assert(array1.Contains("5"), true)
|
||||
gtest.Assert(array1.Len(), 7)
|
||||
t.Assert(array1.Contains("2"), false)
|
||||
t.Assert(array1.Contains("c"), true)
|
||||
t.Assert(array1.Contains("5"), true)
|
||||
t.Assert(array1.Len(), 7)
|
||||
|
||||
array1 = array1.Replace(a3)
|
||||
gtest.Assert(array1.Contains("2"), false)
|
||||
gtest.Assert(array1.Contains("c"), false)
|
||||
gtest.Assert(array1.Contains("5"), false)
|
||||
gtest.Assert(array1.Contains("p"), true)
|
||||
gtest.Assert(array1.Contains("r"), false)
|
||||
gtest.Assert(array1.Len(), 7)
|
||||
t.Assert(array1.Contains("2"), false)
|
||||
t.Assert(array1.Contains("c"), false)
|
||||
t.Assert(array1.Contains("5"), false)
|
||||
t.Assert(array1.Contains("p"), true)
|
||||
t.Assert(array1.Contains("r"), false)
|
||||
t.Assert(array1.Len(), 7)
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
func TestStrArray_Sum(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []string{"0", "1", "2", "3", "4", "5", "6"}
|
||||
a2 := []string{"0", "a", "3", "4", "5", "6"}
|
||||
array1 := garray.NewStrArrayFrom(a1)
|
||||
array2 := garray.NewStrArrayFrom(a2)
|
||||
gtest.Assert(array1.Sum(), 21)
|
||||
gtest.Assert(array2.Sum(), 18)
|
||||
t.Assert(array1.Sum(), 21)
|
||||
t.Assert(array2.Sum(), 18)
|
||||
})
|
||||
}
|
||||
|
||||
func TestStrArray_PopRand(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []string{"0", "1", "2", "3", "4", "5", "6"}
|
||||
array1 := garray.NewStrArrayFrom(a1)
|
||||
str1 := array1.PopRand()
|
||||
gtest.Assert(strings.Contains("0,1,2,3,4,5,6", str1), true)
|
||||
gtest.Assert(array1.Len(), 6)
|
||||
t.Assert(strings.Contains("0,1,2,3,4,5,6", str1), true)
|
||||
t.Assert(array1.Len(), 6)
|
||||
})
|
||||
}
|
||||
|
||||
func TestStrArray_Clone(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []string{"0", "1", "2", "3", "4", "5", "6"}
|
||||
array1 := garray.NewStrArrayFrom(a1)
|
||||
array2 := array1.Clone()
|
||||
gtest.Assert(array2, array1)
|
||||
gtest.Assert(array2.Len(), 7)
|
||||
t.Assert(array2, array1)
|
||||
t.Assert(array2.Len(), 7)
|
||||
})
|
||||
}
|
||||
|
||||
func TestStrArray_CountValues(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []string{"0", "1", "2", "3", "4", "4", "6"}
|
||||
array1 := garray.NewStrArrayFrom(a1)
|
||||
|
||||
m1 := array1.CountValues()
|
||||
gtest.Assert(len(m1), 6)
|
||||
gtest.Assert(m1["2"], 1)
|
||||
gtest.Assert(m1["4"], 2)
|
||||
t.Assert(len(m1), 6)
|
||||
t.Assert(m1["2"], 1)
|
||||
t.Assert(m1["4"], 2)
|
||||
})
|
||||
}
|
||||
|
||||
func TestStrArray_Remove(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []string{"e", "a", "d", "a", "c"}
|
||||
array1 := garray.NewStrArrayFrom(a1)
|
||||
s1 := array1.Remove(1)
|
||||
gtest.Assert(s1, "a")
|
||||
gtest.Assert(array1.Len(), 4)
|
||||
t.Assert(s1, "a")
|
||||
t.Assert(array1.Len(), 4)
|
||||
s1 = array1.Remove(3)
|
||||
gtest.Assert(s1, "c")
|
||||
gtest.Assert(array1.Len(), 3)
|
||||
t.Assert(s1, "c")
|
||||
t.Assert(array1.Len(), 3)
|
||||
})
|
||||
}
|
||||
|
||||
func TestStrArray_RLockFunc(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s1 := []string{"a", "b", "c", "d"}
|
||||
a1 := garray.NewStrArrayFrom(s1, true)
|
||||
|
||||
@ -431,25 +441,25 @@ func TestStrArray_RLockFunc(t *testing.T) {
|
||||
<-ch2 //等待go1完成
|
||||
|
||||
// 防止ci抖动,以豪秒为单位
|
||||
gtest.AssertLT(t2-t1, 20) //go1加的读锁,所go2读的时候,并没有阻塞。
|
||||
gtest.Assert(a1.Contains("g"), true)
|
||||
t.AssertLT(t2-t1, 20) //go1加的读锁,所go2读的时候,并没有阻塞。
|
||||
t.Assert(a1.Contains("g"), true)
|
||||
})
|
||||
}
|
||||
|
||||
func TestStrArray_SortFunc(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s1 := []string{"a", "d", "c", "b"}
|
||||
a1 := garray.NewStrArrayFrom(s1)
|
||||
func1 := func(v1, v2 string) bool {
|
||||
return v1 < v2
|
||||
}
|
||||
a11 := a1.SortFunc(func1)
|
||||
gtest.Assert(a11, []string{"a", "b", "c", "d"})
|
||||
t.Assert(a11, []string{"a", "b", "c", "d"})
|
||||
})
|
||||
}
|
||||
|
||||
func TestStrArray_LockFunc(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s1 := []string{"a", "b", "c", "d"}
|
||||
a1 := garray.NewStrArrayFrom(s1, true)
|
||||
|
||||
@ -475,31 +485,31 @@ func TestStrArray_LockFunc(t *testing.T) {
|
||||
<-ch2 //等待go1完成
|
||||
|
||||
// 防止ci抖动,以豪秒为单位
|
||||
gtest.AssertGT(t2-t1, 20) //go1加的读写互斥锁,所go2读的时候被阻塞。
|
||||
gtest.Assert(a1.Contains("g"), true)
|
||||
t.AssertGT(t2-t1, 20) //go1加的读写互斥锁,所go2读的时候被阻塞。
|
||||
t.Assert(a1.Contains("g"), true)
|
||||
})
|
||||
}
|
||||
|
||||
func TestStrArray_Json(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s1 := []string{"a", "b", "d", "c"}
|
||||
a1 := garray.NewStrArrayFrom(s1)
|
||||
b1, err1 := json.Marshal(a1)
|
||||
b2, err2 := json.Marshal(s1)
|
||||
gtest.Assert(b1, b2)
|
||||
gtest.Assert(err1, err2)
|
||||
t.Assert(b1, b2)
|
||||
t.Assert(err1, err2)
|
||||
|
||||
a2 := garray.NewStrArray()
|
||||
err1 = json.Unmarshal(b2, &a2)
|
||||
gtest.Assert(a2.Slice(), s1)
|
||||
t.Assert(a2.Slice(), s1)
|
||||
|
||||
var a3 garray.StrArray
|
||||
err := json.Unmarshal(b2, &a3)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(a3.Slice(), s1)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(a3.Slice(), s1)
|
||||
})
|
||||
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type User struct {
|
||||
Name string
|
||||
Scores *garray.StrArray
|
||||
@ -509,111 +519,111 @@ func TestStrArray_Json(t *testing.T) {
|
||||
"Scores": []string{"A+", "A", "A"},
|
||||
}
|
||||
b, err := json.Marshal(data)
|
||||
gtest.Assert(err, nil)
|
||||
t.Assert(err, nil)
|
||||
|
||||
user := new(User)
|
||||
err = json.Unmarshal(b, user)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(user.Name, data["Name"])
|
||||
gtest.Assert(user.Scores, data["Scores"])
|
||||
t.Assert(err, nil)
|
||||
t.Assert(user.Name, data["Name"])
|
||||
t.Assert(user.Scores, data["Scores"])
|
||||
})
|
||||
}
|
||||
|
||||
func TestStrArray_Iterator(t *testing.T) {
|
||||
slice := g.SliceStr{"a", "b", "d", "c"}
|
||||
array := garray.NewStrArrayFrom(slice)
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
array.Iterator(func(k int, v string) bool {
|
||||
gtest.Assert(v, slice[k])
|
||||
t.Assert(v, slice[k])
|
||||
return true
|
||||
})
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
array.IteratorAsc(func(k int, v string) bool {
|
||||
gtest.Assert(v, slice[k])
|
||||
t.Assert(v, slice[k])
|
||||
return true
|
||||
})
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
array.IteratorDesc(func(k int, v string) bool {
|
||||
gtest.Assert(v, slice[k])
|
||||
t.Assert(v, slice[k])
|
||||
return true
|
||||
})
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
index := 0
|
||||
array.Iterator(func(k int, v string) bool {
|
||||
index++
|
||||
return false
|
||||
})
|
||||
gtest.Assert(index, 1)
|
||||
t.Assert(index, 1)
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
index := 0
|
||||
array.IteratorAsc(func(k int, v string) bool {
|
||||
index++
|
||||
return false
|
||||
})
|
||||
gtest.Assert(index, 1)
|
||||
t.Assert(index, 1)
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
index := 0
|
||||
array.IteratorDesc(func(k int, v string) bool {
|
||||
index++
|
||||
return false
|
||||
})
|
||||
gtest.Assert(index, 1)
|
||||
t.Assert(index, 1)
|
||||
})
|
||||
}
|
||||
|
||||
func TestStrArray_RemoveValue(t *testing.T) {
|
||||
slice := g.SliceStr{"a", "b", "d", "c"}
|
||||
array := garray.NewStrArrayFrom(slice)
|
||||
gtest.Case(t, func() {
|
||||
gtest.Assert(array.RemoveValue("e"), false)
|
||||
gtest.Assert(array.RemoveValue("b"), true)
|
||||
gtest.Assert(array.RemoveValue("a"), true)
|
||||
gtest.Assert(array.RemoveValue("c"), true)
|
||||
gtest.Assert(array.RemoveValue("f"), false)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
t.Assert(array.RemoveValue("e"), false)
|
||||
t.Assert(array.RemoveValue("b"), true)
|
||||
t.Assert(array.RemoveValue("a"), true)
|
||||
t.Assert(array.RemoveValue("c"), true)
|
||||
t.Assert(array.RemoveValue("f"), false)
|
||||
})
|
||||
}
|
||||
|
||||
func TestStrArray_UnmarshalValue(t *testing.T) {
|
||||
type T struct {
|
||||
type V struct {
|
||||
Name string
|
||||
Array *garray.StrArray
|
||||
}
|
||||
// JSON
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var v *V
|
||||
err := gconv.Struct(g.Map{
|
||||
"name": "john",
|
||||
"array": []byte(`["1","2","3"]`),
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Array.Slice(), g.SliceStr{"1", "2", "3"})
|
||||
}, &v)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(v.Name, "john")
|
||||
t.Assert(v.Array.Slice(), g.SliceStr{"1", "2", "3"})
|
||||
})
|
||||
// Map
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var v *V
|
||||
err := gconv.Struct(g.Map{
|
||||
"name": "john",
|
||||
"array": g.SliceStr{"1", "2", "3"},
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Array.Slice(), g.SliceStr{"1", "2", "3"})
|
||||
}, &v)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(v.Name, "john")
|
||||
t.Assert(v.Array.Slice(), g.SliceStr{"1", "2", "3"})
|
||||
})
|
||||
}
|
||||
|
||||
func TestStrArray_FilterEmpty(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
array := garray.NewStrArrayFrom(g.SliceStr{"", "1", "2", "0"})
|
||||
gtest.Assert(array.FilterEmpty(), g.SliceStr{"1", "2", "0"})
|
||||
t.Assert(array.FilterEmpty(), g.SliceStr{"1", "2", "0"})
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
array := garray.NewStrArrayFrom(g.SliceStr{"1", "2"})
|
||||
gtest.Assert(array.FilterEmpty(), g.SliceStr{"1", "2"})
|
||||
t.Assert(array.FilterEmpty(), g.SliceStr{"1", "2"})
|
||||
})
|
||||
}
|
||||
|
||||
@ -22,7 +22,7 @@ import (
|
||||
)
|
||||
|
||||
func TestSortedArray_NewSortedArrayFrom(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []interface{}{"a", "f", "c"}
|
||||
a2 := []interface{}{"h", "j", "i", "k"}
|
||||
func1 := func(v1, v2 interface{}) int {
|
||||
@ -34,16 +34,16 @@ func TestSortedArray_NewSortedArrayFrom(t *testing.T) {
|
||||
array1 := garray.NewSortedArrayFrom(a1, func1)
|
||||
array2 := garray.NewSortedArrayFrom(a2, func2)
|
||||
|
||||
gtest.Assert(array1.Len(), 3)
|
||||
gtest.Assert(array1, []interface{}{"a", "c", "f"})
|
||||
t.Assert(array1.Len(), 3)
|
||||
t.Assert(array1, []interface{}{"a", "c", "f"})
|
||||
|
||||
gtest.Assert(array2.Len(), 4)
|
||||
gtest.Assert(array2, []interface{}{"k", "i", "j", "h"})
|
||||
t.Assert(array2.Len(), 4)
|
||||
t.Assert(array2, []interface{}{"k", "i", "j", "h"})
|
||||
})
|
||||
}
|
||||
|
||||
func TestNewSortedArrayFromCopy(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []interface{}{"a", "f", "c"}
|
||||
|
||||
func1 := func(v1, v2 interface{}) int {
|
||||
@ -54,15 +54,15 @@ func TestNewSortedArrayFromCopy(t *testing.T) {
|
||||
}
|
||||
array1 := garray.NewSortedArrayFromCopy(a1, func1)
|
||||
array2 := garray.NewSortedArrayFromCopy(a1, func2)
|
||||
gtest.Assert(array1.Len(), 3)
|
||||
gtest.Assert(array1, []interface{}{"a", "c", "f"})
|
||||
gtest.Assert(array1.Len(), 3)
|
||||
gtest.Assert(array2, []interface{}{"c", "f", "a"})
|
||||
t.Assert(array1.Len(), 3)
|
||||
t.Assert(array1, []interface{}{"a", "c", "f"})
|
||||
t.Assert(array1.Len(), 3)
|
||||
t.Assert(array2, []interface{}{"c", "f", "a"})
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedArray_SetArray(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []interface{}{"a", "f", "c"}
|
||||
a2 := []interface{}{"e", "h", "g", "k"}
|
||||
|
||||
@ -72,171 +72,183 @@ func TestSortedArray_SetArray(t *testing.T) {
|
||||
|
||||
array1 := garray.NewSortedArrayFrom(a1, func1)
|
||||
array1.SetArray(a2)
|
||||
gtest.Assert(array1.Len(), 4)
|
||||
gtest.Assert(array1, []interface{}{"e", "g", "h", "k"})
|
||||
t.Assert(array1.Len(), 4)
|
||||
t.Assert(array1, []interface{}{"e", "g", "h", "k"})
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
func TestSortedArray_Sort(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []interface{}{"a", "f", "c"}
|
||||
func1 := func(v1, v2 interface{}) int {
|
||||
return strings.Compare(gconv.String(v1), gconv.String(v2))
|
||||
}
|
||||
array1 := garray.NewSortedArrayFrom(a1, func1)
|
||||
array1.Sort()
|
||||
gtest.Assert(array1.Len(), 3)
|
||||
gtest.Assert(array1, []interface{}{"a", "c", "f"})
|
||||
t.Assert(array1.Len(), 3)
|
||||
t.Assert(array1, []interface{}{"a", "c", "f"})
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
func TestSortedArray_Get(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []interface{}{"a", "f", "c"}
|
||||
func1 := func(v1, v2 interface{}) int {
|
||||
return strings.Compare(gconv.String(v1), gconv.String(v2))
|
||||
}
|
||||
array1 := garray.NewSortedArrayFrom(a1, func1)
|
||||
gtest.Assert(array1.Get(2), "f")
|
||||
gtest.Assert(array1.Get(1), "c")
|
||||
t.Assert(array1.Get(2), "f")
|
||||
t.Assert(array1.Get(1), "c")
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
func TestSortedArray_Remove(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []interface{}{"a", "d", "c", "b"}
|
||||
func1 := func(v1, v2 interface{}) int {
|
||||
return strings.Compare(gconv.String(v1), gconv.String(v2))
|
||||
}
|
||||
array1 := garray.NewSortedArrayFrom(a1, func1)
|
||||
i1 := array1.Remove(1)
|
||||
gtest.Assert(gconv.String(i1), "b")
|
||||
gtest.Assert(array1.Len(), 3)
|
||||
gtest.Assert(array1.Contains("b"), false)
|
||||
t.Assert(gconv.String(i1), "b")
|
||||
t.Assert(array1.Len(), 3)
|
||||
t.Assert(array1.Contains("b"), false)
|
||||
|
||||
gtest.Assert(array1.Remove(-1), nil)
|
||||
gtest.Assert(array1.Remove(100000), nil)
|
||||
t.Assert(array1.Remove(-1), nil)
|
||||
t.Assert(array1.Remove(100000), nil)
|
||||
|
||||
i2 := array1.Remove(0)
|
||||
gtest.Assert(gconv.String(i2), "a")
|
||||
gtest.Assert(array1.Len(), 2)
|
||||
gtest.Assert(array1.Contains("a"), false)
|
||||
t.Assert(gconv.String(i2), "a")
|
||||
t.Assert(array1.Len(), 2)
|
||||
t.Assert(array1.Contains("a"), false)
|
||||
|
||||
i3 := array1.Remove(1)
|
||||
gtest.Assert(gconv.String(i3), "d")
|
||||
gtest.Assert(array1.Len(), 1)
|
||||
gtest.Assert(array1.Contains("d"), false)
|
||||
t.Assert(gconv.String(i3), "d")
|
||||
t.Assert(array1.Len(), 1)
|
||||
t.Assert(array1.Contains("d"), false)
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
func TestSortedArray_PopLeft(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []interface{}{"a", "d", "c", "b"}
|
||||
func1 := func(v1, v2 interface{}) int {
|
||||
return strings.Compare(gconv.String(v1), gconv.String(v2))
|
||||
}
|
||||
array1 := garray.NewSortedArrayFrom(a1, func1)
|
||||
i1 := array1.PopLeft()
|
||||
gtest.Assert(gconv.String(i1), "a")
|
||||
gtest.Assert(array1.Len(), 3)
|
||||
gtest.Assert(array1, []interface{}{"b", "c", "d"})
|
||||
t.Assert(gconv.String(i1), "a")
|
||||
t.Assert(array1.Len(), 3)
|
||||
t.Assert(array1, []interface{}{"b", "c", "d"})
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
func TestSortedArray_PopRight(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []interface{}{"a", "d", "c", "b"}
|
||||
func1 := func(v1, v2 interface{}) int {
|
||||
return strings.Compare(gconv.String(v1), gconv.String(v2))
|
||||
}
|
||||
array1 := garray.NewSortedArrayFrom(a1, func1)
|
||||
i1 := array1.PopRight()
|
||||
gtest.Assert(gconv.String(i1), "d")
|
||||
gtest.Assert(array1.Len(), 3)
|
||||
gtest.Assert(array1, []interface{}{"a", "b", "c"})
|
||||
t.Assert(gconv.String(i1), "d")
|
||||
t.Assert(array1.Len(), 3)
|
||||
t.Assert(array1, []interface{}{"a", "b", "c"})
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
func TestSortedArray_PopRand(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []interface{}{"a", "d", "c", "b"}
|
||||
func1 := func(v1, v2 interface{}) int {
|
||||
return strings.Compare(gconv.String(v1), gconv.String(v2))
|
||||
}
|
||||
array1 := garray.NewSortedArrayFrom(a1, func1)
|
||||
i1 := array1.PopRand()
|
||||
gtest.AssertIN(i1, []interface{}{"a", "d", "c", "b"})
|
||||
gtest.Assert(array1.Len(), 3)
|
||||
t.AssertIN(i1, []interface{}{"a", "d", "c", "b"})
|
||||
t.Assert(array1.Len(), 3)
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedArray_PopRands(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []interface{}{"a", "d", "c", "b"}
|
||||
func1 := func(v1, v2 interface{}) int {
|
||||
return strings.Compare(gconv.String(v1), gconv.String(v2))
|
||||
}
|
||||
array1 := garray.NewSortedArrayFrom(a1, func1)
|
||||
i1 := array1.PopRands(2)
|
||||
gtest.Assert(len(i1), 2)
|
||||
gtest.AssertIN(i1, []interface{}{"a", "d", "c", "b"})
|
||||
gtest.Assert(array1.Len(), 2)
|
||||
t.Assert(len(i1), 2)
|
||||
t.AssertIN(i1, []interface{}{"a", "d", "c", "b"})
|
||||
t.Assert(array1.Len(), 2)
|
||||
|
||||
i2 := array1.PopRands(3)
|
||||
gtest.Assert(len(i1), 2)
|
||||
gtest.AssertIN(i2, []interface{}{"a", "d", "c", "b"})
|
||||
gtest.Assert(array1.Len(), 0)
|
||||
t.Assert(len(i1), 2)
|
||||
t.AssertIN(i2, []interface{}{"a", "d", "c", "b"})
|
||||
t.Assert(array1.Len(), 0)
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedArray_Empty(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
array := garray.NewSortedArray(gutil.ComparatorInt)
|
||||
t.Assert(array.PopLeft(), nil)
|
||||
t.Assert(array.PopLefts(10), nil)
|
||||
t.Assert(array.PopRight(), nil)
|
||||
t.Assert(array.PopRights(10), nil)
|
||||
t.Assert(array.PopRand(), nil)
|
||||
t.Assert(array.PopRands(10), nil)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedArray_PopLefts(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []interface{}{"a", "d", "c", "b", "e", "f"}
|
||||
func1 := func(v1, v2 interface{}) int {
|
||||
return strings.Compare(gconv.String(v1), gconv.String(v2))
|
||||
}
|
||||
array1 := garray.NewSortedArrayFrom(a1, func1)
|
||||
i1 := array1.PopLefts(2)
|
||||
gtest.Assert(len(i1), 2)
|
||||
gtest.AssertIN(i1, []interface{}{"a", "d", "c", "b", "e", "f"})
|
||||
gtest.Assert(array1.Len(), 4)
|
||||
t.Assert(len(i1), 2)
|
||||
t.AssertIN(i1, []interface{}{"a", "d", "c", "b", "e", "f"})
|
||||
t.Assert(array1.Len(), 4)
|
||||
|
||||
i2 := array1.PopLefts(5)
|
||||
gtest.Assert(len(i2), 4)
|
||||
gtest.AssertIN(i1, []interface{}{"a", "d", "c", "b", "e", "f"})
|
||||
gtest.Assert(array1.Len(), 0)
|
||||
t.Assert(len(i2), 4)
|
||||
t.AssertIN(i1, []interface{}{"a", "d", "c", "b", "e", "f"})
|
||||
t.Assert(array1.Len(), 0)
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedArray_PopRights(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []interface{}{"a", "d", "c", "b", "e", "f"}
|
||||
func1 := func(v1, v2 interface{}) int {
|
||||
return strings.Compare(gconv.String(v1), gconv.String(v2))
|
||||
}
|
||||
array1 := garray.NewSortedArrayFrom(a1, func1)
|
||||
i1 := array1.PopRights(2)
|
||||
gtest.Assert(len(i1), 2)
|
||||
gtest.Assert(i1, []interface{}{"e", "f"})
|
||||
gtest.Assert(array1.Len(), 4)
|
||||
t.Assert(len(i1), 2)
|
||||
t.Assert(i1, []interface{}{"e", "f"})
|
||||
t.Assert(array1.Len(), 4)
|
||||
|
||||
i2 := array1.PopRights(10)
|
||||
gtest.Assert(len(i2), 4)
|
||||
t.Assert(len(i2), 4)
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedArray_Range(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []interface{}{"a", "d", "c", "b", "e", "f"}
|
||||
func1 := func(v1, v2 interface{}) int {
|
||||
return strings.Compare(gconv.String(v1), gconv.String(v2))
|
||||
@ -244,25 +256,25 @@ func TestSortedArray_Range(t *testing.T) {
|
||||
array1 := garray.NewSortedArrayFrom(a1, func1)
|
||||
array2 := garray.NewSortedArrayFrom(a1, func1, true)
|
||||
i1 := array1.Range(2, 5)
|
||||
gtest.Assert(i1, []interface{}{"c", "d", "e"})
|
||||
gtest.Assert(array1.Len(), 6)
|
||||
t.Assert(i1, []interface{}{"c", "d", "e"})
|
||||
t.Assert(array1.Len(), 6)
|
||||
|
||||
i2 := array1.Range(7, 5)
|
||||
gtest.Assert(len(i2), 0)
|
||||
t.Assert(len(i2), 0)
|
||||
i2 = array1.Range(-1, 2)
|
||||
gtest.Assert(i2, []interface{}{"a", "b"})
|
||||
t.Assert(i2, []interface{}{"a", "b"})
|
||||
|
||||
i2 = array1.Range(4, 10)
|
||||
gtest.Assert(len(i2), 2)
|
||||
gtest.Assert(i2, []interface{}{"e", "f"})
|
||||
t.Assert(len(i2), 2)
|
||||
t.Assert(i2, []interface{}{"e", "f"})
|
||||
|
||||
gtest.Assert(array2.Range(1, 3), []interface{}{"b", "c"})
|
||||
t.Assert(array2.Range(1, 3), []interface{}{"b", "c"})
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedArray_Sum(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []interface{}{"a", "d", "c", "b", "e", "f"}
|
||||
a2 := []interface{}{"1", "2", "3", "b", "e", "f"}
|
||||
a3 := []interface{}{"4", "5", "6"}
|
||||
@ -272,15 +284,15 @@ func TestSortedArray_Sum(t *testing.T) {
|
||||
array1 := garray.NewSortedArrayFrom(a1, func1)
|
||||
array2 := garray.NewSortedArrayFrom(a2, func1)
|
||||
array3 := garray.NewSortedArrayFrom(a3, func1)
|
||||
gtest.Assert(array1.Sum(), 0)
|
||||
gtest.Assert(array2.Sum(), 6)
|
||||
gtest.Assert(array3.Sum(), 15)
|
||||
t.Assert(array1.Sum(), 0)
|
||||
t.Assert(array2.Sum(), 6)
|
||||
t.Assert(array3.Sum(), 15)
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedArray_Clone(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []interface{}{"a", "d", "c", "b", "e", "f"}
|
||||
|
||||
func1 := func(v1, v2 interface{}) int {
|
||||
@ -288,30 +300,30 @@ func TestSortedArray_Clone(t *testing.T) {
|
||||
}
|
||||
array1 := garray.NewSortedArrayFrom(a1, func1)
|
||||
array2 := array1.Clone()
|
||||
gtest.Assert(array1, array2)
|
||||
t.Assert(array1, array2)
|
||||
array1.Remove(1)
|
||||
gtest.AssertNE(array1, array2)
|
||||
t.AssertNE(array1, array2)
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedArray_Clear(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []interface{}{"a", "d", "c", "b", "e", "f"}
|
||||
|
||||
func1 := func(v1, v2 interface{}) int {
|
||||
return strings.Compare(gconv.String(v1), gconv.String(v2))
|
||||
}
|
||||
array1 := garray.NewSortedArrayFrom(a1, func1)
|
||||
gtest.Assert(array1.Len(), 6)
|
||||
t.Assert(array1.Len(), 6)
|
||||
array1.Clear()
|
||||
gtest.Assert(array1.Len(), 0)
|
||||
t.Assert(array1.Len(), 0)
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedArray_Chunk(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []interface{}{"a", "d", "c", "b", "e"}
|
||||
|
||||
func1 := func(v1, v2 interface{}) int {
|
||||
@ -319,45 +331,45 @@ func TestSortedArray_Chunk(t *testing.T) {
|
||||
}
|
||||
array1 := garray.NewSortedArrayFrom(a1, func1)
|
||||
i1 := array1.Chunk(2)
|
||||
gtest.Assert(len(i1), 3)
|
||||
gtest.Assert(i1[0], []interface{}{"a", "b"})
|
||||
gtest.Assert(i1[2], []interface{}{"e"})
|
||||
t.Assert(len(i1), 3)
|
||||
t.Assert(i1[0], []interface{}{"a", "b"})
|
||||
t.Assert(i1[2], []interface{}{"e"})
|
||||
|
||||
i1 = array1.Chunk(0)
|
||||
gtest.Assert(len(i1), 0)
|
||||
t.Assert(len(i1), 0)
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []interface{}{1, 2, 3, 4, 5}
|
||||
array1 := garray.NewSortedArrayFrom(a1, gutil.ComparatorInt)
|
||||
chunks := array1.Chunk(3)
|
||||
gtest.Assert(len(chunks), 2)
|
||||
gtest.Assert(chunks[0], []interface{}{1, 2, 3})
|
||||
gtest.Assert(chunks[1], []interface{}{4, 5})
|
||||
gtest.Assert(array1.Chunk(0), nil)
|
||||
t.Assert(len(chunks), 2)
|
||||
t.Assert(chunks[0], []interface{}{1, 2, 3})
|
||||
t.Assert(chunks[1], []interface{}{4, 5})
|
||||
t.Assert(array1.Chunk(0), nil)
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []interface{}{1, 2, 3, 4, 5, 6}
|
||||
array1 := garray.NewSortedArrayFrom(a1, gutil.ComparatorInt)
|
||||
chunks := array1.Chunk(2)
|
||||
gtest.Assert(len(chunks), 3)
|
||||
gtest.Assert(chunks[0], []interface{}{1, 2})
|
||||
gtest.Assert(chunks[1], []interface{}{3, 4})
|
||||
gtest.Assert(chunks[2], []interface{}{5, 6})
|
||||
gtest.Assert(array1.Chunk(0), nil)
|
||||
t.Assert(len(chunks), 3)
|
||||
t.Assert(chunks[0], []interface{}{1, 2})
|
||||
t.Assert(chunks[1], []interface{}{3, 4})
|
||||
t.Assert(chunks[2], []interface{}{5, 6})
|
||||
t.Assert(array1.Chunk(0), nil)
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []interface{}{1, 2, 3, 4, 5, 6}
|
||||
array1 := garray.NewSortedArrayFrom(a1, gutil.ComparatorInt)
|
||||
chunks := array1.Chunk(3)
|
||||
gtest.Assert(len(chunks), 2)
|
||||
gtest.Assert(chunks[0], []interface{}{1, 2, 3})
|
||||
gtest.Assert(chunks[1], []interface{}{4, 5, 6})
|
||||
gtest.Assert(array1.Chunk(0), nil)
|
||||
t.Assert(len(chunks), 2)
|
||||
t.Assert(chunks[0], []interface{}{1, 2, 3})
|
||||
t.Assert(chunks[1], []interface{}{4, 5, 6})
|
||||
t.Assert(array1.Chunk(0), nil)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedArray_SubSlice(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []interface{}{"a", "d", "c", "b", "e"}
|
||||
|
||||
func1 := func(v1, v2 interface{}) int {
|
||||
@ -366,28 +378,28 @@ func TestSortedArray_SubSlice(t *testing.T) {
|
||||
array1 := garray.NewSortedArrayFrom(a1, func1)
|
||||
array2 := garray.NewSortedArrayFrom(a1, func1, true)
|
||||
i1 := array1.SubSlice(2, 3)
|
||||
gtest.Assert(len(i1), 3)
|
||||
gtest.Assert(i1, []interface{}{"c", "d", "e"})
|
||||
t.Assert(len(i1), 3)
|
||||
t.Assert(i1, []interface{}{"c", "d", "e"})
|
||||
|
||||
i1 = array1.SubSlice(2, 6)
|
||||
gtest.Assert(len(i1), 3)
|
||||
gtest.Assert(i1, []interface{}{"c", "d", "e"})
|
||||
t.Assert(len(i1), 3)
|
||||
t.Assert(i1, []interface{}{"c", "d", "e"})
|
||||
|
||||
i1 = array1.SubSlice(7, 2)
|
||||
gtest.Assert(len(i1), 0)
|
||||
t.Assert(len(i1), 0)
|
||||
|
||||
s1 := array1.SubSlice(1, -2)
|
||||
gtest.Assert(s1, nil)
|
||||
t.Assert(s1, nil)
|
||||
|
||||
s1 = array1.SubSlice(-9, 2)
|
||||
gtest.Assert(s1, nil)
|
||||
gtest.Assert(array2.SubSlice(1, 3), []interface{}{"b", "c", "d"})
|
||||
t.Assert(s1, nil)
|
||||
t.Assert(array2.SubSlice(1, 3), []interface{}{"b", "c", "d"})
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedArray_Rand(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []interface{}{"a", "d", "c"}
|
||||
|
||||
func1 := func(v1, v2 interface{}) int {
|
||||
@ -395,13 +407,13 @@ func TestSortedArray_Rand(t *testing.T) {
|
||||
}
|
||||
array1 := garray.NewSortedArrayFrom(a1, func1)
|
||||
i1 := array1.Rand()
|
||||
gtest.AssertIN(i1, []interface{}{"a", "d", "c"})
|
||||
gtest.Assert(array1.Len(), 3)
|
||||
t.AssertIN(i1, []interface{}{"a", "d", "c"})
|
||||
t.Assert(array1.Len(), 3)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedArray_Rands(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []interface{}{"a", "d", "c"}
|
||||
|
||||
func1 := func(v1, v2 interface{}) int {
|
||||
@ -409,43 +421,43 @@ func TestSortedArray_Rands(t *testing.T) {
|
||||
}
|
||||
array1 := garray.NewSortedArrayFrom(a1, func1)
|
||||
i1 := array1.Rands(2)
|
||||
gtest.AssertIN(i1, []interface{}{"a", "d", "c"})
|
||||
gtest.Assert(len(i1), 2)
|
||||
gtest.Assert(array1.Len(), 3)
|
||||
t.AssertIN(i1, []interface{}{"a", "d", "c"})
|
||||
t.Assert(len(i1), 2)
|
||||
t.Assert(array1.Len(), 3)
|
||||
|
||||
i1 = array1.Rands(4)
|
||||
gtest.Assert(len(i1), 3)
|
||||
t.Assert(len(i1), 3)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedArray_Join(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []interface{}{"a", "d", "c"}
|
||||
func1 := func(v1, v2 interface{}) int {
|
||||
return strings.Compare(gconv.String(v1), gconv.String(v2))
|
||||
}
|
||||
array1 := garray.NewSortedArrayFrom(a1, func1)
|
||||
gtest.Assert(array1.Join(","), `a,c,d`)
|
||||
gtest.Assert(array1.Join("."), `a.c.d`)
|
||||
t.Assert(array1.Join(","), `a,c,d`)
|
||||
t.Assert(array1.Join("."), `a.c.d`)
|
||||
})
|
||||
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []interface{}{0, 1, `"a"`, `\a`}
|
||||
array1 := garray.NewSortedArrayFrom(a1, gutil.ComparatorString)
|
||||
gtest.Assert(array1.Join("."), `"a".0.1.\a`)
|
||||
t.Assert(array1.Join("."), `"a".0.1.\a`)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedArray_String(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []interface{}{0, 1, "a", "b"}
|
||||
array1 := garray.NewSortedArrayFrom(a1, gutil.ComparatorString)
|
||||
gtest.Assert(array1.String(), `[0,1,"a","b"]`)
|
||||
t.Assert(array1.String(), `[0,1,"a","b"]`)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedArray_CountValues(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []interface{}{"a", "d", "c", "c"}
|
||||
|
||||
func1 := func(v1, v2 interface{}) int {
|
||||
@ -453,15 +465,15 @@ func TestSortedArray_CountValues(t *testing.T) {
|
||||
}
|
||||
array1 := garray.NewSortedArrayFrom(a1, func1)
|
||||
m1 := array1.CountValues()
|
||||
gtest.Assert(len(m1), 3)
|
||||
gtest.Assert(m1["c"], 2)
|
||||
gtest.Assert(m1["a"], 1)
|
||||
t.Assert(len(m1), 3)
|
||||
t.Assert(m1["c"], 2)
|
||||
t.Assert(m1["a"], 1)
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedArray_SetUnique(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []interface{}{"a", "d", "c", "c"}
|
||||
|
||||
func1 := func(v1, v2 interface{}) int {
|
||||
@ -469,13 +481,13 @@ func TestSortedArray_SetUnique(t *testing.T) {
|
||||
}
|
||||
array1 := garray.NewSortedArrayFrom(a1, func1)
|
||||
array1.SetUnique(true)
|
||||
gtest.Assert(array1.Len(), 3)
|
||||
gtest.Assert(array1, []interface{}{"a", "c", "d"})
|
||||
t.Assert(array1.Len(), 3)
|
||||
t.Assert(array1, []interface{}{"a", "c", "d"})
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedArray_LockFunc(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
func1 := func(v1, v2 interface{}) int {
|
||||
return strings.Compare(gconv.String(v1), gconv.String(v2))
|
||||
}
|
||||
@ -504,13 +516,13 @@ func TestSortedArray_LockFunc(t *testing.T) {
|
||||
<-ch2 //等待go1完成
|
||||
|
||||
// 防止ci抖动,以豪秒为单位
|
||||
gtest.AssertGT(t2-t1, 20) //go1加的读写互斥锁,所go2读的时候被阻塞。
|
||||
gtest.Assert(a1.Contains("g"), true)
|
||||
t.AssertGT(t2-t1, 20) //go1加的读写互斥锁,所go2读的时候被阻塞。
|
||||
t.Assert(a1.Contains("g"), true)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedArray_RLockFunc(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
func1 := func(v1, v2 interface{}) int {
|
||||
return strings.Compare(gconv.String(v1), gconv.String(v2))
|
||||
}
|
||||
@ -539,13 +551,13 @@ func TestSortedArray_RLockFunc(t *testing.T) {
|
||||
<-ch2 //等待go1完成
|
||||
|
||||
// 防止ci抖动,以豪秒为单位
|
||||
gtest.AssertLT(t2-t1, 20) //go1加的读锁,所go2读的时候不会被阻塞。
|
||||
gtest.Assert(a1.Contains("g"), true)
|
||||
t.AssertLT(t2-t1, 20) //go1加的读锁,所go2读的时候不会被阻塞。
|
||||
t.Assert(a1.Contains("g"), true)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedArray_Merge(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
func1 := func(v1, v2 interface{}) int {
|
||||
if gconv.Int(v1) < gconv.Int(v2) {
|
||||
return 0
|
||||
@ -564,38 +576,38 @@ func TestSortedArray_Merge(t *testing.T) {
|
||||
|
||||
a1 := garray.NewSortedArrayFrom(s1, func1)
|
||||
|
||||
gtest.Assert(a1.Merge(s2).Len(), 6)
|
||||
gtest.Assert(a1.Merge(i1).Len(), 9)
|
||||
gtest.Assert(a1.Merge(i2).Len(), 10)
|
||||
gtest.Assert(a1.Merge(s3).Len(), 12)
|
||||
gtest.Assert(a1.Merge(s4).Len(), 14)
|
||||
gtest.Assert(a1.Merge(s5).Len(), 16)
|
||||
gtest.Assert(a1.Merge(s6).Len(), 19)
|
||||
t.Assert(a1.Merge(s2).Len(), 6)
|
||||
t.Assert(a1.Merge(i1).Len(), 9)
|
||||
t.Assert(a1.Merge(i2).Len(), 10)
|
||||
t.Assert(a1.Merge(s3).Len(), 12)
|
||||
t.Assert(a1.Merge(s4).Len(), 14)
|
||||
t.Assert(a1.Merge(s5).Len(), 16)
|
||||
t.Assert(a1.Merge(s6).Len(), 19)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedArray_Json(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s1 := []interface{}{"a", "b", "d", "c"}
|
||||
s2 := []interface{}{"a", "b", "c", "d"}
|
||||
a1 := garray.NewSortedArrayFrom(s1, gutil.ComparatorString)
|
||||
b1, err1 := json.Marshal(a1)
|
||||
b2, err2 := json.Marshal(s1)
|
||||
gtest.Assert(b1, b2)
|
||||
gtest.Assert(err1, err2)
|
||||
t.Assert(b1, b2)
|
||||
t.Assert(err1, err2)
|
||||
|
||||
a2 := garray.NewSortedArray(gutil.ComparatorString)
|
||||
err1 = json.Unmarshal(b2, &a2)
|
||||
gtest.Assert(a2.Slice(), s2)
|
||||
t.Assert(a2.Slice(), s2)
|
||||
|
||||
var a3 garray.SortedArray
|
||||
err := json.Unmarshal(b2, &a3)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(a3.Slice(), s1)
|
||||
gtest.Assert(a3.Interfaces(), s1)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(a3.Slice(), s1)
|
||||
t.Assert(a3.Interfaces(), s1)
|
||||
})
|
||||
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type User struct {
|
||||
Name string
|
||||
Scores *garray.SortedArray
|
||||
@ -605,127 +617,127 @@ func TestSortedArray_Json(t *testing.T) {
|
||||
"Scores": []int{99, 100, 98},
|
||||
}
|
||||
b, err := json.Marshal(data)
|
||||
gtest.Assert(err, nil)
|
||||
t.Assert(err, nil)
|
||||
|
||||
user := new(User)
|
||||
err = json.Unmarshal(b, user)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(user.Name, data["Name"])
|
||||
gtest.AssertNE(user.Scores, nil)
|
||||
gtest.Assert(user.Scores.Len(), 3)
|
||||
gtest.AssertIN(user.Scores.PopLeft(), data["Scores"])
|
||||
gtest.AssertIN(user.Scores.PopLeft(), data["Scores"])
|
||||
gtest.AssertIN(user.Scores.PopLeft(), data["Scores"])
|
||||
t.Assert(err, nil)
|
||||
t.Assert(user.Name, data["Name"])
|
||||
t.AssertNE(user.Scores, nil)
|
||||
t.Assert(user.Scores.Len(), 3)
|
||||
t.AssertIN(user.Scores.PopLeft(), data["Scores"])
|
||||
t.AssertIN(user.Scores.PopLeft(), data["Scores"])
|
||||
t.AssertIN(user.Scores.PopLeft(), data["Scores"])
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedArray_Iterator(t *testing.T) {
|
||||
slice := g.Slice{"a", "b", "d", "c"}
|
||||
array := garray.NewSortedArrayFrom(slice, gutil.ComparatorString)
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
array.Iterator(func(k int, v interface{}) bool {
|
||||
gtest.Assert(v, slice[k])
|
||||
t.Assert(v, slice[k])
|
||||
return true
|
||||
})
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
array.IteratorAsc(func(k int, v interface{}) bool {
|
||||
gtest.Assert(v, slice[k])
|
||||
t.Assert(v, slice[k])
|
||||
return true
|
||||
})
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
array.IteratorDesc(func(k int, v interface{}) bool {
|
||||
gtest.Assert(v, slice[k])
|
||||
t.Assert(v, slice[k])
|
||||
return true
|
||||
})
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
index := 0
|
||||
array.Iterator(func(k int, v interface{}) bool {
|
||||
index++
|
||||
return false
|
||||
})
|
||||
gtest.Assert(index, 1)
|
||||
t.Assert(index, 1)
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
index := 0
|
||||
array.IteratorAsc(func(k int, v interface{}) bool {
|
||||
index++
|
||||
return false
|
||||
})
|
||||
gtest.Assert(index, 1)
|
||||
t.Assert(index, 1)
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
index := 0
|
||||
array.IteratorDesc(func(k int, v interface{}) bool {
|
||||
index++
|
||||
return false
|
||||
})
|
||||
gtest.Assert(index, 1)
|
||||
t.Assert(index, 1)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedArray_RemoveValue(t *testing.T) {
|
||||
slice := g.Slice{"a", "b", "d", "c"}
|
||||
array := garray.NewSortedArrayFrom(slice, gutil.ComparatorString)
|
||||
gtest.Case(t, func() {
|
||||
gtest.Assert(array.RemoveValue("e"), false)
|
||||
gtest.Assert(array.RemoveValue("b"), true)
|
||||
gtest.Assert(array.RemoveValue("a"), true)
|
||||
gtest.Assert(array.RemoveValue("c"), true)
|
||||
gtest.Assert(array.RemoveValue("f"), false)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
t.Assert(array.RemoveValue("e"), false)
|
||||
t.Assert(array.RemoveValue("b"), true)
|
||||
t.Assert(array.RemoveValue("a"), true)
|
||||
t.Assert(array.RemoveValue("c"), true)
|
||||
t.Assert(array.RemoveValue("f"), false)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedArray_UnmarshalValue(t *testing.T) {
|
||||
type T struct {
|
||||
type V struct {
|
||||
Name string
|
||||
Array *garray.SortedArray
|
||||
}
|
||||
// JSON
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var v *V
|
||||
err := gconv.Struct(g.Map{
|
||||
"name": "john",
|
||||
"array": []byte(`[2,3,1]`),
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Array.Slice(), g.Slice{1, 2, 3})
|
||||
}, &v)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(v.Name, "john")
|
||||
t.Assert(v.Array.Slice(), g.Slice{1, 2, 3})
|
||||
})
|
||||
// Map
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var v *V
|
||||
err := gconv.Struct(g.Map{
|
||||
"name": "john",
|
||||
"array": g.Slice{2, 3, 1},
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Array.Slice(), g.Slice{1, 2, 3})
|
||||
}, &v)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(v.Name, "john")
|
||||
t.Assert(v.Array.Slice(), g.Slice{1, 2, 3})
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedArray_FilterNil(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
values := g.Slice{0, 1, 2, 3, 4, "", g.Slice{}}
|
||||
array := garray.NewSortedArrayFromCopy(values, gutil.ComparatorInt)
|
||||
gtest.Assert(array.FilterNil().Slice(), g.Slice{0, "", g.Slice{}, 1, 2, 3, 4})
|
||||
t.Assert(array.FilterNil().Slice(), g.Slice{0, "", g.Slice{}, 1, 2, 3, 4})
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
array := garray.NewSortedArrayFromCopy(g.Slice{nil, 1, 2, 3, 4, nil}, gutil.ComparatorInt)
|
||||
gtest.Assert(array.FilterNil(), g.Slice{1, 2, 3, 4})
|
||||
t.Assert(array.FilterNil(), g.Slice{1, 2, 3, 4})
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedArray_FilterEmpty(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
array := garray.NewSortedArrayFrom(g.Slice{0, 1, 2, 3, 4, "", g.Slice{}}, gutil.ComparatorInt)
|
||||
gtest.Assert(array.FilterEmpty(), g.Slice{1, 2, 3, 4})
|
||||
t.Assert(array.FilterEmpty(), g.Slice{1, 2, 3, 4})
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
array := garray.NewSortedArrayFrom(g.Slice{1, 2, 3, 4}, gutil.ComparatorInt)
|
||||
gtest.Assert(array.FilterEmpty(), g.Slice{1, 2, 3, 4})
|
||||
t.Assert(array.FilterEmpty(), g.Slice{1, 2, 3, 4})
|
||||
})
|
||||
}
|
||||
|
||||
@ -21,355 +21,367 @@ import (
|
||||
)
|
||||
|
||||
func TestNewSortedIntArrayFrom(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []int{0, 3, 2, 1, 4, 5, 6}
|
||||
array1 := garray.NewSortedIntArrayFrom(a1, true)
|
||||
gtest.Assert(array1.Join("."), "0.1.2.3.4.5.6")
|
||||
gtest.Assert(array1.Slice(), a1)
|
||||
gtest.Assert(array1.Interfaces(), a1)
|
||||
t.Assert(array1.Join("."), "0.1.2.3.4.5.6")
|
||||
t.Assert(array1.Slice(), a1)
|
||||
t.Assert(array1.Interfaces(), a1)
|
||||
})
|
||||
}
|
||||
|
||||
func TestNewSortedIntArrayFromCopy(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []int{0, 5, 2, 1, 4, 3, 6}
|
||||
array1 := garray.NewSortedIntArrayFromCopy(a1, false)
|
||||
gtest.Assert(array1.Join("."), "0.1.2.3.4.5.6")
|
||||
t.Assert(array1.Join("."), "0.1.2.3.4.5.6")
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedIntArray_SetArray(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []int{0, 1, 2, 3}
|
||||
a2 := []int{4, 5, 6}
|
||||
array1 := garray.NewSortedIntArrayFrom(a1)
|
||||
array2 := array1.SetArray(a2)
|
||||
|
||||
gtest.Assert(array2.Len(), 3)
|
||||
gtest.Assert(array2.Search(3), -1)
|
||||
gtest.Assert(array2.Search(5), 1)
|
||||
gtest.Assert(array2.Search(6), 2)
|
||||
t.Assert(array2.Len(), 3)
|
||||
t.Assert(array2.Search(3), -1)
|
||||
t.Assert(array2.Search(5), 1)
|
||||
t.Assert(array2.Search(6), 2)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedIntArray_Sort(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []int{0, 3, 2, 1}
|
||||
|
||||
array1 := garray.NewSortedIntArrayFrom(a1)
|
||||
array2 := array1.Sort()
|
||||
|
||||
gtest.Assert(array2.Len(), 4)
|
||||
gtest.Assert(array2, []int{0, 1, 2, 3})
|
||||
t.Assert(array2.Len(), 4)
|
||||
t.Assert(array2, []int{0, 1, 2, 3})
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedIntArray_Get(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []int{1, 3, 5, 0}
|
||||
array1 := garray.NewSortedIntArrayFrom(a1)
|
||||
gtest.Assert(array1.Get(0), 0)
|
||||
gtest.Assert(array1.Get(1), 1)
|
||||
gtest.Assert(array1.Get(3), 5)
|
||||
t.Assert(array1.Get(0), 0)
|
||||
t.Assert(array1.Get(1), 1)
|
||||
t.Assert(array1.Get(3), 5)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedIntArray_Remove(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []int{1, 3, 5, 0}
|
||||
array1 := garray.NewSortedIntArrayFrom(a1)
|
||||
|
||||
gtest.Assert(array1.Remove(-1), 0)
|
||||
gtest.Assert(array1.Remove(100000), 0)
|
||||
t.Assert(array1.Remove(-1), 0)
|
||||
t.Assert(array1.Remove(100000), 0)
|
||||
|
||||
i1 := array1.Remove(2)
|
||||
gtest.Assert(i1, 3)
|
||||
gtest.Assert(array1.Search(5), 2)
|
||||
t.Assert(i1, 3)
|
||||
t.Assert(array1.Search(5), 2)
|
||||
|
||||
// 再次删除剩下的数组中的第一个
|
||||
i2 := array1.Remove(0)
|
||||
gtest.Assert(i2, 0)
|
||||
gtest.Assert(array1.Search(5), 1)
|
||||
t.Assert(i2, 0)
|
||||
t.Assert(array1.Search(5), 1)
|
||||
|
||||
a2 := []int{1, 3, 4}
|
||||
array2 := garray.NewSortedIntArrayFrom(a2)
|
||||
i3 := array2.Remove(1)
|
||||
gtest.Assert(array2.Search(1), 0)
|
||||
gtest.Assert(i3, 3)
|
||||
t.Assert(array2.Search(1), 0)
|
||||
t.Assert(i3, 3)
|
||||
i3 = array2.Remove(1)
|
||||
gtest.Assert(array2.Search(4), -1)
|
||||
gtest.Assert(i3, 4)
|
||||
t.Assert(array2.Search(4), -1)
|
||||
t.Assert(i3, 4)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedIntArray_PopLeft(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []int{1, 3, 5, 2}
|
||||
array1 := garray.NewSortedIntArrayFrom(a1)
|
||||
i1 := array1.PopLeft()
|
||||
gtest.Assert(i1, 1)
|
||||
gtest.Assert(array1.Len(), 3)
|
||||
gtest.Assert(array1.Search(1), -1)
|
||||
t.Assert(i1, 1)
|
||||
t.Assert(array1.Len(), 3)
|
||||
t.Assert(array1.Search(1), -1)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedIntArray_PopRight(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []int{1, 3, 5, 2}
|
||||
array1 := garray.NewSortedIntArrayFrom(a1)
|
||||
i1 := array1.PopRight()
|
||||
gtest.Assert(i1, 5)
|
||||
gtest.Assert(array1.Len(), 3)
|
||||
gtest.Assert(array1.Search(5), -1)
|
||||
t.Assert(i1, 5)
|
||||
t.Assert(array1.Len(), 3)
|
||||
t.Assert(array1.Search(5), -1)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedIntArray_PopRand(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []int{1, 3, 5, 2}
|
||||
array1 := garray.NewSortedIntArrayFrom(a1)
|
||||
i1 := array1.PopRand()
|
||||
gtest.Assert(array1.Len(), 3)
|
||||
gtest.Assert(array1.Search(i1), -1)
|
||||
gtest.AssertIN(i1, []int{1, 3, 5, 2})
|
||||
t.Assert(array1.Len(), 3)
|
||||
t.Assert(array1.Search(i1), -1)
|
||||
t.AssertIN(i1, []int{1, 3, 5, 2})
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedIntArray_PopRands(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []int{1, 3, 5, 2}
|
||||
array1 := garray.NewSortedIntArrayFrom(a1)
|
||||
ns1 := array1.PopRands(2)
|
||||
gtest.Assert(array1.Len(), 2)
|
||||
gtest.AssertIN(ns1, []int{1, 3, 5, 2})
|
||||
t.Assert(array1.Len(), 2)
|
||||
t.AssertIN(ns1, []int{1, 3, 5, 2})
|
||||
|
||||
a2 := []int{1, 3, 5, 2}
|
||||
array2 := garray.NewSortedIntArrayFrom(a2)
|
||||
ns2 := array2.PopRands(5)
|
||||
gtest.Assert(array2.Len(), 0)
|
||||
gtest.Assert(len(ns2), 4)
|
||||
gtest.AssertIN(ns2, []int{1, 3, 5, 2})
|
||||
t.Assert(array2.Len(), 0)
|
||||
t.Assert(len(ns2), 4)
|
||||
t.AssertIN(ns2, []int{1, 3, 5, 2})
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedIntArray_Empty(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
array := garray.NewSortedIntArray()
|
||||
t.Assert(array.PopLeft(), 0)
|
||||
t.Assert(array.PopLefts(10), nil)
|
||||
t.Assert(array.PopRight(), 0)
|
||||
t.Assert(array.PopRights(10), nil)
|
||||
t.Assert(array.PopRand(), 0)
|
||||
t.Assert(array.PopRands(10), nil)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedIntArray_PopLefts(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []int{1, 3, 5, 2}
|
||||
array1 := garray.NewSortedIntArrayFrom(a1)
|
||||
ns1 := array1.PopLefts(2)
|
||||
gtest.Assert(array1.Len(), 2)
|
||||
gtest.Assert(ns1, []int{1, 2})
|
||||
t.Assert(array1.Len(), 2)
|
||||
t.Assert(ns1, []int{1, 2})
|
||||
|
||||
a2 := []int{1, 3, 5, 2}
|
||||
array2 := garray.NewSortedIntArrayFrom(a2)
|
||||
ns2 := array2.PopLefts(5)
|
||||
gtest.Assert(array2.Len(), 0)
|
||||
gtest.AssertIN(ns2, []int{1, 3, 5, 2})
|
||||
t.Assert(array2.Len(), 0)
|
||||
t.AssertIN(ns2, []int{1, 3, 5, 2})
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedIntArray_PopRights(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []int{1, 3, 5, 2}
|
||||
array1 := garray.NewSortedIntArrayFrom(a1)
|
||||
ns1 := array1.PopRights(2)
|
||||
gtest.Assert(array1.Len(), 2)
|
||||
gtest.Assert(ns1, []int{3, 5})
|
||||
t.Assert(array1.Len(), 2)
|
||||
t.Assert(ns1, []int{3, 5})
|
||||
|
||||
a2 := []int{1, 3, 5, 2}
|
||||
array2 := garray.NewSortedIntArrayFrom(a2)
|
||||
ns2 := array2.PopRights(5)
|
||||
gtest.Assert(array2.Len(), 0)
|
||||
gtest.AssertIN(ns2, []int{1, 3, 5, 2})
|
||||
t.Assert(array2.Len(), 0)
|
||||
t.AssertIN(ns2, []int{1, 3, 5, 2})
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedIntArray_Range(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []int{1, 3, 5, 2, 6, 7}
|
||||
array1 := garray.NewSortedIntArrayFrom(a1)
|
||||
array2 := garray.NewSortedIntArrayFrom(a1, true)
|
||||
ns1 := array1.Range(1, 4)
|
||||
gtest.Assert(len(ns1), 3)
|
||||
gtest.Assert(ns1, []int{2, 3, 5})
|
||||
t.Assert(len(ns1), 3)
|
||||
t.Assert(ns1, []int{2, 3, 5})
|
||||
|
||||
ns2 := array1.Range(5, 4)
|
||||
gtest.Assert(len(ns2), 0)
|
||||
t.Assert(len(ns2), 0)
|
||||
|
||||
ns3 := array1.Range(-1, 4)
|
||||
gtest.Assert(len(ns3), 4)
|
||||
t.Assert(len(ns3), 4)
|
||||
|
||||
nsl := array1.Range(5, 8)
|
||||
gtest.Assert(len(nsl), 1)
|
||||
gtest.Assert(array2.Range(1, 2), []int{2})
|
||||
t.Assert(len(nsl), 1)
|
||||
t.Assert(array2.Range(1, 2), []int{2})
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedIntArray_Sum(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []int{1, 3, 5}
|
||||
array1 := garray.NewSortedIntArrayFrom(a1)
|
||||
n1 := array1.Sum()
|
||||
gtest.Assert(n1, 9)
|
||||
t.Assert(n1, 9)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedIntArray_Join(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []int{1, 3, 5}
|
||||
array1 := garray.NewSortedIntArrayFrom(a1)
|
||||
gtest.Assert(array1.Join("."), `1.3.5`)
|
||||
t.Assert(array1.Join("."), `1.3.5`)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedIntArray_String(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []int{1, 3, 5}
|
||||
array1 := garray.NewSortedIntArrayFrom(a1)
|
||||
gtest.Assert(array1.String(), `[1,3,5]`)
|
||||
t.Assert(array1.String(), `[1,3,5]`)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedIntArray_Contains(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []int{1, 3, 5}
|
||||
array1 := garray.NewSortedIntArrayFrom(a1)
|
||||
gtest.Assert(array1.Contains(4), false)
|
||||
t.Assert(array1.Contains(4), false)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedIntArray_Clone(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []int{1, 3, 5}
|
||||
array1 := garray.NewSortedIntArrayFrom(a1)
|
||||
array2 := array1.Clone()
|
||||
gtest.Assert(array2.Len(), 3)
|
||||
gtest.Assert(array2, array1)
|
||||
t.Assert(array2.Len(), 3)
|
||||
t.Assert(array2, array1)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedIntArray_Clear(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []int{1, 3, 5}
|
||||
array1 := garray.NewSortedIntArrayFrom(a1)
|
||||
array1.Clear()
|
||||
gtest.Assert(array1.Len(), 0)
|
||||
t.Assert(array1.Len(), 0)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedIntArray_Chunk(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []int{1, 2, 3, 4, 5}
|
||||
array1 := garray.NewSortedIntArrayFrom(a1)
|
||||
ns1 := array1.Chunk(2) //按每几个元素切成一个数组
|
||||
ns2 := array1.Chunk(-1)
|
||||
gtest.Assert(len(ns1), 3)
|
||||
gtest.Assert(ns1[0], []int{1, 2})
|
||||
gtest.Assert(ns1[2], []int{5})
|
||||
gtest.Assert(len(ns2), 0)
|
||||
t.Assert(len(ns1), 3)
|
||||
t.Assert(ns1[0], []int{1, 2})
|
||||
t.Assert(ns1[2], []int{5})
|
||||
t.Assert(len(ns2), 0)
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []int{1, 2, 3, 4, 5}
|
||||
array1 := garray.NewSortedIntArrayFrom(a1)
|
||||
chunks := array1.Chunk(3)
|
||||
gtest.Assert(len(chunks), 2)
|
||||
gtest.Assert(chunks[0], []int{1, 2, 3})
|
||||
gtest.Assert(chunks[1], []int{4, 5})
|
||||
gtest.Assert(array1.Chunk(0), nil)
|
||||
t.Assert(len(chunks), 2)
|
||||
t.Assert(chunks[0], []int{1, 2, 3})
|
||||
t.Assert(chunks[1], []int{4, 5})
|
||||
t.Assert(array1.Chunk(0), nil)
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []int{1, 2, 3, 4, 5, 6}
|
||||
array1 := garray.NewSortedIntArrayFrom(a1)
|
||||
chunks := array1.Chunk(2)
|
||||
gtest.Assert(len(chunks), 3)
|
||||
gtest.Assert(chunks[0], []int{1, 2})
|
||||
gtest.Assert(chunks[1], []int{3, 4})
|
||||
gtest.Assert(chunks[2], []int{5, 6})
|
||||
gtest.Assert(array1.Chunk(0), nil)
|
||||
t.Assert(len(chunks), 3)
|
||||
t.Assert(chunks[0], []int{1, 2})
|
||||
t.Assert(chunks[1], []int{3, 4})
|
||||
t.Assert(chunks[2], []int{5, 6})
|
||||
t.Assert(array1.Chunk(0), nil)
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []int{1, 2, 3, 4, 5, 6}
|
||||
array1 := garray.NewSortedIntArrayFrom(a1)
|
||||
chunks := array1.Chunk(3)
|
||||
gtest.Assert(len(chunks), 2)
|
||||
gtest.Assert(chunks[0], []int{1, 2, 3})
|
||||
gtest.Assert(chunks[1], []int{4, 5, 6})
|
||||
gtest.Assert(array1.Chunk(0), nil)
|
||||
t.Assert(len(chunks), 2)
|
||||
t.Assert(chunks[0], []int{1, 2, 3})
|
||||
t.Assert(chunks[1], []int{4, 5, 6})
|
||||
t.Assert(array1.Chunk(0), nil)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedIntArray_SubSlice(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []int{1, 2, 3, 4, 5}
|
||||
array1 := garray.NewSortedIntArrayFrom(a1)
|
||||
array2 := garray.NewSortedIntArrayFrom(a1, true)
|
||||
ns1 := array1.SubSlice(1, 2)
|
||||
gtest.Assert(len(ns1), 2)
|
||||
gtest.Assert(ns1, []int{2, 3})
|
||||
t.Assert(len(ns1), 2)
|
||||
t.Assert(ns1, []int{2, 3})
|
||||
|
||||
ns2 := array1.SubSlice(7, 2)
|
||||
gtest.Assert(len(ns2), 0)
|
||||
t.Assert(len(ns2), 0)
|
||||
|
||||
ns3 := array1.SubSlice(3, 5)
|
||||
gtest.Assert(len(ns3), 2)
|
||||
gtest.Assert(ns3, []int{4, 5})
|
||||
t.Assert(len(ns3), 2)
|
||||
t.Assert(ns3, []int{4, 5})
|
||||
|
||||
ns4 := array1.SubSlice(3, 1)
|
||||
gtest.Assert(len(ns4), 1)
|
||||
gtest.Assert(ns4, []int{4})
|
||||
gtest.Assert(array1.SubSlice(-1, 1), []int{5})
|
||||
gtest.Assert(array1.SubSlice(-9, 1), nil)
|
||||
gtest.Assert(array1.SubSlice(1, -9), nil)
|
||||
gtest.Assert(array2.SubSlice(1, 2), []int{2, 3})
|
||||
t.Assert(len(ns4), 1)
|
||||
t.Assert(ns4, []int{4})
|
||||
t.Assert(array1.SubSlice(-1, 1), []int{5})
|
||||
t.Assert(array1.SubSlice(-9, 1), nil)
|
||||
t.Assert(array1.SubSlice(1, -9), nil)
|
||||
t.Assert(array2.SubSlice(1, 2), []int{2, 3})
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedIntArray_Rand(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []int{1, 2, 3, 4, 5}
|
||||
array1 := garray.NewSortedIntArrayFrom(a1)
|
||||
ns1 := array1.Rand() //按每几个元素切成一个数组
|
||||
gtest.AssertIN(ns1, a1)
|
||||
t.AssertIN(ns1, a1)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedIntArray_Rands(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []int{1, 2, 3, 4, 5}
|
||||
array1 := garray.NewSortedIntArrayFrom(a1)
|
||||
ns1 := array1.Rands(2) //按每几个元素切成一个数组
|
||||
gtest.AssertIN(ns1, a1)
|
||||
gtest.Assert(len(ns1), 2)
|
||||
t.AssertIN(ns1, a1)
|
||||
t.Assert(len(ns1), 2)
|
||||
|
||||
ns2 := array1.Rands(6) //按每几个元素切成一个数组
|
||||
gtest.AssertIN(ns2, a1)
|
||||
gtest.Assert(len(ns2), 5)
|
||||
t.AssertIN(ns2, a1)
|
||||
t.Assert(len(ns2), 5)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedIntArray_CountValues(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []int{1, 2, 3, 4, 5, 3}
|
||||
array1 := garray.NewSortedIntArrayFrom(a1)
|
||||
ns1 := array1.CountValues() //按每几个元素切成一个数组
|
||||
gtest.Assert(len(ns1), 5)
|
||||
gtest.Assert(ns1[2], 1)
|
||||
gtest.Assert(ns1[3], 2)
|
||||
t.Assert(len(ns1), 5)
|
||||
t.Assert(ns1[2], 1)
|
||||
t.Assert(ns1[3], 2)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedIntArray_SetUnique(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []int{1, 2, 3, 4, 5, 3}
|
||||
array1 := garray.NewSortedIntArrayFrom(a1)
|
||||
array1.SetUnique(true)
|
||||
gtest.Assert(array1.Len(), 5)
|
||||
gtest.Assert(array1, []int{1, 2, 3, 4, 5})
|
||||
t.Assert(array1.Len(), 5)
|
||||
t.Assert(array1, []int{1, 2, 3, 4, 5})
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedIntArray_LockFunc(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s1 := []int{1, 2, 3, 4}
|
||||
a1 := garray.NewSortedIntArrayFrom(s1, true)
|
||||
ch1 := make(chan int64, 3)
|
||||
@ -394,13 +406,13 @@ func TestSortedIntArray_LockFunc(t *testing.T) {
|
||||
<-ch2 //等待go1完成
|
||||
|
||||
// 防止ci抖动,以豪秒为单位
|
||||
gtest.AssertGT(t2-t1, 20) //go1加的读写互斥锁,所go2读的时候被阻塞。
|
||||
gtest.Assert(a1.Contains(6), true)
|
||||
t.AssertGT(t2-t1, 20) //go1加的读写互斥锁,所go2读的时候被阻塞。
|
||||
t.Assert(a1.Contains(6), true)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedIntArray_RLockFunc(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s1 := []int{1, 2, 3, 4}
|
||||
a1 := garray.NewSortedIntArrayFrom(s1, true)
|
||||
|
||||
@ -426,13 +438,13 @@ func TestSortedIntArray_RLockFunc(t *testing.T) {
|
||||
<-ch2 //等待go1完成
|
||||
|
||||
// 防止ci抖动,以豪秒为单位
|
||||
gtest.AssertLT(t2-t1, 20) //go1加的读锁,所go2读的时候,并没有阻塞。
|
||||
gtest.Assert(a1.Contains(6), true)
|
||||
t.AssertLT(t2-t1, 20) //go1加的读锁,所go2读的时候,并没有阻塞。
|
||||
t.Assert(a1.Contains(6), true)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedIntArray_Merge(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
func1 := func(v1, v2 interface{}) int {
|
||||
if gconv.Int(v1) < gconv.Int(v2) {
|
||||
return 0
|
||||
@ -449,37 +461,37 @@ func TestSortedIntArray_Merge(t *testing.T) {
|
||||
s6 := garray.NewSortedIntArrayFrom([]int{1, 2, 3})
|
||||
a1 := garray.NewSortedIntArrayFrom(i0)
|
||||
|
||||
gtest.Assert(a1.Merge(s2).Len(), 6)
|
||||
gtest.Assert(a1.Merge(i1).Len(), 9)
|
||||
gtest.Assert(a1.Merge(i2).Len(), 10)
|
||||
gtest.Assert(a1.Merge(s3).Len(), 12)
|
||||
gtest.Assert(a1.Merge(s4).Len(), 14)
|
||||
gtest.Assert(a1.Merge(s5).Len(), 16)
|
||||
gtest.Assert(a1.Merge(s6).Len(), 19)
|
||||
t.Assert(a1.Merge(s2).Len(), 6)
|
||||
t.Assert(a1.Merge(i1).Len(), 9)
|
||||
t.Assert(a1.Merge(i2).Len(), 10)
|
||||
t.Assert(a1.Merge(s3).Len(), 12)
|
||||
t.Assert(a1.Merge(s4).Len(), 14)
|
||||
t.Assert(a1.Merge(s5).Len(), 16)
|
||||
t.Assert(a1.Merge(s6).Len(), 19)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedIntArray_Json(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s1 := []int{1, 4, 3, 2}
|
||||
s2 := []int{1, 2, 3, 4}
|
||||
a1 := garray.NewSortedIntArrayFrom(s1)
|
||||
b1, err1 := json.Marshal(a1)
|
||||
b2, err2 := json.Marshal(s1)
|
||||
gtest.Assert(b1, b2)
|
||||
gtest.Assert(err1, err2)
|
||||
t.Assert(b1, b2)
|
||||
t.Assert(err1, err2)
|
||||
|
||||
a2 := garray.NewSortedIntArray()
|
||||
err1 = json.Unmarshal(b2, &a2)
|
||||
gtest.Assert(a2.Slice(), s2)
|
||||
t.Assert(a2.Slice(), s2)
|
||||
|
||||
var a3 garray.SortedIntArray
|
||||
err := json.Unmarshal(b2, &a3)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(a3.Slice(), s1)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(a3.Slice(), s1)
|
||||
})
|
||||
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type User struct {
|
||||
Name string
|
||||
Scores *garray.SortedIntArray
|
||||
@ -489,112 +501,112 @@ func TestSortedIntArray_Json(t *testing.T) {
|
||||
"Scores": []int{99, 100, 98},
|
||||
}
|
||||
b, err := json.Marshal(data)
|
||||
gtest.Assert(err, nil)
|
||||
t.Assert(err, nil)
|
||||
|
||||
user := new(User)
|
||||
err = json.Unmarshal(b, user)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(user.Name, data["Name"])
|
||||
gtest.Assert(user.Scores, []int{98, 99, 100})
|
||||
t.Assert(err, nil)
|
||||
t.Assert(user.Name, data["Name"])
|
||||
t.Assert(user.Scores, []int{98, 99, 100})
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedIntArray_Iterator(t *testing.T) {
|
||||
slice := g.SliceInt{10, 20, 30, 40}
|
||||
array := garray.NewSortedIntArrayFrom(slice)
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
array.Iterator(func(k int, v int) bool {
|
||||
gtest.Assert(v, slice[k])
|
||||
t.Assert(v, slice[k])
|
||||
return true
|
||||
})
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
array.IteratorAsc(func(k int, v int) bool {
|
||||
gtest.Assert(v, slice[k])
|
||||
t.Assert(v, slice[k])
|
||||
return true
|
||||
})
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
array.IteratorDesc(func(k int, v int) bool {
|
||||
gtest.Assert(v, slice[k])
|
||||
t.Assert(v, slice[k])
|
||||
return true
|
||||
})
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
index := 0
|
||||
array.Iterator(func(k int, v int) bool {
|
||||
index++
|
||||
return false
|
||||
})
|
||||
gtest.Assert(index, 1)
|
||||
t.Assert(index, 1)
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
index := 0
|
||||
array.IteratorAsc(func(k int, v int) bool {
|
||||
index++
|
||||
return false
|
||||
})
|
||||
gtest.Assert(index, 1)
|
||||
t.Assert(index, 1)
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
index := 0
|
||||
array.IteratorDesc(func(k int, v int) bool {
|
||||
index++
|
||||
return false
|
||||
})
|
||||
gtest.Assert(index, 1)
|
||||
t.Assert(index, 1)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedIntArray_RemoveValue(t *testing.T) {
|
||||
slice := g.SliceInt{10, 20, 30, 40}
|
||||
array := garray.NewSortedIntArrayFrom(slice)
|
||||
gtest.Case(t, func() {
|
||||
gtest.Assert(array.RemoveValue(99), false)
|
||||
gtest.Assert(array.RemoveValue(20), true)
|
||||
gtest.Assert(array.RemoveValue(10), true)
|
||||
gtest.Assert(array.RemoveValue(20), false)
|
||||
gtest.Assert(array.RemoveValue(88), false)
|
||||
gtest.Assert(array.Len(), 2)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
t.Assert(array.RemoveValue(99), false)
|
||||
t.Assert(array.RemoveValue(20), true)
|
||||
t.Assert(array.RemoveValue(10), true)
|
||||
t.Assert(array.RemoveValue(20), false)
|
||||
t.Assert(array.RemoveValue(88), false)
|
||||
t.Assert(array.Len(), 2)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedIntArray_UnmarshalValue(t *testing.T) {
|
||||
type T struct {
|
||||
type V struct {
|
||||
Name string
|
||||
Array *garray.SortedIntArray
|
||||
}
|
||||
// JSON
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var v *V
|
||||
err := gconv.Struct(g.Map{
|
||||
"name": "john",
|
||||
"array": []byte(`[2,3,1]`),
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Array.Slice(), g.Slice{1, 2, 3})
|
||||
}, &v)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(v.Name, "john")
|
||||
t.Assert(v.Array.Slice(), g.Slice{1, 2, 3})
|
||||
})
|
||||
// Map
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var v *V
|
||||
err := gconv.Struct(g.Map{
|
||||
"name": "john",
|
||||
"array": g.Slice{2, 3, 1},
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Array.Slice(), g.Slice{1, 2, 3})
|
||||
}, &v)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(v.Name, "john")
|
||||
t.Assert(v.Array.Slice(), g.Slice{1, 2, 3})
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedIntArray_FilterEmpty(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
array := garray.NewSortedIntArrayFrom(g.SliceInt{0, 1, 2, 3, 4, 0})
|
||||
gtest.Assert(array.FilterEmpty(), g.SliceInt{1, 2, 3, 4})
|
||||
t.Assert(array.FilterEmpty(), g.SliceInt{1, 2, 3, 4})
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
array := garray.NewSortedIntArrayFrom(g.SliceInt{1, 2, 3, 4})
|
||||
gtest.Assert(array.FilterEmpty(), g.SliceInt{1, 2, 3, 4})
|
||||
t.Assert(array.FilterEmpty(), g.SliceInt{1, 2, 3, 4})
|
||||
})
|
||||
}
|
||||
|
||||
@ -20,362 +20,374 @@ import (
|
||||
)
|
||||
|
||||
func TestNewSortedStrArrayFrom(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []string{"a", "d", "c", "b"}
|
||||
s1 := garray.NewSortedStrArrayFrom(a1, true)
|
||||
gtest.Assert(s1, []string{"a", "b", "c", "d"})
|
||||
t.Assert(s1, []string{"a", "b", "c", "d"})
|
||||
s2 := garray.NewSortedStrArrayFrom(a1, false)
|
||||
gtest.Assert(s2, []string{"a", "b", "c", "d"})
|
||||
t.Assert(s2, []string{"a", "b", "c", "d"})
|
||||
})
|
||||
}
|
||||
|
||||
func TestNewSortedStrArrayFromCopy(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []string{"a", "d", "c", "b"}
|
||||
s1 := garray.NewSortedStrArrayFromCopy(a1, true)
|
||||
gtest.Assert(s1.Len(), 4)
|
||||
gtest.Assert(s1, []string{"a", "b", "c", "d"})
|
||||
t.Assert(s1.Len(), 4)
|
||||
t.Assert(s1, []string{"a", "b", "c", "d"})
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedStrArray_SetArray(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []string{"a", "d", "c", "b"}
|
||||
a2 := []string{"f", "g", "h"}
|
||||
array1 := garray.NewSortedStrArrayFrom(a1)
|
||||
array1.SetArray(a2)
|
||||
gtest.Assert(array1.Len(), 3)
|
||||
gtest.Assert(array1.Contains("d"), false)
|
||||
gtest.Assert(array1.Contains("b"), false)
|
||||
gtest.Assert(array1.Contains("g"), true)
|
||||
t.Assert(array1.Len(), 3)
|
||||
t.Assert(array1.Contains("d"), false)
|
||||
t.Assert(array1.Contains("b"), false)
|
||||
t.Assert(array1.Contains("g"), true)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedStrArray_Sort(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []string{"a", "d", "c", "b"}
|
||||
array1 := garray.NewSortedStrArrayFrom(a1)
|
||||
|
||||
gtest.Assert(array1, []string{"a", "b", "c", "d"})
|
||||
t.Assert(array1, []string{"a", "b", "c", "d"})
|
||||
array1.Sort()
|
||||
gtest.Assert(array1.Len(), 4)
|
||||
gtest.Assert(array1.Contains("c"), true)
|
||||
gtest.Assert(array1, []string{"a", "b", "c", "d"})
|
||||
t.Assert(array1.Len(), 4)
|
||||
t.Assert(array1.Contains("c"), true)
|
||||
t.Assert(array1, []string{"a", "b", "c", "d"})
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedStrArray_Get(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []string{"a", "d", "c", "b"}
|
||||
array1 := garray.NewSortedStrArrayFrom(a1)
|
||||
gtest.Assert(array1.Get(2), "c")
|
||||
gtest.Assert(array1.Get(0), "a")
|
||||
t.Assert(array1.Get(2), "c")
|
||||
t.Assert(array1.Get(0), "a")
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedStrArray_Remove(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []string{"a", "d", "c", "b"}
|
||||
array1 := garray.NewSortedStrArrayFrom(a1)
|
||||
|
||||
gtest.Assert(array1.Remove(-1), "")
|
||||
gtest.Assert(array1.Remove(100000), "")
|
||||
t.Assert(array1.Remove(-1), "")
|
||||
t.Assert(array1.Remove(100000), "")
|
||||
|
||||
gtest.Assert(array1.Remove(2), "c")
|
||||
gtest.Assert(array1.Get(2), "d")
|
||||
gtest.Assert(array1.Len(), 3)
|
||||
gtest.Assert(array1.Contains("c"), false)
|
||||
t.Assert(array1.Remove(2), "c")
|
||||
t.Assert(array1.Get(2), "d")
|
||||
t.Assert(array1.Len(), 3)
|
||||
t.Assert(array1.Contains("c"), false)
|
||||
|
||||
gtest.Assert(array1.Remove(0), "a")
|
||||
gtest.Assert(array1.Len(), 2)
|
||||
gtest.Assert(array1.Contains("a"), false)
|
||||
t.Assert(array1.Remove(0), "a")
|
||||
t.Assert(array1.Len(), 2)
|
||||
t.Assert(array1.Contains("a"), false)
|
||||
|
||||
// 此时array1里的元素只剩下2个
|
||||
gtest.Assert(array1.Remove(1), "d")
|
||||
gtest.Assert(array1.Len(), 1)
|
||||
t.Assert(array1.Remove(1), "d")
|
||||
t.Assert(array1.Len(), 1)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedStrArray_PopLeft(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []string{"e", "a", "d", "c", "b"}
|
||||
array1 := garray.NewSortedStrArrayFrom(a1)
|
||||
s1 := array1.PopLeft()
|
||||
gtest.Assert(s1, "a")
|
||||
gtest.Assert(array1.Len(), 4)
|
||||
gtest.Assert(array1.Contains("a"), false)
|
||||
t.Assert(s1, "a")
|
||||
t.Assert(array1.Len(), 4)
|
||||
t.Assert(array1.Contains("a"), false)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedStrArray_PopRight(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []string{"e", "a", "d", "c", "b"}
|
||||
array1 := garray.NewSortedStrArrayFrom(a1)
|
||||
s1 := array1.PopRight()
|
||||
gtest.Assert(s1, "e")
|
||||
gtest.Assert(array1.Len(), 4)
|
||||
gtest.Assert(array1.Contains("e"), false)
|
||||
t.Assert(s1, "e")
|
||||
t.Assert(array1.Len(), 4)
|
||||
t.Assert(array1.Contains("e"), false)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedStrArray_PopRand(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []string{"e", "a", "d", "c", "b"}
|
||||
array1 := garray.NewSortedStrArrayFrom(a1)
|
||||
s1 := array1.PopRand()
|
||||
gtest.AssertIN(s1, []string{"e", "a", "d", "c", "b"})
|
||||
gtest.Assert(array1.Len(), 4)
|
||||
gtest.Assert(array1.Contains(s1), false)
|
||||
t.AssertIN(s1, []string{"e", "a", "d", "c", "b"})
|
||||
t.Assert(array1.Len(), 4)
|
||||
t.Assert(array1.Contains(s1), false)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedStrArray_PopRands(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []string{"e", "a", "d", "c", "b"}
|
||||
array1 := garray.NewSortedStrArrayFrom(a1)
|
||||
s1 := array1.PopRands(2)
|
||||
gtest.AssertIN(s1, []string{"e", "a", "d", "c", "b"})
|
||||
gtest.Assert(array1.Len(), 3)
|
||||
gtest.Assert(len(s1), 2)
|
||||
t.AssertIN(s1, []string{"e", "a", "d", "c", "b"})
|
||||
t.Assert(array1.Len(), 3)
|
||||
t.Assert(len(s1), 2)
|
||||
|
||||
s1 = array1.PopRands(4)
|
||||
gtest.Assert(len(s1), 3)
|
||||
gtest.AssertIN(s1, []string{"e", "a", "d", "c", "b"})
|
||||
t.Assert(len(s1), 3)
|
||||
t.AssertIN(s1, []string{"e", "a", "d", "c", "b"})
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedStrArray_Empty(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
array := garray.NewSortedStrArray()
|
||||
t.Assert(array.PopLeft(), nil)
|
||||
t.Assert(array.PopLefts(10), nil)
|
||||
t.Assert(array.PopRight(), nil)
|
||||
t.Assert(array.PopRights(10), nil)
|
||||
t.Assert(array.PopRand(), nil)
|
||||
t.Assert(array.PopRands(10), nil)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedStrArray_PopLefts(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []string{"e", "a", "d", "c", "b"}
|
||||
array1 := garray.NewSortedStrArrayFrom(a1)
|
||||
s1 := array1.PopLefts(2)
|
||||
gtest.Assert(s1, []string{"a", "b"})
|
||||
gtest.Assert(array1.Len(), 3)
|
||||
gtest.Assert(len(s1), 2)
|
||||
t.Assert(s1, []string{"a", "b"})
|
||||
t.Assert(array1.Len(), 3)
|
||||
t.Assert(len(s1), 2)
|
||||
|
||||
s1 = array1.PopLefts(4)
|
||||
gtest.Assert(len(s1), 3)
|
||||
gtest.Assert(s1, []string{"c", "d", "e"})
|
||||
t.Assert(len(s1), 3)
|
||||
t.Assert(s1, []string{"c", "d", "e"})
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedStrArray_PopRights(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []string{"e", "a", "d", "c", "b", "f", "g"}
|
||||
array1 := garray.NewSortedStrArrayFrom(a1)
|
||||
s1 := array1.PopRights(2)
|
||||
gtest.Assert(s1, []string{"f", "g"})
|
||||
gtest.Assert(array1.Len(), 5)
|
||||
gtest.Assert(len(s1), 2)
|
||||
t.Assert(s1, []string{"f", "g"})
|
||||
t.Assert(array1.Len(), 5)
|
||||
t.Assert(len(s1), 2)
|
||||
s1 = array1.PopRights(6)
|
||||
gtest.Assert(len(s1), 5)
|
||||
gtest.Assert(s1, []string{"a", "b", "c", "d", "e"})
|
||||
gtest.Assert(array1.Len(), 0)
|
||||
t.Assert(len(s1), 5)
|
||||
t.Assert(s1, []string{"a", "b", "c", "d", "e"})
|
||||
t.Assert(array1.Len(), 0)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedStrArray_Range(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []string{"e", "a", "d", "c", "b", "f", "g"}
|
||||
array1 := garray.NewSortedStrArrayFrom(a1)
|
||||
array2 := garray.NewSortedStrArrayFrom(a1, true)
|
||||
s1 := array1.Range(2, 4)
|
||||
gtest.Assert(len(s1), 2)
|
||||
gtest.Assert(s1, []string{"c", "d"})
|
||||
t.Assert(len(s1), 2)
|
||||
t.Assert(s1, []string{"c", "d"})
|
||||
|
||||
s1 = array1.Range(-1, 2)
|
||||
gtest.Assert(len(s1), 2)
|
||||
gtest.Assert(s1, []string{"a", "b"})
|
||||
t.Assert(len(s1), 2)
|
||||
t.Assert(s1, []string{"a", "b"})
|
||||
|
||||
s1 = array1.Range(4, 8)
|
||||
gtest.Assert(len(s1), 3)
|
||||
gtest.Assert(s1, []string{"e", "f", "g"})
|
||||
gtest.Assert(array1.Range(10, 2), nil)
|
||||
t.Assert(len(s1), 3)
|
||||
t.Assert(s1, []string{"e", "f", "g"})
|
||||
t.Assert(array1.Range(10, 2), nil)
|
||||
|
||||
s2 := array2.Range(2, 4)
|
||||
gtest.Assert(s2, []string{"c", "d"})
|
||||
t.Assert(s2, []string{"c", "d"})
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedStrArray_Sum(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []string{"e", "a", "d", "c", "b", "f", "g"}
|
||||
a2 := []string{"1", "2", "3", "4", "a"}
|
||||
array1 := garray.NewSortedStrArrayFrom(a1)
|
||||
array2 := garray.NewSortedStrArrayFrom(a2)
|
||||
gtest.Assert(array1.Sum(), 0)
|
||||
gtest.Assert(array2.Sum(), 10)
|
||||
t.Assert(array1.Sum(), 0)
|
||||
t.Assert(array2.Sum(), 10)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedStrArray_Clone(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []string{"e", "a", "d", "c", "b", "f", "g"}
|
||||
array1 := garray.NewSortedStrArrayFrom(a1)
|
||||
array2 := array1.Clone()
|
||||
gtest.Assert(array1, array2)
|
||||
t.Assert(array1, array2)
|
||||
array1.Remove(1)
|
||||
gtest.Assert(array2.Len(), 7)
|
||||
t.Assert(array2.Len(), 7)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedStrArray_Clear(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []string{"e", "a", "d", "c", "b", "f", "g"}
|
||||
array1 := garray.NewSortedStrArrayFrom(a1)
|
||||
array1.Clear()
|
||||
gtest.Assert(array1.Len(), 0)
|
||||
t.Assert(array1.Len(), 0)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedStrArray_SubSlice(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []string{"e", "a", "d", "c", "b", "f", "g"}
|
||||
array1 := garray.NewSortedStrArrayFrom(a1)
|
||||
array2 := garray.NewSortedStrArrayFrom(a1, true)
|
||||
s1 := array1.SubSlice(1, 3)
|
||||
gtest.Assert(len(s1), 3)
|
||||
gtest.Assert(s1, []string{"b", "c", "d"})
|
||||
gtest.Assert(array1.Len(), 7)
|
||||
t.Assert(len(s1), 3)
|
||||
t.Assert(s1, []string{"b", "c", "d"})
|
||||
t.Assert(array1.Len(), 7)
|
||||
|
||||
s2 := array1.SubSlice(1, 10)
|
||||
gtest.Assert(len(s2), 6)
|
||||
t.Assert(len(s2), 6)
|
||||
|
||||
s3 := array1.SubSlice(10, 2)
|
||||
gtest.Assert(len(s3), 0)
|
||||
t.Assert(len(s3), 0)
|
||||
|
||||
s3 = array1.SubSlice(-5, 2)
|
||||
gtest.Assert(s3, []string{"c", "d"})
|
||||
t.Assert(s3, []string{"c", "d"})
|
||||
|
||||
s3 = array1.SubSlice(-10, 2)
|
||||
gtest.Assert(s3, nil)
|
||||
t.Assert(s3, nil)
|
||||
|
||||
s3 = array1.SubSlice(1, -2)
|
||||
gtest.Assert(s3, nil)
|
||||
t.Assert(s3, nil)
|
||||
|
||||
gtest.Assert(array2.SubSlice(1, 3), []string{"b", "c", "d"})
|
||||
t.Assert(array2.SubSlice(1, 3), []string{"b", "c", "d"})
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedStrArray_Len(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []string{"e", "a", "d", "c", "b", "f", "g"}
|
||||
array1 := garray.NewSortedStrArrayFrom(a1)
|
||||
gtest.Assert(array1.Len(), 7)
|
||||
t.Assert(array1.Len(), 7)
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedStrArray_Rand(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []string{"e", "a", "d"}
|
||||
array1 := garray.NewSortedStrArrayFrom(a1)
|
||||
gtest.AssertIN(array1.Rand(), []string{"e", "a", "d"})
|
||||
t.AssertIN(array1.Rand(), []string{"e", "a", "d"})
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedStrArray_Rands(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []string{"e", "a", "d"}
|
||||
array1 := garray.NewSortedStrArrayFrom(a1)
|
||||
s1 := array1.Rands(2)
|
||||
|
||||
gtest.AssertIN(s1, []string{"e", "a", "d"})
|
||||
gtest.Assert(len(s1), 2)
|
||||
t.AssertIN(s1, []string{"e", "a", "d"})
|
||||
t.Assert(len(s1), 2)
|
||||
|
||||
s1 = array1.Rands(4)
|
||||
gtest.AssertIN(s1, []string{"e", "a", "d"})
|
||||
gtest.Assert(len(s1), 3)
|
||||
t.AssertIN(s1, []string{"e", "a", "d"})
|
||||
t.Assert(len(s1), 3)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedStrArray_Join(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []string{"e", "a", "d"}
|
||||
array1 := garray.NewSortedStrArrayFrom(a1)
|
||||
gtest.Assert(array1.Join(","), `a,d,e`)
|
||||
gtest.Assert(array1.Join("."), `a.d.e`)
|
||||
t.Assert(array1.Join(","), `a,d,e`)
|
||||
t.Assert(array1.Join("."), `a.d.e`)
|
||||
})
|
||||
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []string{"a", `"b"`, `\c`}
|
||||
array1 := garray.NewSortedStrArrayFrom(a1)
|
||||
gtest.Assert(array1.Join("."), `"b".\c.a`)
|
||||
t.Assert(array1.Join("."), `"b".\c.a`)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedStrArray_String(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []string{"e", "a", "d"}
|
||||
array1 := garray.NewSortedStrArrayFrom(a1)
|
||||
gtest.Assert(array1.String(), `["a","d","e"]`)
|
||||
t.Assert(array1.String(), `["a","d","e"]`)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedStrArray_CountValues(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []string{"e", "a", "d", "a", "c"}
|
||||
array1 := garray.NewSortedStrArrayFrom(a1)
|
||||
m1 := array1.CountValues()
|
||||
gtest.Assert(m1["a"], 2)
|
||||
gtest.Assert(m1["d"], 1)
|
||||
t.Assert(m1["a"], 2)
|
||||
t.Assert(m1["d"], 1)
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedStrArray_Chunk(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []string{"e", "a", "d", "a", "c"}
|
||||
array1 := garray.NewSortedStrArrayFrom(a1)
|
||||
array2 := array1.Chunk(2)
|
||||
gtest.Assert(len(array2), 3)
|
||||
gtest.Assert(len(array2[0]), 2)
|
||||
gtest.Assert(array2[1], []string{"c", "d"})
|
||||
gtest.Assert(array1.Chunk(0), nil)
|
||||
t.Assert(len(array2), 3)
|
||||
t.Assert(len(array2[0]), 2)
|
||||
t.Assert(array2[1], []string{"c", "d"})
|
||||
t.Assert(array1.Chunk(0), nil)
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []string{"1", "2", "3", "4", "5"}
|
||||
array1 := garray.NewSortedStrArrayFrom(a1)
|
||||
chunks := array1.Chunk(3)
|
||||
gtest.Assert(len(chunks), 2)
|
||||
gtest.Assert(chunks[0], []string{"1", "2", "3"})
|
||||
gtest.Assert(chunks[1], []string{"4", "5"})
|
||||
gtest.Assert(array1.Chunk(0), nil)
|
||||
t.Assert(len(chunks), 2)
|
||||
t.Assert(chunks[0], []string{"1", "2", "3"})
|
||||
t.Assert(chunks[1], []string{"4", "5"})
|
||||
t.Assert(array1.Chunk(0), nil)
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []string{"1", "2", "3", "4", "5", "6"}
|
||||
array1 := garray.NewSortedStrArrayFrom(a1)
|
||||
chunks := array1.Chunk(2)
|
||||
gtest.Assert(len(chunks), 3)
|
||||
gtest.Assert(chunks[0], []string{"1", "2"})
|
||||
gtest.Assert(chunks[1], []string{"3", "4"})
|
||||
gtest.Assert(chunks[2], []string{"5", "6"})
|
||||
gtest.Assert(array1.Chunk(0), nil)
|
||||
t.Assert(len(chunks), 3)
|
||||
t.Assert(chunks[0], []string{"1", "2"})
|
||||
t.Assert(chunks[1], []string{"3", "4"})
|
||||
t.Assert(chunks[2], []string{"5", "6"})
|
||||
t.Assert(array1.Chunk(0), nil)
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []string{"1", "2", "3", "4", "5", "6"}
|
||||
array1 := garray.NewSortedStrArrayFrom(a1)
|
||||
chunks := array1.Chunk(3)
|
||||
gtest.Assert(len(chunks), 2)
|
||||
gtest.Assert(chunks[0], []string{"1", "2", "3"})
|
||||
gtest.Assert(chunks[1], []string{"4", "5", "6"})
|
||||
gtest.Assert(array1.Chunk(0), nil)
|
||||
t.Assert(len(chunks), 2)
|
||||
t.Assert(chunks[0], []string{"1", "2", "3"})
|
||||
t.Assert(chunks[1], []string{"4", "5", "6"})
|
||||
t.Assert(array1.Chunk(0), nil)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedStrArray_SetUnique(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []string{"e", "a", "d", "a", "c"}
|
||||
array1 := garray.NewSortedStrArrayFrom(a1)
|
||||
array2 := array1.SetUnique(true)
|
||||
gtest.Assert(array2.Len(), 4)
|
||||
gtest.Assert(array2, []string{"a", "c", "d", "e"})
|
||||
t.Assert(array2.Len(), 4)
|
||||
t.Assert(array2, []string{"a", "c", "d", "e"})
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedStrArray_LockFunc(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s1 := []string{"a", "b", "c", "d"}
|
||||
a1 := garray.NewSortedStrArrayFrom(s1, true)
|
||||
|
||||
@ -401,13 +413,13 @@ func TestSortedStrArray_LockFunc(t *testing.T) {
|
||||
<-ch2 //等待go1完成
|
||||
|
||||
// 防止ci抖动,以豪秒为单位
|
||||
gtest.AssertGT(t2-t1, 20) //go1加的读写互斥锁,所go2读的时候被阻塞。
|
||||
gtest.Assert(a1.Contains("g"), true)
|
||||
t.AssertGT(t2-t1, 20) //go1加的读写互斥锁,所go2读的时候被阻塞。
|
||||
t.Assert(a1.Contains("g"), true)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedStrArray_RLockFunc(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s1 := []string{"a", "b", "c", "d"}
|
||||
a1 := garray.NewSortedStrArrayFrom(s1, true)
|
||||
|
||||
@ -433,13 +445,13 @@ func TestSortedStrArray_RLockFunc(t *testing.T) {
|
||||
<-ch2 //等待go1完成
|
||||
|
||||
// 防止ci抖动,以豪秒为单位
|
||||
gtest.AssertLT(t2-t1, 20) //go1加的读锁,所go2读的时候,并没有阻塞。
|
||||
gtest.Assert(a1.Contains("g"), true)
|
||||
t.AssertLT(t2-t1, 20) //go1加的读锁,所go2读的时候,并没有阻塞。
|
||||
t.Assert(a1.Contains("g"), true)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedStrArray_Merge(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
func1 := func(v1, v2 interface{}) int {
|
||||
if gconv.Int(v1) < gconv.Int(v2) {
|
||||
return 0
|
||||
@ -457,39 +469,39 @@ func TestSortedStrArray_Merge(t *testing.T) {
|
||||
s6 := garray.NewSortedIntArrayFrom([]int{1, 2, 3})
|
||||
a1 := garray.NewSortedStrArrayFrom(s1)
|
||||
|
||||
gtest.Assert(a1.Merge(s2).Len(), 6)
|
||||
gtest.Assert(a1.Merge(i1).Len(), 9)
|
||||
gtest.Assert(a1.Merge(i2).Len(), 10)
|
||||
gtest.Assert(a1.Merge(s3).Len(), 12)
|
||||
gtest.Assert(a1.Merge(s4).Len(), 14)
|
||||
gtest.Assert(a1.Merge(s5).Len(), 16)
|
||||
gtest.Assert(a1.Merge(s6).Len(), 19)
|
||||
t.Assert(a1.Merge(s2).Len(), 6)
|
||||
t.Assert(a1.Merge(i1).Len(), 9)
|
||||
t.Assert(a1.Merge(i2).Len(), 10)
|
||||
t.Assert(a1.Merge(s3).Len(), 12)
|
||||
t.Assert(a1.Merge(s4).Len(), 14)
|
||||
t.Assert(a1.Merge(s5).Len(), 16)
|
||||
t.Assert(a1.Merge(s6).Len(), 19)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedStrArray_Json(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s1 := []string{"a", "b", "d", "c"}
|
||||
s2 := []string{"a", "b", "c", "d"}
|
||||
a1 := garray.NewSortedStrArrayFrom(s1)
|
||||
b1, err1 := json.Marshal(a1)
|
||||
b2, err2 := json.Marshal(s1)
|
||||
gtest.Assert(b1, b2)
|
||||
gtest.Assert(err1, err2)
|
||||
t.Assert(b1, b2)
|
||||
t.Assert(err1, err2)
|
||||
|
||||
a2 := garray.NewSortedStrArray()
|
||||
err1 = json.Unmarshal(b2, &a2)
|
||||
gtest.Assert(a2.Slice(), s2)
|
||||
gtest.Assert(a2.Interfaces(), s2)
|
||||
t.Assert(a2.Slice(), s2)
|
||||
t.Assert(a2.Interfaces(), s2)
|
||||
|
||||
var a3 garray.SortedStrArray
|
||||
err := json.Unmarshal(b2, &a3)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(a3.Slice(), s1)
|
||||
gtest.Assert(a3.Interfaces(), s1)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(a3.Slice(), s1)
|
||||
t.Assert(a3.Interfaces(), s1)
|
||||
})
|
||||
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type User struct {
|
||||
Name string
|
||||
Scores *garray.SortedStrArray
|
||||
@ -499,111 +511,111 @@ func TestSortedStrArray_Json(t *testing.T) {
|
||||
"Scores": []string{"A+", "A", "A"},
|
||||
}
|
||||
b, err := json.Marshal(data)
|
||||
gtest.Assert(err, nil)
|
||||
t.Assert(err, nil)
|
||||
|
||||
user := new(User)
|
||||
err = json.Unmarshal(b, user)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(user.Name, data["Name"])
|
||||
gtest.Assert(user.Scores, []string{"A", "A", "A+"})
|
||||
t.Assert(err, nil)
|
||||
t.Assert(user.Name, data["Name"])
|
||||
t.Assert(user.Scores, []string{"A", "A", "A+"})
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedStrArray_Iterator(t *testing.T) {
|
||||
slice := g.SliceStr{"a", "b", "d", "c"}
|
||||
array := garray.NewSortedStrArrayFrom(slice)
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
array.Iterator(func(k int, v string) bool {
|
||||
gtest.Assert(v, slice[k])
|
||||
t.Assert(v, slice[k])
|
||||
return true
|
||||
})
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
array.IteratorAsc(func(k int, v string) bool {
|
||||
gtest.Assert(v, slice[k])
|
||||
t.Assert(v, slice[k])
|
||||
return true
|
||||
})
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
array.IteratorDesc(func(k int, v string) bool {
|
||||
gtest.Assert(v, slice[k])
|
||||
t.Assert(v, slice[k])
|
||||
return true
|
||||
})
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
index := 0
|
||||
array.Iterator(func(k int, v string) bool {
|
||||
index++
|
||||
return false
|
||||
})
|
||||
gtest.Assert(index, 1)
|
||||
t.Assert(index, 1)
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
index := 0
|
||||
array.IteratorAsc(func(k int, v string) bool {
|
||||
index++
|
||||
return false
|
||||
})
|
||||
gtest.Assert(index, 1)
|
||||
t.Assert(index, 1)
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
index := 0
|
||||
array.IteratorDesc(func(k int, v string) bool {
|
||||
index++
|
||||
return false
|
||||
})
|
||||
gtest.Assert(index, 1)
|
||||
t.Assert(index, 1)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedStrArray_RemoveValue(t *testing.T) {
|
||||
slice := g.SliceStr{"a", "b", "d", "c"}
|
||||
array := garray.NewSortedStrArrayFrom(slice)
|
||||
gtest.Case(t, func() {
|
||||
gtest.Assert(array.RemoveValue("e"), false)
|
||||
gtest.Assert(array.RemoveValue("b"), true)
|
||||
gtest.Assert(array.RemoveValue("a"), true)
|
||||
gtest.Assert(array.RemoveValue("c"), true)
|
||||
gtest.Assert(array.RemoveValue("f"), false)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
t.Assert(array.RemoveValue("e"), false)
|
||||
t.Assert(array.RemoveValue("b"), true)
|
||||
t.Assert(array.RemoveValue("a"), true)
|
||||
t.Assert(array.RemoveValue("c"), true)
|
||||
t.Assert(array.RemoveValue("f"), false)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedStrArray_UnmarshalValue(t *testing.T) {
|
||||
type T struct {
|
||||
type V struct {
|
||||
Name string
|
||||
Array *garray.SortedStrArray
|
||||
}
|
||||
// JSON
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var v *V
|
||||
err := gconv.Struct(g.Map{
|
||||
"name": "john",
|
||||
"array": []byte(`["1","3","2"]`),
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Array.Slice(), g.SliceStr{"1", "2", "3"})
|
||||
}, &v)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(v.Name, "john")
|
||||
t.Assert(v.Array.Slice(), g.SliceStr{"1", "2", "3"})
|
||||
})
|
||||
// Map
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var v *V
|
||||
err := gconv.Struct(g.Map{
|
||||
"name": "john",
|
||||
"array": g.SliceStr{"1", "3", "2"},
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Array.Slice(), g.SliceStr{"1", "2", "3"})
|
||||
}, &v)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(v.Name, "john")
|
||||
t.Assert(v.Array.Slice(), g.SliceStr{"1", "2", "3"})
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedStrArray_FilterEmpty(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
array := garray.NewSortedStrArrayFrom(g.SliceStr{"", "1", "2", "0"})
|
||||
gtest.Assert(array.FilterEmpty(), g.SliceStr{"0", "1", "2"})
|
||||
t.Assert(array.FilterEmpty(), g.SliceStr{"0", "1", "2"})
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
array := garray.NewSortedStrArrayFrom(g.SliceStr{"1", "2"})
|
||||
gtest.Assert(array.FilterEmpty(), g.SliceStr{"1", "2"})
|
||||
t.Assert(array.FilterEmpty(), g.SliceStr{"1", "2"})
|
||||
})
|
||||
}
|
||||
|
||||
@ -9,18 +9,18 @@ import (
|
||||
)
|
||||
|
||||
func Test_Gchan(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
ch := gchan.New(10)
|
||||
|
||||
gtest.Assert(ch.Cap(), 10)
|
||||
gtest.Assert(ch.Push(1), nil)
|
||||
gtest.Assert(ch.Len(), 1)
|
||||
gtest.Assert(ch.Size(), 1)
|
||||
t.Assert(ch.Cap(), 10)
|
||||
t.Assert(ch.Push(1), nil)
|
||||
t.Assert(ch.Len(), 1)
|
||||
t.Assert(ch.Size(), 1)
|
||||
ch.Pop()
|
||||
gtest.Assert(ch.Len(), 0)
|
||||
gtest.Assert(ch.Size(), 0)
|
||||
t.Assert(ch.Len(), 0)
|
||||
t.Assert(ch.Size(), 0)
|
||||
ch.Close()
|
||||
gtest.Assert(ch.Push(1), errors.New("channel is closed"))
|
||||
t.Assert(ch.Push(1), errors.New("channel is closed"))
|
||||
|
||||
ch = gchan.New(0)
|
||||
ch1 := gchan.New(0)
|
||||
@ -32,7 +32,7 @@ func Test_Gchan(t *testing.T) {
|
||||
ch1.Push(i)
|
||||
break
|
||||
}
|
||||
gtest.Assert(v, i)
|
||||
t.Assert(v, i)
|
||||
i++
|
||||
}
|
||||
}()
|
||||
@ -41,7 +41,7 @@ func Test_Gchan(t *testing.T) {
|
||||
ch.Push(index)
|
||||
}
|
||||
ch.Close()
|
||||
gtest.Assert(ch1.Pop(), 10)
|
||||
t.Assert(ch1.Pop(), 10)
|
||||
ch1.Close()
|
||||
})
|
||||
}
|
||||
|
||||
@ -16,7 +16,7 @@ import (
|
||||
)
|
||||
|
||||
// 检查链表长度
|
||||
func checkListLen(t *testing.T, l *List, len int) bool {
|
||||
func checkListLen(t *gtest.T, l *List, len int) bool {
|
||||
if n := l.Len(); n != len {
|
||||
t.Errorf("l.Len() = %d, want %d", n, len)
|
||||
return false
|
||||
@ -25,7 +25,7 @@ func checkListLen(t *testing.T, l *List, len int) bool {
|
||||
}
|
||||
|
||||
// 检查指针地址
|
||||
func checkListPointers(t *testing.T, l *List, es []*Element) {
|
||||
func checkListPointers(t *gtest.T, l *List, es []*Element) {
|
||||
if !checkListLen(t, l, len(es)) {
|
||||
return
|
||||
}
|
||||
@ -80,87 +80,89 @@ func TestBasic(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestList(t *testing.T) {
|
||||
l := New()
|
||||
checkListPointers(t, l, []*Element{})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
l := New()
|
||||
checkListPointers(t, l, []*Element{})
|
||||
|
||||
// Single element list
|
||||
e := l.PushFront("a")
|
||||
checkListPointers(t, l, []*Element{e})
|
||||
l.MoveToFront(e)
|
||||
checkListPointers(t, l, []*Element{e})
|
||||
l.MoveToBack(e)
|
||||
checkListPointers(t, l, []*Element{e})
|
||||
l.Remove(e)
|
||||
checkListPointers(t, l, []*Element{})
|
||||
|
||||
// Bigger list
|
||||
e2 := l.PushFront(2)
|
||||
e1 := l.PushFront(1)
|
||||
e3 := l.PushBack(3)
|
||||
e4 := l.PushBack("banana")
|
||||
checkListPointers(t, l, []*Element{e1, e2, e3, e4})
|
||||
|
||||
l.Remove(e2)
|
||||
checkListPointers(t, l, []*Element{e1, e3, e4})
|
||||
|
||||
l.MoveToFront(e3) // move from middle
|
||||
checkListPointers(t, l, []*Element{e3, e1, e4})
|
||||
|
||||
l.MoveToFront(e1)
|
||||
l.MoveToBack(e3) // move from middle
|
||||
checkListPointers(t, l, []*Element{e1, e4, e3})
|
||||
|
||||
l.MoveToFront(e3) // move from back
|
||||
checkListPointers(t, l, []*Element{e3, e1, e4})
|
||||
l.MoveToFront(e3) // should be no-op
|
||||
checkListPointers(t, l, []*Element{e3, e1, e4})
|
||||
|
||||
l.MoveToBack(e3) // move from front
|
||||
checkListPointers(t, l, []*Element{e1, e4, e3})
|
||||
l.MoveToBack(e3) // should be no-op
|
||||
checkListPointers(t, l, []*Element{e1, e4, e3})
|
||||
|
||||
e2 = l.InsertBefore(e1, 2) // insert before front
|
||||
checkListPointers(t, l, []*Element{e2, e1, e4, e3})
|
||||
l.Remove(e2)
|
||||
e2 = l.InsertBefore(e4, 2) // insert before middle
|
||||
checkListPointers(t, l, []*Element{e1, e2, e4, e3})
|
||||
l.Remove(e2)
|
||||
e2 = l.InsertBefore(e3, 2) // insert before back
|
||||
checkListPointers(t, l, []*Element{e1, e4, e2, e3})
|
||||
l.Remove(e2)
|
||||
|
||||
e2 = l.InsertAfter(e1, 2) // insert after front
|
||||
checkListPointers(t, l, []*Element{e1, e2, e4, e3})
|
||||
l.Remove(e2)
|
||||
e2 = l.InsertAfter(e4, 2) // insert after middle
|
||||
checkListPointers(t, l, []*Element{e1, e4, e2, e3})
|
||||
l.Remove(e2)
|
||||
e2 = l.InsertAfter(e3, 2) // insert after back
|
||||
checkListPointers(t, l, []*Element{e1, e4, e3, e2})
|
||||
l.Remove(e2)
|
||||
|
||||
// Check standard iteration.
|
||||
sum := 0
|
||||
for e := l.Front(); e != nil; e = e.Next() {
|
||||
if i, ok := e.Value.(int); ok {
|
||||
sum += i
|
||||
}
|
||||
}
|
||||
if sum != 4 {
|
||||
t.Errorf("sum over l = %d, want 4", sum)
|
||||
}
|
||||
|
||||
// Clear all elements by iterating
|
||||
var next *Element
|
||||
for e := l.Front(); e != nil; e = next {
|
||||
next = e.Next()
|
||||
// Single element list
|
||||
e := l.PushFront("a")
|
||||
checkListPointers(t, l, []*Element{e})
|
||||
l.MoveToFront(e)
|
||||
checkListPointers(t, l, []*Element{e})
|
||||
l.MoveToBack(e)
|
||||
checkListPointers(t, l, []*Element{e})
|
||||
l.Remove(e)
|
||||
}
|
||||
checkListPointers(t, l, []*Element{})
|
||||
checkListPointers(t, l, []*Element{})
|
||||
|
||||
// Bigger list
|
||||
e2 := l.PushFront(2)
|
||||
e1 := l.PushFront(1)
|
||||
e3 := l.PushBack(3)
|
||||
e4 := l.PushBack("banana")
|
||||
checkListPointers(t, l, []*Element{e1, e2, e3, e4})
|
||||
|
||||
l.Remove(e2)
|
||||
checkListPointers(t, l, []*Element{e1, e3, e4})
|
||||
|
||||
l.MoveToFront(e3) // move from middle
|
||||
checkListPointers(t, l, []*Element{e3, e1, e4})
|
||||
|
||||
l.MoveToFront(e1)
|
||||
l.MoveToBack(e3) // move from middle
|
||||
checkListPointers(t, l, []*Element{e1, e4, e3})
|
||||
|
||||
l.MoveToFront(e3) // move from back
|
||||
checkListPointers(t, l, []*Element{e3, e1, e4})
|
||||
l.MoveToFront(e3) // should be no-op
|
||||
checkListPointers(t, l, []*Element{e3, e1, e4})
|
||||
|
||||
l.MoveToBack(e3) // move from front
|
||||
checkListPointers(t, l, []*Element{e1, e4, e3})
|
||||
l.MoveToBack(e3) // should be no-op
|
||||
checkListPointers(t, l, []*Element{e1, e4, e3})
|
||||
|
||||
e2 = l.InsertBefore(e1, 2) // insert before front
|
||||
checkListPointers(t, l, []*Element{e2, e1, e4, e3})
|
||||
l.Remove(e2)
|
||||
e2 = l.InsertBefore(e4, 2) // insert before middle
|
||||
checkListPointers(t, l, []*Element{e1, e2, e4, e3})
|
||||
l.Remove(e2)
|
||||
e2 = l.InsertBefore(e3, 2) // insert before back
|
||||
checkListPointers(t, l, []*Element{e1, e4, e2, e3})
|
||||
l.Remove(e2)
|
||||
|
||||
e2 = l.InsertAfter(e1, 2) // insert after front
|
||||
checkListPointers(t, l, []*Element{e1, e2, e4, e3})
|
||||
l.Remove(e2)
|
||||
e2 = l.InsertAfter(e4, 2) // insert after middle
|
||||
checkListPointers(t, l, []*Element{e1, e4, e2, e3})
|
||||
l.Remove(e2)
|
||||
e2 = l.InsertAfter(e3, 2) // insert after back
|
||||
checkListPointers(t, l, []*Element{e1, e4, e3, e2})
|
||||
l.Remove(e2)
|
||||
|
||||
// Check standard iteration.
|
||||
sum := 0
|
||||
for e := l.Front(); e != nil; e = e.Next() {
|
||||
if i, ok := e.Value.(int); ok {
|
||||
sum += i
|
||||
}
|
||||
}
|
||||
if sum != 4 {
|
||||
t.Errorf("sum over l = %d, want 4", sum)
|
||||
}
|
||||
|
||||
// Clear all elements by iterating
|
||||
var next *Element
|
||||
for e := l.Front(); e != nil; e = next {
|
||||
next = e.Next()
|
||||
l.Remove(e)
|
||||
}
|
||||
checkListPointers(t, l, []*Element{})
|
||||
})
|
||||
}
|
||||
|
||||
func checkList(t *testing.T, l *List, es []interface{}) {
|
||||
func checkList(t *gtest.T, l *List, es []interface{}) {
|
||||
if !checkListLen(t, l, len(es)) {
|
||||
return
|
||||
}
|
||||
@ -193,60 +195,64 @@ func checkList(t *testing.T, l *List, es []interface{}) {
|
||||
}
|
||||
|
||||
func TestExtending(t *testing.T) {
|
||||
l1 := New()
|
||||
l2 := New()
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
l1 := New()
|
||||
l2 := New()
|
||||
|
||||
l1.PushBack(1)
|
||||
l1.PushBack(2)
|
||||
l1.PushBack(3)
|
||||
l1.PushBack(1)
|
||||
l1.PushBack(2)
|
||||
l1.PushBack(3)
|
||||
|
||||
l2.PushBack(4)
|
||||
l2.PushBack(5)
|
||||
l2.PushBack(4)
|
||||
l2.PushBack(5)
|
||||
|
||||
l3 := New()
|
||||
l3.PushBackList(l1)
|
||||
checkList(t, l3, []interface{}{1, 2, 3})
|
||||
l3.PushBackList(l2)
|
||||
checkList(t, l3, []interface{}{1, 2, 3, 4, 5})
|
||||
l3 := New()
|
||||
l3.PushBackList(l1)
|
||||
checkList(t, l3, []interface{}{1, 2, 3})
|
||||
l3.PushBackList(l2)
|
||||
checkList(t, l3, []interface{}{1, 2, 3, 4, 5})
|
||||
|
||||
l3 = New()
|
||||
l3.PushFrontList(l2)
|
||||
checkList(t, l3, []interface{}{4, 5})
|
||||
l3.PushFrontList(l1)
|
||||
checkList(t, l3, []interface{}{1, 2, 3, 4, 5})
|
||||
l3 = New()
|
||||
l3.PushFrontList(l2)
|
||||
checkList(t, l3, []interface{}{4, 5})
|
||||
l3.PushFrontList(l1)
|
||||
checkList(t, l3, []interface{}{1, 2, 3, 4, 5})
|
||||
|
||||
checkList(t, l1, []interface{}{1, 2, 3})
|
||||
checkList(t, l2, []interface{}{4, 5})
|
||||
checkList(t, l1, []interface{}{1, 2, 3})
|
||||
checkList(t, l2, []interface{}{4, 5})
|
||||
|
||||
l3 = New()
|
||||
l3.PushBackList(l1)
|
||||
checkList(t, l3, []interface{}{1, 2, 3})
|
||||
l3.PushBackList(l3)
|
||||
checkList(t, l3, []interface{}{1, 2, 3, 1, 2, 3})
|
||||
l3 = New()
|
||||
l3.PushBackList(l1)
|
||||
checkList(t, l3, []interface{}{1, 2, 3})
|
||||
l3.PushBackList(l3)
|
||||
checkList(t, l3, []interface{}{1, 2, 3, 1, 2, 3})
|
||||
|
||||
l3 = New()
|
||||
l3.PushFrontList(l1)
|
||||
checkList(t, l3, []interface{}{1, 2, 3})
|
||||
l3.PushFrontList(l3)
|
||||
checkList(t, l3, []interface{}{1, 2, 3, 1, 2, 3})
|
||||
l3 = New()
|
||||
l3.PushFrontList(l1)
|
||||
checkList(t, l3, []interface{}{1, 2, 3})
|
||||
l3.PushFrontList(l3)
|
||||
checkList(t, l3, []interface{}{1, 2, 3, 1, 2, 3})
|
||||
|
||||
l3 = New()
|
||||
l1.PushBackList(l3)
|
||||
checkList(t, l1, []interface{}{1, 2, 3})
|
||||
l1.PushFrontList(l3)
|
||||
checkList(t, l1, []interface{}{1, 2, 3})
|
||||
l3 = New()
|
||||
l1.PushBackList(l3)
|
||||
checkList(t, l1, []interface{}{1, 2, 3})
|
||||
l1.PushFrontList(l3)
|
||||
checkList(t, l1, []interface{}{1, 2, 3})
|
||||
})
|
||||
}
|
||||
|
||||
func TestRemove(t *testing.T) {
|
||||
l := New()
|
||||
e1 := l.PushBack(1)
|
||||
e2 := l.PushBack(2)
|
||||
checkListPointers(t, l, []*Element{e1, e2})
|
||||
//e := l.Front()
|
||||
//l.Remove(e)
|
||||
//checkListPointers(t, l, []*Element{e2})
|
||||
//l.Remove(e)
|
||||
//checkListPointers(t, l, []*Element{e2})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
l := New()
|
||||
e1 := l.PushBack(1)
|
||||
e2 := l.PushBack(2)
|
||||
checkListPointers(t, l, []*Element{e1, e2})
|
||||
//e := l.Front()
|
||||
//l.Remove(e)
|
||||
//checkListPointers(t, l, []*Element{e2})
|
||||
//l.Remove(e)
|
||||
//checkListPointers(t, l, []*Element{e2})
|
||||
})
|
||||
}
|
||||
|
||||
func TestIssue4103(t *testing.T) {
|
||||
@ -289,375 +295,416 @@ func TestIssue6349(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestMove(t *testing.T) {
|
||||
l := New()
|
||||
e1 := l.PushBack(1)
|
||||
e2 := l.PushBack(2)
|
||||
e3 := l.PushBack(3)
|
||||
e4 := l.PushBack(4)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
l := New()
|
||||
e1 := l.PushBack(1)
|
||||
e2 := l.PushBack(2)
|
||||
e3 := l.PushBack(3)
|
||||
e4 := l.PushBack(4)
|
||||
|
||||
l.MoveAfter(e3, e3)
|
||||
checkListPointers(t, l, []*Element{e1, e2, e3, e4})
|
||||
l.MoveBefore(e2, e2)
|
||||
checkListPointers(t, l, []*Element{e1, e2, e3, e4})
|
||||
l.MoveAfter(e3, e3)
|
||||
checkListPointers(t, l, []*Element{e1, e2, e3, e4})
|
||||
l.MoveBefore(e2, e2)
|
||||
checkListPointers(t, l, []*Element{e1, e2, e3, e4})
|
||||
|
||||
l.MoveAfter(e3, e2)
|
||||
checkListPointers(t, l, []*Element{e1, e2, e3, e4})
|
||||
l.MoveBefore(e2, e3)
|
||||
checkListPointers(t, l, []*Element{e1, e2, e3, e4})
|
||||
l.MoveAfter(e3, e2)
|
||||
checkListPointers(t, l, []*Element{e1, e2, e3, e4})
|
||||
l.MoveBefore(e2, e3)
|
||||
checkListPointers(t, l, []*Element{e1, e2, e3, e4})
|
||||
|
||||
l.MoveBefore(e2, e4)
|
||||
checkListPointers(t, l, []*Element{e1, e3, e2, e4})
|
||||
e2, e3 = e3, e2
|
||||
l.MoveBefore(e2, e4)
|
||||
checkListPointers(t, l, []*Element{e1, e3, e2, e4})
|
||||
e2, e3 = e3, e2
|
||||
|
||||
l.MoveBefore(e4, e1)
|
||||
checkListPointers(t, l, []*Element{e4, e1, e2, e3})
|
||||
e1, e2, e3, e4 = e4, e1, e2, e3
|
||||
l.MoveBefore(e4, e1)
|
||||
checkListPointers(t, l, []*Element{e4, e1, e2, e3})
|
||||
e1, e2, e3, e4 = e4, e1, e2, e3
|
||||
|
||||
l.MoveAfter(e4, e1)
|
||||
checkListPointers(t, l, []*Element{e1, e4, e2, e3})
|
||||
e2, e3, e4 = e4, e2, e3
|
||||
l.MoveAfter(e4, e1)
|
||||
checkListPointers(t, l, []*Element{e1, e4, e2, e3})
|
||||
e2, e3, e4 = e4, e2, e3
|
||||
|
||||
l.MoveAfter(e2, e3)
|
||||
checkListPointers(t, l, []*Element{e1, e3, e2, e4})
|
||||
e2, e3 = e3, e2
|
||||
l.MoveAfter(e2, e3)
|
||||
checkListPointers(t, l, []*Element{e1, e3, e2, e4})
|
||||
e2, e3 = e3, e2
|
||||
})
|
||||
}
|
||||
|
||||
// Test PushFront, PushBack, PushFrontList, PushBackList with uninitialized List
|
||||
func TestZeroList(t *testing.T) {
|
||||
var l1 = New()
|
||||
l1.PushFront(1)
|
||||
checkList(t, l1, []interface{}{1})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var l1 = New()
|
||||
l1.PushFront(1)
|
||||
checkList(t, l1, []interface{}{1})
|
||||
|
||||
var l2 = New()
|
||||
l2.PushBack(1)
|
||||
checkList(t, l2, []interface{}{1})
|
||||
var l2 = New()
|
||||
l2.PushBack(1)
|
||||
checkList(t, l2, []interface{}{1})
|
||||
|
||||
var l3 = New()
|
||||
l3.PushFrontList(l1)
|
||||
checkList(t, l3, []interface{}{1})
|
||||
var l3 = New()
|
||||
l3.PushFrontList(l1)
|
||||
checkList(t, l3, []interface{}{1})
|
||||
|
||||
var l4 = New()
|
||||
l4.PushBackList(l2)
|
||||
checkList(t, l4, []interface{}{1})
|
||||
var l4 = New()
|
||||
l4.PushBackList(l2)
|
||||
checkList(t, l4, []interface{}{1})
|
||||
})
|
||||
}
|
||||
|
||||
// Test that a list l is not modified when calling InsertBefore with a mark that is not an element of l.
|
||||
func TestInsertBeforeUnknownMark(t *testing.T) {
|
||||
l := New()
|
||||
l.PushBack(1)
|
||||
l.PushBack(2)
|
||||
l.PushBack(3)
|
||||
l.InsertBefore(new(Element), 1)
|
||||
checkList(t, l, []interface{}{1, 2, 3})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
l := New()
|
||||
l.PushBack(1)
|
||||
l.PushBack(2)
|
||||
l.PushBack(3)
|
||||
l.InsertBefore(new(Element), 1)
|
||||
checkList(t, l, []interface{}{1, 2, 3})
|
||||
})
|
||||
}
|
||||
|
||||
// Test that a list l is not modified when calling InsertAfter with a mark that is not an element of l.
|
||||
func TestInsertAfterUnknownMark(t *testing.T) {
|
||||
l := New()
|
||||
l.PushBack(1)
|
||||
l.PushBack(2)
|
||||
l.PushBack(3)
|
||||
l.InsertAfter(new(Element), 1)
|
||||
checkList(t, l, []interface{}{1, 2, 3})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
l := New()
|
||||
l.PushBack(1)
|
||||
l.PushBack(2)
|
||||
l.PushBack(3)
|
||||
l.InsertAfter(new(Element), 1)
|
||||
checkList(t, l, []interface{}{1, 2, 3})
|
||||
})
|
||||
}
|
||||
|
||||
// Test that a list l is not modified when calling MoveAfter or MoveBefore with a mark that is not an element of l.
|
||||
func TestMoveUnknownMark(t *testing.T) {
|
||||
l1 := New()
|
||||
e1 := l1.PushBack(1)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
l1 := New()
|
||||
e1 := l1.PushBack(1)
|
||||
|
||||
l2 := New()
|
||||
e2 := l2.PushBack(2)
|
||||
l2 := New()
|
||||
e2 := l2.PushBack(2)
|
||||
|
||||
l1.MoveAfter(e1, e2)
|
||||
checkList(t, l1, []interface{}{1})
|
||||
checkList(t, l2, []interface{}{2})
|
||||
l1.MoveAfter(e1, e2)
|
||||
checkList(t, l1, []interface{}{1})
|
||||
checkList(t, l2, []interface{}{2})
|
||||
|
||||
l1.MoveBefore(e1, e2)
|
||||
checkList(t, l1, []interface{}{1})
|
||||
checkList(t, l2, []interface{}{2})
|
||||
l1.MoveBefore(e1, e2)
|
||||
checkList(t, l1, []interface{}{1})
|
||||
checkList(t, l2, []interface{}{2})
|
||||
})
|
||||
}
|
||||
|
||||
func TestList_RemoveAll(t *testing.T) {
|
||||
l := New()
|
||||
l.PushBack(1)
|
||||
l.RemoveAll()
|
||||
checkList(t, l, []interface{}{})
|
||||
l.PushBack(2)
|
||||
checkList(t, l, []interface{}{2})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
l := New()
|
||||
l.PushBack(1)
|
||||
l.RemoveAll()
|
||||
checkList(t, l, []interface{}{})
|
||||
l.PushBack(2)
|
||||
checkList(t, l, []interface{}{2})
|
||||
})
|
||||
}
|
||||
|
||||
func TestList_PushFronts(t *testing.T) {
|
||||
l := New()
|
||||
a1 := []interface{}{1, 2}
|
||||
l.PushFronts(a1)
|
||||
checkList(t, l, []interface{}{2, 1})
|
||||
a1 = []interface{}{3, 4, 5}
|
||||
l.PushFronts(a1)
|
||||
checkList(t, l, []interface{}{5, 4, 3, 2, 1})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
l := New()
|
||||
a1 := []interface{}{1, 2}
|
||||
l.PushFronts(a1)
|
||||
checkList(t, l, []interface{}{2, 1})
|
||||
a1 = []interface{}{3, 4, 5}
|
||||
l.PushFronts(a1)
|
||||
checkList(t, l, []interface{}{5, 4, 3, 2, 1})
|
||||
})
|
||||
}
|
||||
|
||||
func TestList_PushBacks(t *testing.T) {
|
||||
l := New()
|
||||
a1 := []interface{}{1, 2}
|
||||
l.PushBacks(a1)
|
||||
checkList(t, l, []interface{}{1, 2})
|
||||
a1 = []interface{}{3, 4, 5}
|
||||
l.PushBacks(a1)
|
||||
checkList(t, l, []interface{}{1, 2, 3, 4, 5})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
l := New()
|
||||
a1 := []interface{}{1, 2}
|
||||
l.PushBacks(a1)
|
||||
checkList(t, l, []interface{}{1, 2})
|
||||
a1 = []interface{}{3, 4, 5}
|
||||
l.PushBacks(a1)
|
||||
checkList(t, l, []interface{}{1, 2, 3, 4, 5})
|
||||
})
|
||||
}
|
||||
|
||||
func TestList_PopBacks(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
l := New()
|
||||
a1 := []interface{}{1, 2, 3, 4}
|
||||
a2 := []interface{}{"a", "c", "b", "e"}
|
||||
l.PushFronts(a1)
|
||||
i1 := l.PopBacks(2)
|
||||
gtest.Assert(i1, []interface{}{1, 2})
|
||||
t.Assert(i1, []interface{}{1, 2})
|
||||
|
||||
l.PushBacks(a2) //4.3,a,c,b,e
|
||||
i1 = l.PopBacks(3)
|
||||
gtest.Assert(i1, []interface{}{"e", "b", "c"})
|
||||
t.Assert(i1, []interface{}{"e", "b", "c"})
|
||||
})
|
||||
}
|
||||
|
||||
func TestList_PopFronts(t *testing.T) {
|
||||
l := New()
|
||||
a1 := []interface{}{1, 2, 3, 4}
|
||||
l.PushFronts(a1)
|
||||
i1 := l.PopFronts(2)
|
||||
gtest.Assert(i1, []interface{}{4, 3})
|
||||
gtest.Assert(l.Len(), 2)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
l := New()
|
||||
a1 := []interface{}{1, 2, 3, 4}
|
||||
l.PushFronts(a1)
|
||||
i1 := l.PopFronts(2)
|
||||
t.Assert(i1, []interface{}{4, 3})
|
||||
t.Assert(l.Len(), 2)
|
||||
})
|
||||
}
|
||||
|
||||
func TestList_PopBackAll(t *testing.T) {
|
||||
l := New()
|
||||
a1 := []interface{}{1, 2, 3, 4}
|
||||
l.PushFronts(a1)
|
||||
i1 := l.PopBackAll()
|
||||
gtest.Assert(i1, []interface{}{1, 2, 3, 4})
|
||||
gtest.Assert(l.Len(), 0)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
l := New()
|
||||
a1 := []interface{}{1, 2, 3, 4}
|
||||
l.PushFronts(a1)
|
||||
i1 := l.PopBackAll()
|
||||
t.Assert(i1, []interface{}{1, 2, 3, 4})
|
||||
t.Assert(l.Len(), 0)
|
||||
})
|
||||
}
|
||||
|
||||
func TestList_PopFrontAll(t *testing.T) {
|
||||
l := New()
|
||||
a1 := []interface{}{1, 2, 3, 4}
|
||||
l.PushFronts(a1)
|
||||
i1 := l.PopFrontAll()
|
||||
gtest.Assert(i1, []interface{}{4, 3, 2, 1})
|
||||
gtest.Assert(l.Len(), 0)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
l := New()
|
||||
a1 := []interface{}{1, 2, 3, 4}
|
||||
l.PushFronts(a1)
|
||||
i1 := l.PopFrontAll()
|
||||
t.Assert(i1, []interface{}{4, 3, 2, 1})
|
||||
t.Assert(l.Len(), 0)
|
||||
})
|
||||
}
|
||||
|
||||
func TestList_FrontAll(t *testing.T) {
|
||||
l := New()
|
||||
a1 := []interface{}{1, 2, 3, 4}
|
||||
l.PushFronts(a1)
|
||||
i1 := l.FrontAll()
|
||||
gtest.Assert(i1, []interface{}{4, 3, 2, 1})
|
||||
gtest.Assert(l.Len(), 4)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
l := New()
|
||||
a1 := []interface{}{1, 2, 3, 4}
|
||||
l.PushFronts(a1)
|
||||
i1 := l.FrontAll()
|
||||
t.Assert(i1, []interface{}{4, 3, 2, 1})
|
||||
t.Assert(l.Len(), 4)
|
||||
})
|
||||
}
|
||||
|
||||
func TestList_BackAll(t *testing.T) {
|
||||
l := New()
|
||||
a1 := []interface{}{1, 2, 3, 4}
|
||||
l.PushFronts(a1)
|
||||
i1 := l.BackAll()
|
||||
gtest.Assert(i1, []interface{}{1, 2, 3, 4})
|
||||
gtest.Assert(l.Len(), 4)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
l := New()
|
||||
a1 := []interface{}{1, 2, 3, 4}
|
||||
l.PushFronts(a1)
|
||||
i1 := l.BackAll()
|
||||
t.Assert(i1, []interface{}{1, 2, 3, 4})
|
||||
t.Assert(l.Len(), 4)
|
||||
})
|
||||
}
|
||||
|
||||
func TestList_FrontValue(t *testing.T) {
|
||||
l := New()
|
||||
l2 := New()
|
||||
a1 := []interface{}{1, 2, 3, 4}
|
||||
l.PushFronts(a1)
|
||||
i1 := l.FrontValue()
|
||||
gtest.Assert(gconv.Int(i1), 4)
|
||||
gtest.Assert(l.Len(), 4)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
l := New()
|
||||
l2 := New()
|
||||
a1 := []interface{}{1, 2, 3, 4}
|
||||
l.PushFronts(a1)
|
||||
i1 := l.FrontValue()
|
||||
t.Assert(gconv.Int(i1), 4)
|
||||
t.Assert(l.Len(), 4)
|
||||
|
||||
i1 = l2.FrontValue()
|
||||
gtest.Assert(i1, nil)
|
||||
i1 = l2.FrontValue()
|
||||
t.Assert(i1, nil)
|
||||
})
|
||||
}
|
||||
|
||||
func TestList_BackValue(t *testing.T) {
|
||||
l := New()
|
||||
l2 := New()
|
||||
a1 := []interface{}{1, 2, 3, 4}
|
||||
l.PushFronts(a1)
|
||||
i1 := l.BackValue()
|
||||
gtest.Assert(gconv.Int(i1), 1)
|
||||
gtest.Assert(l.Len(), 4)
|
||||
|
||||
i1 = l2.FrontValue()
|
||||
gtest.Assert(i1, nil)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
l := New()
|
||||
l2 := New()
|
||||
a1 := []interface{}{1, 2, 3, 4}
|
||||
l.PushFronts(a1)
|
||||
i1 := l.BackValue()
|
||||
t.Assert(gconv.Int(i1), 1)
|
||||
t.Assert(l.Len(), 4)
|
||||
|
||||
i1 = l2.FrontValue()
|
||||
t.Assert(i1, nil)
|
||||
})
|
||||
}
|
||||
|
||||
func TestList_Back(t *testing.T) {
|
||||
l := New()
|
||||
a1 := []interface{}{1, 2, 3, 4}
|
||||
l.PushFronts(a1)
|
||||
e1 := l.Back()
|
||||
gtest.Assert(e1.Value, 1)
|
||||
gtest.Assert(l.Len(), 4)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
l := New()
|
||||
a1 := []interface{}{1, 2, 3, 4}
|
||||
l.PushFronts(a1)
|
||||
e1 := l.Back()
|
||||
t.Assert(e1.Value, 1)
|
||||
t.Assert(l.Len(), 4)
|
||||
})
|
||||
}
|
||||
|
||||
func TestList_Size(t *testing.T) {
|
||||
l := New()
|
||||
a1 := []interface{}{1, 2, 3, 4}
|
||||
l.PushFronts(a1)
|
||||
gtest.Assert(l.Size(), 4)
|
||||
l.PopFront()
|
||||
gtest.Assert(l.Size(), 3)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
l := New()
|
||||
a1 := []interface{}{1, 2, 3, 4}
|
||||
l.PushFronts(a1)
|
||||
t.Assert(l.Size(), 4)
|
||||
l.PopFront()
|
||||
t.Assert(l.Size(), 3)
|
||||
})
|
||||
}
|
||||
|
||||
func TestList_Removes(t *testing.T) {
|
||||
l := New()
|
||||
a1 := []interface{}{1, 2, 3, 4}
|
||||
l.PushFronts(a1)
|
||||
e1 := l.Back()
|
||||
l.Removes([]*Element{e1})
|
||||
gtest.Assert(l.Len(), 3)
|
||||
|
||||
e2 := l.Back()
|
||||
l.Removes([]*Element{e2})
|
||||
gtest.Assert(l.Len(), 2)
|
||||
checkList(t, l, []interface{}{4, 3})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
l := New()
|
||||
a1 := []interface{}{1, 2, 3, 4}
|
||||
l.PushFronts(a1)
|
||||
e1 := l.Back()
|
||||
l.Removes([]*Element{e1})
|
||||
t.Assert(l.Len(), 3)
|
||||
|
||||
e2 := l.Back()
|
||||
l.Removes([]*Element{e2})
|
||||
t.Assert(l.Len(), 2)
|
||||
checkList(t, l, []interface{}{4, 3})
|
||||
})
|
||||
}
|
||||
|
||||
func TestList_Clear(t *testing.T) {
|
||||
l := New()
|
||||
a1 := []interface{}{1, 2, 3, 4}
|
||||
l.PushFronts(a1)
|
||||
l.Clear()
|
||||
gtest.Assert(l.Len(), 0)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
l := New()
|
||||
a1 := []interface{}{1, 2, 3, 4}
|
||||
l.PushFronts(a1)
|
||||
l.Clear()
|
||||
t.Assert(l.Len(), 0)
|
||||
})
|
||||
}
|
||||
|
||||
func TestList_IteratorAsc(t *testing.T) {
|
||||
l := New()
|
||||
a1 := []interface{}{1, 2, 5, 6, 3, 4}
|
||||
l.PushFronts(a1)
|
||||
e1 := l.Back()
|
||||
fun1 := func(e *Element) bool {
|
||||
if gconv.Int(e1.Value) > 2 {
|
||||
return true
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
l := New()
|
||||
a1 := []interface{}{1, 2, 5, 6, 3, 4}
|
||||
l.PushFronts(a1)
|
||||
e1 := l.Back()
|
||||
fun1 := func(e *Element) bool {
|
||||
if gconv.Int(e1.Value) > 2 {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
return false
|
||||
}
|
||||
checkList(t, l, []interface{}{4, 3, 6, 5, 2, 1})
|
||||
l.IteratorAsc(fun1)
|
||||
checkList(t, l, []interface{}{4, 3, 6, 5, 2, 1})
|
||||
checkList(t, l, []interface{}{4, 3, 6, 5, 2, 1})
|
||||
l.IteratorAsc(fun1)
|
||||
checkList(t, l, []interface{}{4, 3, 6, 5, 2, 1})
|
||||
})
|
||||
}
|
||||
|
||||
func TestList_IteratorDesc(t *testing.T) {
|
||||
l := New()
|
||||
a1 := []interface{}{1, 2, 3, 4}
|
||||
l.PushFronts(a1)
|
||||
e1 := l.Back()
|
||||
fun1 := func(e *Element) bool {
|
||||
if gconv.Int(e1.Value) > 6 {
|
||||
return true
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
l := New()
|
||||
a1 := []interface{}{1, 2, 3, 4}
|
||||
l.PushFronts(a1)
|
||||
e1 := l.Back()
|
||||
fun1 := func(e *Element) bool {
|
||||
if gconv.Int(e1.Value) > 6 {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
return false
|
||||
}
|
||||
l.IteratorDesc(fun1)
|
||||
gtest.Assert(l.Len(), 4)
|
||||
checkList(t, l, []interface{}{4, 3, 2, 1})
|
||||
l.IteratorDesc(fun1)
|
||||
t.Assert(l.Len(), 4)
|
||||
checkList(t, l, []interface{}{4, 3, 2, 1})
|
||||
})
|
||||
}
|
||||
|
||||
func TestList_Iterator(t *testing.T) {
|
||||
l := New()
|
||||
a1 := []interface{}{"a", "b", "c", "d", "e"}
|
||||
l.PushFronts(a1)
|
||||
e1 := l.Back()
|
||||
fun1 := func(e *Element) bool {
|
||||
if gconv.String(e1.Value) > "c" {
|
||||
return true
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
l := New()
|
||||
a1 := []interface{}{"a", "b", "c", "d", "e"}
|
||||
l.PushFronts(a1)
|
||||
e1 := l.Back()
|
||||
fun1 := func(e *Element) bool {
|
||||
if gconv.String(e1.Value) > "c" {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
return false
|
||||
}
|
||||
checkList(t, l, []interface{}{"e", "d", "c", "b", "a"})
|
||||
l.Iterator(fun1)
|
||||
checkList(t, l, []interface{}{"e", "d", "c", "b", "a"})
|
||||
checkList(t, l, []interface{}{"e", "d", "c", "b", "a"})
|
||||
l.Iterator(fun1)
|
||||
checkList(t, l, []interface{}{"e", "d", "c", "b", "a"})
|
||||
})
|
||||
}
|
||||
|
||||
func TestList_Join(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
l := NewFrom([]interface{}{1, 2, "a", `"b"`, `\c`})
|
||||
gtest.Assert(l.Join(","), `1,2,"a","\"b\"","\\c"`)
|
||||
gtest.Assert(l.Join("."), `1.2."a"."\"b\""."\\c"`)
|
||||
t.Assert(l.Join(","), `1,2,"a","\"b\"","\\c"`)
|
||||
t.Assert(l.Join("."), `1.2."a"."\"b\""."\\c"`)
|
||||
})
|
||||
}
|
||||
|
||||
func TestList_String(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
l := NewFrom([]interface{}{1, 2, "a", `"b"`, `\c`})
|
||||
gtest.Assert(l.String(), `[1,2,"a","\"b\"","\\c"]`)
|
||||
t.Assert(l.String(), `[1,2,"a","\"b\"","\\c"]`)
|
||||
})
|
||||
}
|
||||
|
||||
func TestList_Json(t *testing.T) {
|
||||
// Marshal
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a := []interface{}{"a", "b", "c"}
|
||||
l := New()
|
||||
l.PushBacks(a)
|
||||
b1, err1 := json.Marshal(l)
|
||||
b2, err2 := json.Marshal(a)
|
||||
gtest.Assert(err1, err2)
|
||||
gtest.Assert(b1, b2)
|
||||
t.Assert(err1, err2)
|
||||
t.Assert(b1, b2)
|
||||
})
|
||||
// Unmarshal
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a := []interface{}{"a", "b", "c"}
|
||||
l := New()
|
||||
b, err := json.Marshal(a)
|
||||
gtest.Assert(err, nil)
|
||||
t.Assert(err, nil)
|
||||
|
||||
err = json.Unmarshal(b, l)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(l.FrontAll(), a)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(l.FrontAll(), a)
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var l List
|
||||
a := []interface{}{"a", "b", "c"}
|
||||
b, err := json.Marshal(a)
|
||||
gtest.Assert(err, nil)
|
||||
t.Assert(err, nil)
|
||||
|
||||
err = json.Unmarshal(b, &l)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(l.FrontAll(), a)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(l.FrontAll(), a)
|
||||
})
|
||||
}
|
||||
|
||||
func TestList_UnmarshalValue(t *testing.T) {
|
||||
type T struct {
|
||||
type TList struct {
|
||||
Name string
|
||||
List *List
|
||||
}
|
||||
// JSON
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var tlist *TList
|
||||
err := gconv.Struct(map[string]interface{}{
|
||||
"name": "john",
|
||||
"list": []byte(`[1,2,3]`),
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.List.FrontAll(), []interface{}{1, 2, 3})
|
||||
}, &tlist)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(tlist.Name, "john")
|
||||
t.Assert(tlist.List.FrontAll(), []interface{}{1, 2, 3})
|
||||
})
|
||||
// Map
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var tlist *TList
|
||||
err := gconv.Struct(map[string]interface{}{
|
||||
"name": "john",
|
||||
"list": []interface{}{1, 2, 3},
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.List.FrontAll(), []interface{}{1, 2, 3})
|
||||
}, &tlist)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(tlist.Name, "john")
|
||||
t.Assert(tlist.List.FrontAll(), []interface{}{1, 2, 3})
|
||||
})
|
||||
}
|
||||
|
||||
@ -18,109 +18,120 @@ func getValue() interface{} {
|
||||
}
|
||||
|
||||
func Test_Map_Basic(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gmap.New()
|
||||
m.Set("key1", "val1")
|
||||
gtest.Assert(m.Keys(), []interface{}{"key1"})
|
||||
t.Assert(m.Keys(), []interface{}{"key1"})
|
||||
|
||||
gtest.Assert(m.Get("key1"), "val1")
|
||||
gtest.Assert(m.Size(), 1)
|
||||
gtest.Assert(m.IsEmpty(), false)
|
||||
t.Assert(m.Get("key1"), "val1")
|
||||
t.Assert(m.Size(), 1)
|
||||
t.Assert(m.IsEmpty(), false)
|
||||
|
||||
gtest.Assert(m.GetOrSet("key2", "val2"), "val2")
|
||||
gtest.Assert(m.SetIfNotExist("key2", "val2"), false)
|
||||
t.Assert(m.GetOrSet("key2", "val2"), "val2")
|
||||
t.Assert(m.SetIfNotExist("key2", "val2"), false)
|
||||
|
||||
gtest.Assert(m.SetIfNotExist("key3", "val3"), true)
|
||||
t.Assert(m.SetIfNotExist("key3", "val3"), true)
|
||||
|
||||
gtest.Assert(m.Remove("key2"), "val2")
|
||||
gtest.Assert(m.Contains("key2"), false)
|
||||
t.Assert(m.Remove("key2"), "val2")
|
||||
t.Assert(m.Contains("key2"), false)
|
||||
|
||||
gtest.AssertIN("key3", m.Keys())
|
||||
gtest.AssertIN("key1", m.Keys())
|
||||
gtest.AssertIN("val3", m.Values())
|
||||
gtest.AssertIN("val1", m.Values())
|
||||
t.AssertIN("key3", m.Keys())
|
||||
t.AssertIN("key1", m.Keys())
|
||||
t.AssertIN("val3", m.Values())
|
||||
t.AssertIN("val1", m.Values())
|
||||
|
||||
m.Flip()
|
||||
gtest.Assert(m.Map(), map[interface{}]interface{}{"val3": "key3", "val1": "key1"})
|
||||
t.Assert(m.Map(), map[interface{}]interface{}{"val3": "key3", "val1": "key1"})
|
||||
|
||||
m.Clear()
|
||||
gtest.Assert(m.Size(), 0)
|
||||
gtest.Assert(m.IsEmpty(), true)
|
||||
t.Assert(m.Size(), 0)
|
||||
t.Assert(m.IsEmpty(), true)
|
||||
|
||||
m2 := gmap.NewFrom(map[interface{}]interface{}{1: 1, "key1": "val1"})
|
||||
gtest.Assert(m2.Map(), map[interface{}]interface{}{1: 1, "key1": "val1"})
|
||||
t.Assert(m2.Map(), map[interface{}]interface{}{1: 1, "key1": "val1"})
|
||||
})
|
||||
}
|
||||
func Test_Map_Set_Fun(t *testing.T) {
|
||||
m := gmap.New()
|
||||
m.GetOrSetFunc("fun", getValue)
|
||||
m.GetOrSetFuncLock("funlock", getValue)
|
||||
gtest.Assert(m.Get("funlock"), 3)
|
||||
gtest.Assert(m.Get("fun"), 3)
|
||||
m.GetOrSetFunc("fun", getValue)
|
||||
gtest.Assert(m.SetIfNotExistFunc("fun", getValue), false)
|
||||
gtest.Assert(m.SetIfNotExistFuncLock("funlock", getValue), false)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gmap.New()
|
||||
m.GetOrSetFunc("fun", getValue)
|
||||
m.GetOrSetFuncLock("funlock", getValue)
|
||||
t.Assert(m.Get("funlock"), 3)
|
||||
t.Assert(m.Get("fun"), 3)
|
||||
m.GetOrSetFunc("fun", getValue)
|
||||
t.Assert(m.SetIfNotExistFunc("fun", getValue), false)
|
||||
t.Assert(m.SetIfNotExistFuncLock("funlock", getValue), false)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Map_Batch(t *testing.T) {
|
||||
m := gmap.New()
|
||||
m.Sets(map[interface{}]interface{}{1: 1, "key1": "val1", "key2": "val2", "key3": "val3"})
|
||||
gtest.Assert(m.Map(), map[interface{}]interface{}{1: 1, "key1": "val1", "key2": "val2", "key3": "val3"})
|
||||
m.Removes([]interface{}{"key1", 1})
|
||||
gtest.Assert(m.Map(), map[interface{}]interface{}{"key2": "val2", "key3": "val3"})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gmap.New()
|
||||
m.Sets(map[interface{}]interface{}{1: 1, "key1": "val1", "key2": "val2", "key3": "val3"})
|
||||
t.Assert(m.Map(), map[interface{}]interface{}{1: 1, "key1": "val1", "key2": "val2", "key3": "val3"})
|
||||
m.Removes([]interface{}{"key1", 1})
|
||||
t.Assert(m.Map(), map[interface{}]interface{}{"key2": "val2", "key3": "val3"})
|
||||
})
|
||||
}
|
||||
func Test_Map_Iterator(t *testing.T) {
|
||||
expect := map[interface{}]interface{}{1: 1, "key1": "val1"}
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
expect := map[interface{}]interface{}{1: 1, "key1": "val1"}
|
||||
|
||||
m := gmap.NewFrom(expect)
|
||||
m.Iterator(func(k interface{}, v interface{}) bool {
|
||||
gtest.Assert(expect[k], v)
|
||||
return true
|
||||
m := gmap.NewFrom(expect)
|
||||
m.Iterator(func(k interface{}, v interface{}) bool {
|
||||
t.Assert(expect[k], v)
|
||||
return true
|
||||
})
|
||||
// 断言返回值对遍历控制
|
||||
i := 0
|
||||
j := 0
|
||||
m.Iterator(func(k interface{}, v interface{}) bool {
|
||||
i++
|
||||
return true
|
||||
})
|
||||
m.Iterator(func(k interface{}, v interface{}) bool {
|
||||
j++
|
||||
return false
|
||||
})
|
||||
t.Assert(i, 2)
|
||||
t.Assert(j, 1)
|
||||
})
|
||||
// 断言返回值对遍历控制
|
||||
i := 0
|
||||
j := 0
|
||||
m.Iterator(func(k interface{}, v interface{}) bool {
|
||||
i++
|
||||
return true
|
||||
})
|
||||
m.Iterator(func(k interface{}, v interface{}) bool {
|
||||
j++
|
||||
return false
|
||||
})
|
||||
gtest.Assert(i, 2)
|
||||
gtest.Assert(j, 1)
|
||||
}
|
||||
|
||||
func Test_Map_Lock(t *testing.T) {
|
||||
expect := map[interface{}]interface{}{1: 1, "key1": "val1"}
|
||||
|
||||
m := gmap.NewFrom(expect)
|
||||
m.LockFunc(func(m map[interface{}]interface{}) {
|
||||
gtest.Assert(m, expect)
|
||||
})
|
||||
m.RLockFunc(func(m map[interface{}]interface{}) {
|
||||
gtest.Assert(m, expect)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
expect := map[interface{}]interface{}{1: 1, "key1": "val1"}
|
||||
m := gmap.NewFrom(expect)
|
||||
m.LockFunc(func(m map[interface{}]interface{}) {
|
||||
t.Assert(m, expect)
|
||||
})
|
||||
m.RLockFunc(func(m map[interface{}]interface{}) {
|
||||
t.Assert(m, expect)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Map_Clone(t *testing.T) {
|
||||
//clone 方法是深克隆
|
||||
m := gmap.NewFrom(map[interface{}]interface{}{1: 1, "key1": "val1"})
|
||||
m_clone := m.Clone()
|
||||
m.Remove(1)
|
||||
//修改原 map,clone 后的 map 不影响
|
||||
gtest.AssertIN(1, m_clone.Keys())
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
//clone 方法是深克隆
|
||||
m := gmap.NewFrom(map[interface{}]interface{}{1: 1, "key1": "val1"})
|
||||
m_clone := m.Clone()
|
||||
m.Remove(1)
|
||||
//修改原 map,clone 后的 map 不影响
|
||||
t.AssertIN(1, m_clone.Keys())
|
||||
|
||||
m_clone.Remove("key1")
|
||||
//修改clone map,原 map 不影响
|
||||
gtest.AssertIN("key1", m.Keys())
|
||||
m_clone.Remove("key1")
|
||||
//修改clone map,原 map 不影响
|
||||
t.AssertIN("key1", m.Keys())
|
||||
})
|
||||
}
|
||||
func Test_Map_Basic_Merge(t *testing.T) {
|
||||
m1 := gmap.New()
|
||||
m2 := gmap.New()
|
||||
m1.Set("key1", "val1")
|
||||
m2.Set("key2", "val2")
|
||||
m1.Merge(m2)
|
||||
gtest.Assert(m1.Map(), map[interface{}]interface{}{"key1": "val1", "key2": "val2"})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m1 := gmap.New()
|
||||
m2 := gmap.New()
|
||||
m1.Set("key1", "val1")
|
||||
m2.Set("key2", "val2")
|
||||
m1.Merge(m2)
|
||||
t.Assert(m1.Map(), map[interface{}]interface{}{"key1": "val1", "key2": "val2"})
|
||||
})
|
||||
}
|
||||
|
||||
@ -6,7 +6,7 @@ import (
|
||||
"github.com/gogf/gf/container/gmap"
|
||||
)
|
||||
|
||||
func Example_Normal_Basic() {
|
||||
func Example_normalBasic() {
|
||||
m := gmap.New()
|
||||
|
||||
//Add data
|
||||
@ -61,7 +61,7 @@ func Example_Normal_Basic() {
|
||||
fmt.Println(m.Size())
|
||||
|
||||
}
|
||||
func Example_Normal_Merge() {
|
||||
func Example_normalMerge() {
|
||||
m1 := gmap.New()
|
||||
m2 := gmap.New()
|
||||
m1.Set("key1", "val1")
|
||||
|
||||
@ -22,166 +22,181 @@ func anyAnyCallBack(int, interface{}) bool {
|
||||
}
|
||||
|
||||
func Test_AnyAnyMap_Basic(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gmap.NewAnyAnyMap()
|
||||
m.Set(1, 1)
|
||||
|
||||
gtest.Assert(m.Get(1), 1)
|
||||
gtest.Assert(m.Size(), 1)
|
||||
gtest.Assert(m.IsEmpty(), false)
|
||||
t.Assert(m.Get(1), 1)
|
||||
t.Assert(m.Size(), 1)
|
||||
t.Assert(m.IsEmpty(), false)
|
||||
|
||||
gtest.Assert(m.GetOrSet(2, "2"), "2")
|
||||
gtest.Assert(m.SetIfNotExist(2, "2"), false)
|
||||
t.Assert(m.GetOrSet(2, "2"), "2")
|
||||
t.Assert(m.SetIfNotExist(2, "2"), false)
|
||||
|
||||
gtest.Assert(m.SetIfNotExist(3, 3), true)
|
||||
t.Assert(m.SetIfNotExist(3, 3), true)
|
||||
|
||||
gtest.Assert(m.Remove(2), "2")
|
||||
gtest.Assert(m.Contains(2), false)
|
||||
t.Assert(m.Remove(2), "2")
|
||||
t.Assert(m.Contains(2), false)
|
||||
|
||||
gtest.AssertIN(3, m.Keys())
|
||||
gtest.AssertIN(1, m.Keys())
|
||||
gtest.AssertIN(3, m.Values())
|
||||
gtest.AssertIN(1, m.Values())
|
||||
t.AssertIN(3, m.Keys())
|
||||
t.AssertIN(1, m.Keys())
|
||||
t.AssertIN(3, m.Values())
|
||||
t.AssertIN(1, m.Values())
|
||||
m.Flip()
|
||||
gtest.Assert(m.Map(), map[interface{}]int{1: 1, 3: 3})
|
||||
t.Assert(m.Map(), map[interface{}]int{1: 1, 3: 3})
|
||||
|
||||
m.Clear()
|
||||
gtest.Assert(m.Size(), 0)
|
||||
gtest.Assert(m.IsEmpty(), true)
|
||||
t.Assert(m.Size(), 0)
|
||||
t.Assert(m.IsEmpty(), true)
|
||||
|
||||
m2 := gmap.NewAnyAnyMapFrom(map[interface{}]interface{}{1: 1, 2: "2"})
|
||||
gtest.Assert(m2.Map(), map[interface{}]interface{}{1: 1, 2: "2"})
|
||||
t.Assert(m2.Map(), map[interface{}]interface{}{1: 1, 2: "2"})
|
||||
})
|
||||
}
|
||||
|
||||
func Test_AnyAnyMap_Set_Fun(t *testing.T) {
|
||||
m := gmap.NewAnyAnyMap()
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gmap.NewAnyAnyMap()
|
||||
|
||||
m.GetOrSetFunc(1, getAny)
|
||||
m.GetOrSetFuncLock(2, getAny)
|
||||
gtest.Assert(m.Get(1), 123)
|
||||
gtest.Assert(m.Get(2), 123)
|
||||
m.GetOrSetFunc(1, getAny)
|
||||
m.GetOrSetFuncLock(2, getAny)
|
||||
t.Assert(m.Get(1), 123)
|
||||
t.Assert(m.Get(2), 123)
|
||||
|
||||
gtest.Assert(m.SetIfNotExistFunc(1, getAny), false)
|
||||
gtest.Assert(m.SetIfNotExistFunc(3, getAny), true)
|
||||
t.Assert(m.SetIfNotExistFunc(1, getAny), false)
|
||||
t.Assert(m.SetIfNotExistFunc(3, getAny), true)
|
||||
|
||||
gtest.Assert(m.SetIfNotExistFuncLock(2, getAny), false)
|
||||
gtest.Assert(m.SetIfNotExistFuncLock(4, getAny), true)
|
||||
t.Assert(m.SetIfNotExistFuncLock(2, getAny), false)
|
||||
t.Assert(m.SetIfNotExistFuncLock(4, getAny), true)
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
func Test_AnyAnyMap_Batch(t *testing.T) {
|
||||
m := gmap.NewAnyAnyMap()
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gmap.NewAnyAnyMap()
|
||||
|
||||
m.Sets(map[interface{}]interface{}{1: 1, 2: "2", 3: 3})
|
||||
gtest.Assert(m.Map(), map[interface{}]interface{}{1: 1, 2: "2", 3: 3})
|
||||
m.Removes([]interface{}{1, 2})
|
||||
gtest.Assert(m.Map(), map[interface{}]interface{}{3: 3})
|
||||
m.Sets(map[interface{}]interface{}{1: 1, 2: "2", 3: 3})
|
||||
t.Assert(m.Map(), map[interface{}]interface{}{1: 1, 2: "2", 3: 3})
|
||||
m.Removes([]interface{}{1, 2})
|
||||
t.Assert(m.Map(), map[interface{}]interface{}{3: 3})
|
||||
})
|
||||
}
|
||||
|
||||
func Test_AnyAnyMap_Iterator(t *testing.T) {
|
||||
expect := map[interface{}]interface{}{1: 1, 2: "2"}
|
||||
m := gmap.NewAnyAnyMapFrom(expect)
|
||||
m.Iterator(func(k interface{}, v interface{}) bool {
|
||||
gtest.Assert(expect[k], v)
|
||||
return true
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
expect := map[interface{}]interface{}{1: 1, 2: "2"}
|
||||
m := gmap.NewAnyAnyMapFrom(expect)
|
||||
m.Iterator(func(k interface{}, v interface{}) bool {
|
||||
t.Assert(expect[k], v)
|
||||
return true
|
||||
})
|
||||
// 断言返回值对遍历控制
|
||||
i := 0
|
||||
j := 0
|
||||
m.Iterator(func(k interface{}, v interface{}) bool {
|
||||
i++
|
||||
return true
|
||||
})
|
||||
m.Iterator(func(k interface{}, v interface{}) bool {
|
||||
j++
|
||||
return false
|
||||
})
|
||||
t.Assert(i, "2")
|
||||
t.Assert(j, 1)
|
||||
})
|
||||
// 断言返回值对遍历控制
|
||||
i := 0
|
||||
j := 0
|
||||
m.Iterator(func(k interface{}, v interface{}) bool {
|
||||
i++
|
||||
return true
|
||||
})
|
||||
m.Iterator(func(k interface{}, v interface{}) bool {
|
||||
j++
|
||||
return false
|
||||
})
|
||||
gtest.Assert(i, "2")
|
||||
gtest.Assert(j, 1)
|
||||
|
||||
}
|
||||
|
||||
func Test_AnyAnyMap_Lock(t *testing.T) {
|
||||
expect := map[interface{}]interface{}{1: 1, 2: "2"}
|
||||
m := gmap.NewAnyAnyMapFrom(expect)
|
||||
m.LockFunc(func(m map[interface{}]interface{}) {
|
||||
gtest.Assert(m, expect)
|
||||
})
|
||||
m.RLockFunc(func(m map[interface{}]interface{}) {
|
||||
gtest.Assert(m, expect)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
expect := map[interface{}]interface{}{1: 1, 2: "2"}
|
||||
m := gmap.NewAnyAnyMapFrom(expect)
|
||||
m.LockFunc(func(m map[interface{}]interface{}) {
|
||||
t.Assert(m, expect)
|
||||
})
|
||||
m.RLockFunc(func(m map[interface{}]interface{}) {
|
||||
t.Assert(m, expect)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func Test_AnyAnyMap_Clone(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
//clone 方法是深克隆
|
||||
m := gmap.NewAnyAnyMapFrom(map[interface{}]interface{}{1: 1, 2: "2"})
|
||||
|
||||
m_clone := m.Clone()
|
||||
m.Remove(1)
|
||||
//修改原 map,clone 后的 map 不影响
|
||||
gtest.AssertIN(1, m_clone.Keys())
|
||||
t.AssertIN(1, m_clone.Keys())
|
||||
|
||||
m_clone.Remove(2)
|
||||
//修改clone map,原 map 不影响
|
||||
gtest.AssertIN(2, m.Keys())
|
||||
t.AssertIN(2, m.Keys())
|
||||
})
|
||||
}
|
||||
|
||||
func Test_AnyAnyMap_Merge(t *testing.T) {
|
||||
m1 := gmap.NewAnyAnyMap()
|
||||
m2 := gmap.NewAnyAnyMap()
|
||||
m1.Set(1, 1)
|
||||
m2.Set(2, "2")
|
||||
m1.Merge(m2)
|
||||
gtest.Assert(m1.Map(), map[interface{}]interface{}{1: 1, 2: "2"})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m1 := gmap.NewAnyAnyMap()
|
||||
m2 := gmap.NewAnyAnyMap()
|
||||
m1.Set(1, 1)
|
||||
m2.Set(2, "2")
|
||||
m1.Merge(m2)
|
||||
t.Assert(m1.Map(), map[interface{}]interface{}{1: 1, 2: "2"})
|
||||
})
|
||||
}
|
||||
|
||||
func Test_AnyAnyMap_Map(t *testing.T) {
|
||||
m := gmap.NewAnyAnyMap()
|
||||
m.Set(1, 0)
|
||||
m.Set(2, 2)
|
||||
gtest.Assert(m.Get(1), 0)
|
||||
gtest.Assert(m.Get(2), 2)
|
||||
data := m.Map()
|
||||
gtest.Assert(data[1], 0)
|
||||
gtest.Assert(data[2], 2)
|
||||
data[3] = 3
|
||||
gtest.Assert(m.Get(3), 3)
|
||||
m.Set(4, 4)
|
||||
gtest.Assert(data[4], 4)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gmap.NewAnyAnyMap()
|
||||
m.Set(1, 0)
|
||||
m.Set(2, 2)
|
||||
t.Assert(m.Get(1), 0)
|
||||
t.Assert(m.Get(2), 2)
|
||||
data := m.Map()
|
||||
t.Assert(data[1], 0)
|
||||
t.Assert(data[2], 2)
|
||||
data[3] = 3
|
||||
t.Assert(m.Get(3), 3)
|
||||
m.Set(4, 4)
|
||||
t.Assert(data[4], 4)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_AnyAnyMap_MapCopy(t *testing.T) {
|
||||
m := gmap.NewAnyAnyMap()
|
||||
m.Set(1, 0)
|
||||
m.Set(2, 2)
|
||||
gtest.Assert(m.Get(1), 0)
|
||||
gtest.Assert(m.Get(2), 2)
|
||||
data := m.MapCopy()
|
||||
gtest.Assert(data[1], 0)
|
||||
gtest.Assert(data[2], 2)
|
||||
data[3] = 3
|
||||
gtest.Assert(m.Get(3), nil)
|
||||
m.Set(4, 4)
|
||||
gtest.Assert(data[4], nil)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gmap.NewAnyAnyMap()
|
||||
m.Set(1, 0)
|
||||
m.Set(2, 2)
|
||||
t.Assert(m.Get(1), 0)
|
||||
t.Assert(m.Get(2), 2)
|
||||
data := m.MapCopy()
|
||||
t.Assert(data[1], 0)
|
||||
t.Assert(data[2], 2)
|
||||
data[3] = 3
|
||||
t.Assert(m.Get(3), nil)
|
||||
m.Set(4, 4)
|
||||
t.Assert(data[4], nil)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_AnyAnyMap_FilterEmpty(t *testing.T) {
|
||||
m := gmap.NewAnyAnyMap()
|
||||
m.Set(1, 0)
|
||||
m.Set(2, 2)
|
||||
gtest.Assert(m.Get(1), 0)
|
||||
gtest.Assert(m.Get(2), 2)
|
||||
m.FilterEmpty()
|
||||
gtest.Assert(m.Get(1), nil)
|
||||
gtest.Assert(m.Get(2), 2)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gmap.NewAnyAnyMap()
|
||||
m.Set(1, 0)
|
||||
m.Set(2, 2)
|
||||
t.Assert(m.Get(1), 0)
|
||||
t.Assert(m.Get(2), 2)
|
||||
m.FilterEmpty()
|
||||
t.Assert(m.Get(1), nil)
|
||||
t.Assert(m.Get(2), 2)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_AnyAnyMap_Json(t *testing.T) {
|
||||
// Marshal
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
data := g.MapAnyAny{
|
||||
"k1": "v1",
|
||||
"k2": "v2",
|
||||
@ -189,125 +204,125 @@ func Test_AnyAnyMap_Json(t *testing.T) {
|
||||
m1 := gmap.NewAnyAnyMapFrom(data)
|
||||
b1, err1 := json.Marshal(m1)
|
||||
b2, err2 := json.Marshal(gconv.Map(data))
|
||||
gtest.Assert(err1, err2)
|
||||
gtest.Assert(b1, b2)
|
||||
t.Assert(err1, err2)
|
||||
t.Assert(b1, b2)
|
||||
})
|
||||
// Unmarshal
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
data := g.MapAnyAny{
|
||||
"k1": "v1",
|
||||
"k2": "v2",
|
||||
}
|
||||
b, err := json.Marshal(gconv.Map(data))
|
||||
gtest.Assert(err, nil)
|
||||
t.Assert(err, nil)
|
||||
|
||||
m := gmap.New()
|
||||
err = json.Unmarshal(b, m)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(m.Get("k1"), data["k1"])
|
||||
gtest.Assert(m.Get("k2"), data["k2"])
|
||||
t.Assert(err, nil)
|
||||
t.Assert(m.Get("k1"), data["k1"])
|
||||
t.Assert(m.Get("k2"), data["k2"])
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
data := g.MapAnyAny{
|
||||
"k1": "v1",
|
||||
"k2": "v2",
|
||||
}
|
||||
b, err := json.Marshal(gconv.Map(data))
|
||||
gtest.Assert(err, nil)
|
||||
t.Assert(err, nil)
|
||||
|
||||
var m gmap.Map
|
||||
err = json.Unmarshal(b, &m)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(m.Get("k1"), data["k1"])
|
||||
gtest.Assert(m.Get("k2"), data["k2"])
|
||||
t.Assert(err, nil)
|
||||
t.Assert(m.Get("k1"), data["k1"])
|
||||
t.Assert(m.Get("k2"), data["k2"])
|
||||
})
|
||||
}
|
||||
|
||||
func Test_AnyAnyMap_Pop(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gmap.NewAnyAnyMapFrom(g.MapAnyAny{
|
||||
"k1": "v1",
|
||||
"k2": "v2",
|
||||
})
|
||||
gtest.Assert(m.Size(), 2)
|
||||
t.Assert(m.Size(), 2)
|
||||
|
||||
k1, v1 := m.Pop()
|
||||
gtest.AssertIN(k1, g.Slice{"k1", "k2"})
|
||||
gtest.AssertIN(v1, g.Slice{"v1", "v2"})
|
||||
gtest.Assert(m.Size(), 1)
|
||||
t.AssertIN(k1, g.Slice{"k1", "k2"})
|
||||
t.AssertIN(v1, g.Slice{"v1", "v2"})
|
||||
t.Assert(m.Size(), 1)
|
||||
k2, v2 := m.Pop()
|
||||
gtest.AssertIN(k2, g.Slice{"k1", "k2"})
|
||||
gtest.AssertIN(v2, g.Slice{"v1", "v2"})
|
||||
gtest.Assert(m.Size(), 0)
|
||||
t.AssertIN(k2, g.Slice{"k1", "k2"})
|
||||
t.AssertIN(v2, g.Slice{"v1", "v2"})
|
||||
t.Assert(m.Size(), 0)
|
||||
|
||||
gtest.AssertNE(k1, k2)
|
||||
gtest.AssertNE(v1, v2)
|
||||
t.AssertNE(k1, k2)
|
||||
t.AssertNE(v1, v2)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_AnyAnyMap_Pops(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gmap.NewAnyAnyMapFrom(g.MapAnyAny{
|
||||
"k1": "v1",
|
||||
"k2": "v2",
|
||||
"k3": "v3",
|
||||
})
|
||||
gtest.Assert(m.Size(), 3)
|
||||
t.Assert(m.Size(), 3)
|
||||
|
||||
kArray := garray.New()
|
||||
vArray := garray.New()
|
||||
for k, v := range m.Pops(1) {
|
||||
gtest.AssertIN(k, g.Slice{"k1", "k2", "k3"})
|
||||
gtest.AssertIN(v, g.Slice{"v1", "v2", "v3"})
|
||||
t.AssertIN(k, g.Slice{"k1", "k2", "k3"})
|
||||
t.AssertIN(v, g.Slice{"v1", "v2", "v3"})
|
||||
kArray.Append(k)
|
||||
vArray.Append(v)
|
||||
}
|
||||
gtest.Assert(m.Size(), 2)
|
||||
t.Assert(m.Size(), 2)
|
||||
for k, v := range m.Pops(2) {
|
||||
gtest.AssertIN(k, g.Slice{"k1", "k2", "k3"})
|
||||
gtest.AssertIN(v, g.Slice{"v1", "v2", "v3"})
|
||||
t.AssertIN(k, g.Slice{"k1", "k2", "k3"})
|
||||
t.AssertIN(v, g.Slice{"v1", "v2", "v3"})
|
||||
kArray.Append(k)
|
||||
vArray.Append(v)
|
||||
}
|
||||
gtest.Assert(m.Size(), 0)
|
||||
t.Assert(m.Size(), 0)
|
||||
|
||||
gtest.Assert(kArray.Unique().Len(), 3)
|
||||
gtest.Assert(vArray.Unique().Len(), 3)
|
||||
t.Assert(kArray.Unique().Len(), 3)
|
||||
t.Assert(vArray.Unique().Len(), 3)
|
||||
})
|
||||
}
|
||||
|
||||
func TestAnyAnyMap_UnmarshalValue(t *testing.T) {
|
||||
type T struct {
|
||||
type V struct {
|
||||
Name string
|
||||
Map *gmap.Map
|
||||
}
|
||||
// JSON
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var v *V
|
||||
err := gconv.Struct(map[string]interface{}{
|
||||
"name": "john",
|
||||
"map": []byte(`{"k1":"v1","k2":"v2"}`),
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Map.Size(), 2)
|
||||
gtest.Assert(t.Map.Get("k1"), "v1")
|
||||
gtest.Assert(t.Map.Get("k2"), "v2")
|
||||
}, &v)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(v.Name, "john")
|
||||
t.Assert(v.Map.Size(), 2)
|
||||
t.Assert(v.Map.Get("k1"), "v1")
|
||||
t.Assert(v.Map.Get("k2"), "v2")
|
||||
})
|
||||
// Map
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var v *V
|
||||
err := gconv.Struct(map[string]interface{}{
|
||||
"name": "john",
|
||||
"map": g.Map{
|
||||
"k1": "v1",
|
||||
"k2": "v2",
|
||||
},
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Map.Size(), 2)
|
||||
gtest.Assert(t.Map.Get("k1"), "v1")
|
||||
gtest.Assert(t.Map.Get("k2"), "v2")
|
||||
}, &v)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(v.Name, "john")
|
||||
t.Assert(v.Map.Size(), 2)
|
||||
t.Assert(v.Map.Get("k1"), "v1")
|
||||
t.Assert(v.Map.Get("k2"), "v2")
|
||||
})
|
||||
}
|
||||
|
||||
@ -24,161 +24,178 @@ func intAnyCallBack(int, interface{}) bool {
|
||||
return true
|
||||
}
|
||||
func Test_IntAnyMap_Basic(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gmap.NewIntAnyMap()
|
||||
m.Set(1, 1)
|
||||
|
||||
gtest.Assert(m.Get(1), 1)
|
||||
gtest.Assert(m.Size(), 1)
|
||||
gtest.Assert(m.IsEmpty(), false)
|
||||
t.Assert(m.Get(1), 1)
|
||||
t.Assert(m.Size(), 1)
|
||||
t.Assert(m.IsEmpty(), false)
|
||||
|
||||
gtest.Assert(m.GetOrSet(2, "2"), "2")
|
||||
gtest.Assert(m.SetIfNotExist(2, "2"), false)
|
||||
t.Assert(m.GetOrSet(2, "2"), "2")
|
||||
t.Assert(m.SetIfNotExist(2, "2"), false)
|
||||
|
||||
gtest.Assert(m.SetIfNotExist(3, 3), true)
|
||||
t.Assert(m.SetIfNotExist(3, 3), true)
|
||||
|
||||
gtest.Assert(m.Remove(2), "2")
|
||||
gtest.Assert(m.Contains(2), false)
|
||||
t.Assert(m.Remove(2), "2")
|
||||
t.Assert(m.Contains(2), false)
|
||||
|
||||
gtest.AssertIN(3, m.Keys())
|
||||
gtest.AssertIN(1, m.Keys())
|
||||
gtest.AssertIN(3, m.Values())
|
||||
gtest.AssertIN(1, m.Values())
|
||||
t.AssertIN(3, m.Keys())
|
||||
t.AssertIN(1, m.Keys())
|
||||
t.AssertIN(3, m.Values())
|
||||
t.AssertIN(1, m.Values())
|
||||
m.Flip()
|
||||
gtest.Assert(m.Map(), map[interface{}]int{1: 1, 3: 3})
|
||||
t.Assert(m.Map(), map[interface{}]int{1: 1, 3: 3})
|
||||
|
||||
m.Clear()
|
||||
gtest.Assert(m.Size(), 0)
|
||||
gtest.Assert(m.IsEmpty(), true)
|
||||
t.Assert(m.Size(), 0)
|
||||
t.Assert(m.IsEmpty(), true)
|
||||
|
||||
m2 := gmap.NewIntAnyMapFrom(map[int]interface{}{1: 1, 2: "2"})
|
||||
gtest.Assert(m2.Map(), map[int]interface{}{1: 1, 2: "2"})
|
||||
t.Assert(m2.Map(), map[int]interface{}{1: 1, 2: "2"})
|
||||
})
|
||||
}
|
||||
func Test_IntAnyMap_Set_Fun(t *testing.T) {
|
||||
m := gmap.NewIntAnyMap()
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gmap.NewIntAnyMap()
|
||||
|
||||
m.GetOrSetFunc(1, getAny)
|
||||
m.GetOrSetFuncLock(2, getAny)
|
||||
gtest.Assert(m.Get(1), 123)
|
||||
gtest.Assert(m.Get(2), 123)
|
||||
m.GetOrSetFunc(1, getAny)
|
||||
m.GetOrSetFuncLock(2, getAny)
|
||||
t.Assert(m.Get(1), 123)
|
||||
t.Assert(m.Get(2), 123)
|
||||
|
||||
gtest.Assert(m.SetIfNotExistFunc(1, getAny), false)
|
||||
gtest.Assert(m.SetIfNotExistFunc(3, getAny), true)
|
||||
|
||||
gtest.Assert(m.SetIfNotExistFuncLock(2, getAny), false)
|
||||
gtest.Assert(m.SetIfNotExistFuncLock(4, getAny), true)
|
||||
t.Assert(m.SetIfNotExistFunc(1, getAny), false)
|
||||
t.Assert(m.SetIfNotExistFunc(3, getAny), true)
|
||||
|
||||
t.Assert(m.SetIfNotExistFuncLock(2, getAny), false)
|
||||
t.Assert(m.SetIfNotExistFuncLock(4, getAny), true)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_IntAnyMap_Batch(t *testing.T) {
|
||||
m := gmap.NewIntAnyMap()
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gmap.NewIntAnyMap()
|
||||
|
||||
m.Sets(map[int]interface{}{1: 1, 2: "2", 3: 3})
|
||||
gtest.Assert(m.Map(), map[int]interface{}{1: 1, 2: "2", 3: 3})
|
||||
m.Removes([]int{1, 2})
|
||||
gtest.Assert(m.Map(), map[int]interface{}{3: 3})
|
||||
m.Sets(map[int]interface{}{1: 1, 2: "2", 3: 3})
|
||||
t.Assert(m.Map(), map[int]interface{}{1: 1, 2: "2", 3: 3})
|
||||
m.Removes([]int{1, 2})
|
||||
t.Assert(m.Map(), map[int]interface{}{3: 3})
|
||||
})
|
||||
}
|
||||
func Test_IntAnyMap_Iterator(t *testing.T) {
|
||||
expect := map[int]interface{}{1: 1, 2: "2"}
|
||||
m := gmap.NewIntAnyMapFrom(expect)
|
||||
m.Iterator(func(k int, v interface{}) bool {
|
||||
gtest.Assert(expect[k], v)
|
||||
return true
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
expect := map[int]interface{}{1: 1, 2: "2"}
|
||||
m := gmap.NewIntAnyMapFrom(expect)
|
||||
m.Iterator(func(k int, v interface{}) bool {
|
||||
t.Assert(expect[k], v)
|
||||
return true
|
||||
})
|
||||
// 断言返回值对遍历控制
|
||||
i := 0
|
||||
j := 0
|
||||
m.Iterator(func(k int, v interface{}) bool {
|
||||
i++
|
||||
return true
|
||||
})
|
||||
m.Iterator(func(k int, v interface{}) bool {
|
||||
j++
|
||||
return false
|
||||
})
|
||||
t.Assert(i, "2")
|
||||
t.Assert(j, 1)
|
||||
})
|
||||
// 断言返回值对遍历控制
|
||||
i := 0
|
||||
j := 0
|
||||
m.Iterator(func(k int, v interface{}) bool {
|
||||
i++
|
||||
return true
|
||||
})
|
||||
m.Iterator(func(k int, v interface{}) bool {
|
||||
j++
|
||||
return false
|
||||
})
|
||||
gtest.Assert(i, "2")
|
||||
gtest.Assert(j, 1)
|
||||
|
||||
}
|
||||
|
||||
func Test_IntAnyMap_Lock(t *testing.T) {
|
||||
expect := map[int]interface{}{1: 1, 2: "2"}
|
||||
m := gmap.NewIntAnyMapFrom(expect)
|
||||
m.LockFunc(func(m map[int]interface{}) {
|
||||
gtest.Assert(m, expect)
|
||||
})
|
||||
m.RLockFunc(func(m map[int]interface{}) {
|
||||
gtest.Assert(m, expect)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
expect := map[int]interface{}{1: 1, 2: "2"}
|
||||
m := gmap.NewIntAnyMapFrom(expect)
|
||||
m.LockFunc(func(m map[int]interface{}) {
|
||||
t.Assert(m, expect)
|
||||
})
|
||||
m.RLockFunc(func(m map[int]interface{}) {
|
||||
t.Assert(m, expect)
|
||||
})
|
||||
})
|
||||
}
|
||||
func Test_IntAnyMap_Clone(t *testing.T) {
|
||||
//clone 方法是深克隆
|
||||
m := gmap.NewIntAnyMapFrom(map[int]interface{}{1: 1, 2: "2"})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
//clone 方法是深克隆
|
||||
m := gmap.NewIntAnyMapFrom(map[int]interface{}{1: 1, 2: "2"})
|
||||
|
||||
m_clone := m.Clone()
|
||||
m.Remove(1)
|
||||
//修改原 map,clone 后的 map 不影响
|
||||
gtest.AssertIN(1, m_clone.Keys())
|
||||
m_clone := m.Clone()
|
||||
m.Remove(1)
|
||||
//修改原 map,clone 后的 map 不影响
|
||||
t.AssertIN(1, m_clone.Keys())
|
||||
|
||||
m_clone.Remove(2)
|
||||
//修改clone map,原 map 不影响
|
||||
gtest.AssertIN(2, m.Keys())
|
||||
m_clone.Remove(2)
|
||||
//修改clone map,原 map 不影响
|
||||
t.AssertIN(2, m.Keys())
|
||||
})
|
||||
}
|
||||
func Test_IntAnyMap_Merge(t *testing.T) {
|
||||
m1 := gmap.NewIntAnyMap()
|
||||
m2 := gmap.NewIntAnyMap()
|
||||
m1.Set(1, 1)
|
||||
m2.Set(2, "2")
|
||||
m1.Merge(m2)
|
||||
gtest.Assert(m1.Map(), map[int]interface{}{1: 1, 2: "2"})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m1 := gmap.NewIntAnyMap()
|
||||
m2 := gmap.NewIntAnyMap()
|
||||
m1.Set(1, 1)
|
||||
m2.Set(2, "2")
|
||||
m1.Merge(m2)
|
||||
t.Assert(m1.Map(), map[int]interface{}{1: 1, 2: "2"})
|
||||
})
|
||||
}
|
||||
|
||||
func Test_IntAnyMap_Map(t *testing.T) {
|
||||
m := gmap.NewIntAnyMap()
|
||||
m.Set(1, 0)
|
||||
m.Set(2, 2)
|
||||
gtest.Assert(m.Get(1), 0)
|
||||
gtest.Assert(m.Get(2), 2)
|
||||
data := m.Map()
|
||||
gtest.Assert(data[1], 0)
|
||||
gtest.Assert(data[2], 2)
|
||||
data[3] = 3
|
||||
gtest.Assert(m.Get(3), 3)
|
||||
m.Set(4, 4)
|
||||
gtest.Assert(data[4], 4)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gmap.NewIntAnyMap()
|
||||
m.Set(1, 0)
|
||||
m.Set(2, 2)
|
||||
t.Assert(m.Get(1), 0)
|
||||
t.Assert(m.Get(2), 2)
|
||||
data := m.Map()
|
||||
t.Assert(data[1], 0)
|
||||
t.Assert(data[2], 2)
|
||||
data[3] = 3
|
||||
t.Assert(m.Get(3), 3)
|
||||
m.Set(4, 4)
|
||||
t.Assert(data[4], 4)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_IntAnyMap_MapCopy(t *testing.T) {
|
||||
m := gmap.NewIntAnyMap()
|
||||
m.Set(1, 0)
|
||||
m.Set(2, 2)
|
||||
gtest.Assert(m.Get(1), 0)
|
||||
gtest.Assert(m.Get(2), 2)
|
||||
data := m.MapCopy()
|
||||
gtest.Assert(data[1], 0)
|
||||
gtest.Assert(data[2], 2)
|
||||
data[3] = 3
|
||||
gtest.Assert(m.Get(3), nil)
|
||||
m.Set(4, 4)
|
||||
gtest.Assert(data[4], nil)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gmap.NewIntAnyMap()
|
||||
m.Set(1, 0)
|
||||
m.Set(2, 2)
|
||||
t.Assert(m.Get(1), 0)
|
||||
t.Assert(m.Get(2), 2)
|
||||
data := m.MapCopy()
|
||||
t.Assert(data[1], 0)
|
||||
t.Assert(data[2], 2)
|
||||
data[3] = 3
|
||||
t.Assert(m.Get(3), nil)
|
||||
m.Set(4, 4)
|
||||
t.Assert(data[4], nil)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_IntAnyMap_FilterEmpty(t *testing.T) {
|
||||
m := gmap.NewIntAnyMap()
|
||||
m.Set(1, 0)
|
||||
m.Set(2, 2)
|
||||
gtest.Assert(m.Size(), 2)
|
||||
gtest.Assert(m.Get(1), 0)
|
||||
gtest.Assert(m.Get(2), 2)
|
||||
m.FilterEmpty()
|
||||
gtest.Assert(m.Size(), 1)
|
||||
gtest.Assert(m.Get(2), 2)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gmap.NewIntAnyMap()
|
||||
m.Set(1, 0)
|
||||
m.Set(2, 2)
|
||||
t.Assert(m.Size(), 2)
|
||||
t.Assert(m.Get(1), 0)
|
||||
t.Assert(m.Get(2), 2)
|
||||
m.FilterEmpty()
|
||||
t.Assert(m.Size(), 1)
|
||||
t.Assert(m.Get(2), 2)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_IntAnyMap_Json(t *testing.T) {
|
||||
// Marshal
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
data := g.MapIntAny{
|
||||
1: "v1",
|
||||
2: "v2",
|
||||
@ -186,111 +203,111 @@ func Test_IntAnyMap_Json(t *testing.T) {
|
||||
m1 := gmap.NewIntAnyMapFrom(data)
|
||||
b1, err1 := json.Marshal(m1)
|
||||
b2, err2 := json.Marshal(data)
|
||||
gtest.Assert(err1, err2)
|
||||
gtest.Assert(b1, b2)
|
||||
t.Assert(err1, err2)
|
||||
t.Assert(b1, b2)
|
||||
})
|
||||
// Unmarshal
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
data := g.MapIntAny{
|
||||
1: "v1",
|
||||
2: "v2",
|
||||
}
|
||||
b, err := json.Marshal(data)
|
||||
gtest.Assert(err, nil)
|
||||
t.Assert(err, nil)
|
||||
|
||||
m := gmap.NewIntAnyMap()
|
||||
err = json.Unmarshal(b, m)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(m.Get(1), data[1])
|
||||
gtest.Assert(m.Get(2), data[2])
|
||||
t.Assert(err, nil)
|
||||
t.Assert(m.Get(1), data[1])
|
||||
t.Assert(m.Get(2), data[2])
|
||||
})
|
||||
}
|
||||
|
||||
func Test_IntAnyMap_Pop(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gmap.NewIntAnyMapFrom(g.MapIntAny{
|
||||
1: "v1",
|
||||
2: "v2",
|
||||
})
|
||||
gtest.Assert(m.Size(), 2)
|
||||
t.Assert(m.Size(), 2)
|
||||
|
||||
k1, v1 := m.Pop()
|
||||
gtest.AssertIN(k1, g.Slice{1, 2})
|
||||
gtest.AssertIN(v1, g.Slice{"v1", "v2"})
|
||||
gtest.Assert(m.Size(), 1)
|
||||
t.AssertIN(k1, g.Slice{1, 2})
|
||||
t.AssertIN(v1, g.Slice{"v1", "v2"})
|
||||
t.Assert(m.Size(), 1)
|
||||
k2, v2 := m.Pop()
|
||||
gtest.AssertIN(k2, g.Slice{1, 2})
|
||||
gtest.AssertIN(v2, g.Slice{"v1", "v2"})
|
||||
gtest.Assert(m.Size(), 0)
|
||||
t.AssertIN(k2, g.Slice{1, 2})
|
||||
t.AssertIN(v2, g.Slice{"v1", "v2"})
|
||||
t.Assert(m.Size(), 0)
|
||||
|
||||
gtest.AssertNE(k1, k2)
|
||||
gtest.AssertNE(v1, v2)
|
||||
t.AssertNE(k1, k2)
|
||||
t.AssertNE(v1, v2)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_IntAnyMap_Pops(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gmap.NewIntAnyMapFrom(g.MapIntAny{
|
||||
1: "v1",
|
||||
2: "v2",
|
||||
3: "v3",
|
||||
})
|
||||
gtest.Assert(m.Size(), 3)
|
||||
t.Assert(m.Size(), 3)
|
||||
|
||||
kArray := garray.New()
|
||||
vArray := garray.New()
|
||||
for k, v := range m.Pops(1) {
|
||||
gtest.AssertIN(k, g.Slice{1, 2, 3})
|
||||
gtest.AssertIN(v, g.Slice{"v1", "v2", "v3"})
|
||||
t.AssertIN(k, g.Slice{1, 2, 3})
|
||||
t.AssertIN(v, g.Slice{"v1", "v2", "v3"})
|
||||
kArray.Append(k)
|
||||
vArray.Append(v)
|
||||
}
|
||||
gtest.Assert(m.Size(), 2)
|
||||
t.Assert(m.Size(), 2)
|
||||
for k, v := range m.Pops(2) {
|
||||
gtest.AssertIN(k, g.Slice{1, 2, 3})
|
||||
gtest.AssertIN(v, g.Slice{"v1", "v2", "v3"})
|
||||
t.AssertIN(k, g.Slice{1, 2, 3})
|
||||
t.AssertIN(v, g.Slice{"v1", "v2", "v3"})
|
||||
kArray.Append(k)
|
||||
vArray.Append(v)
|
||||
}
|
||||
gtest.Assert(m.Size(), 0)
|
||||
t.Assert(m.Size(), 0)
|
||||
|
||||
gtest.Assert(kArray.Unique().Len(), 3)
|
||||
gtest.Assert(vArray.Unique().Len(), 3)
|
||||
t.Assert(kArray.Unique().Len(), 3)
|
||||
t.Assert(vArray.Unique().Len(), 3)
|
||||
})
|
||||
}
|
||||
|
||||
func TestIntAnyMap_UnmarshalValue(t *testing.T) {
|
||||
type T struct {
|
||||
type V struct {
|
||||
Name string
|
||||
Map *gmap.IntAnyMap
|
||||
}
|
||||
// JSON
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var v *V
|
||||
err := gconv.Struct(map[string]interface{}{
|
||||
"name": "john",
|
||||
"map": []byte(`{"1":"v1","2":"v2"}`),
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Map.Size(), 2)
|
||||
gtest.Assert(t.Map.Get(1), "v1")
|
||||
gtest.Assert(t.Map.Get(2), "v2")
|
||||
}, &v)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(v.Name, "john")
|
||||
t.Assert(v.Map.Size(), 2)
|
||||
t.Assert(v.Map.Get(1), "v1")
|
||||
t.Assert(v.Map.Get(2), "v2")
|
||||
})
|
||||
// Map
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var v *V
|
||||
err := gconv.Struct(map[string]interface{}{
|
||||
"name": "john",
|
||||
"map": g.MapIntAny{
|
||||
1: "v1",
|
||||
2: "v2",
|
||||
},
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Map.Size(), 2)
|
||||
gtest.Assert(t.Map.Get(1), "v1")
|
||||
gtest.Assert(t.Map.Get(2), "v2")
|
||||
}, &v)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(v.Name, "john")
|
||||
t.Assert(v.Map.Size(), 2)
|
||||
t.Assert(v.Map.Get(1), "v1")
|
||||
t.Assert(v.Map.Get(2), "v2")
|
||||
})
|
||||
}
|
||||
|
||||
@ -24,164 +24,180 @@ func intIntCallBack(int, int) bool {
|
||||
return true
|
||||
}
|
||||
func Test_IntIntMap_Basic(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gmap.NewIntIntMap()
|
||||
m.Set(1, 1)
|
||||
|
||||
gtest.Assert(m.Get(1), 1)
|
||||
gtest.Assert(m.Size(), 1)
|
||||
gtest.Assert(m.IsEmpty(), false)
|
||||
t.Assert(m.Get(1), 1)
|
||||
t.Assert(m.Size(), 1)
|
||||
t.Assert(m.IsEmpty(), false)
|
||||
|
||||
gtest.Assert(m.GetOrSet(2, 2), 2)
|
||||
gtest.Assert(m.SetIfNotExist(2, 2), false)
|
||||
t.Assert(m.GetOrSet(2, 2), 2)
|
||||
t.Assert(m.SetIfNotExist(2, 2), false)
|
||||
|
||||
gtest.Assert(m.SetIfNotExist(3, 3), true)
|
||||
t.Assert(m.SetIfNotExist(3, 3), true)
|
||||
|
||||
gtest.Assert(m.Remove(2), 2)
|
||||
gtest.Assert(m.Contains(2), false)
|
||||
t.Assert(m.Remove(2), 2)
|
||||
t.Assert(m.Contains(2), false)
|
||||
|
||||
gtest.AssertIN(3, m.Keys())
|
||||
gtest.AssertIN(1, m.Keys())
|
||||
gtest.AssertIN(3, m.Values())
|
||||
gtest.AssertIN(1, m.Values())
|
||||
t.AssertIN(3, m.Keys())
|
||||
t.AssertIN(1, m.Keys())
|
||||
t.AssertIN(3, m.Values())
|
||||
t.AssertIN(1, m.Values())
|
||||
m.Flip()
|
||||
gtest.Assert(m.Map(), map[int]int{1: 1, 3: 3})
|
||||
t.Assert(m.Map(), map[int]int{1: 1, 3: 3})
|
||||
|
||||
m.Clear()
|
||||
gtest.Assert(m.Size(), 0)
|
||||
gtest.Assert(m.IsEmpty(), true)
|
||||
t.Assert(m.Size(), 0)
|
||||
t.Assert(m.IsEmpty(), true)
|
||||
|
||||
m2 := gmap.NewIntIntMapFrom(map[int]int{1: 1, 2: 2})
|
||||
gtest.Assert(m2.Map(), map[int]int{1: 1, 2: 2})
|
||||
t.Assert(m2.Map(), map[int]int{1: 1, 2: 2})
|
||||
})
|
||||
}
|
||||
func Test_IntIntMap_Set_Fun(t *testing.T) {
|
||||
m := gmap.NewIntIntMap()
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gmap.NewIntIntMap()
|
||||
|
||||
m.GetOrSetFunc(1, getInt)
|
||||
m.GetOrSetFuncLock(2, getInt)
|
||||
gtest.Assert(m.Get(1), 123)
|
||||
gtest.Assert(m.Get(2), 123)
|
||||
gtest.Assert(m.SetIfNotExistFunc(1, getInt), false)
|
||||
gtest.Assert(m.SetIfNotExistFunc(3, getInt), true)
|
||||
|
||||
gtest.Assert(m.SetIfNotExistFuncLock(2, getInt), false)
|
||||
gtest.Assert(m.SetIfNotExistFuncLock(4, getInt), true)
|
||||
m.GetOrSetFunc(1, getInt)
|
||||
m.GetOrSetFuncLock(2, getInt)
|
||||
t.Assert(m.Get(1), 123)
|
||||
t.Assert(m.Get(2), 123)
|
||||
t.Assert(m.SetIfNotExistFunc(1, getInt), false)
|
||||
t.Assert(m.SetIfNotExistFunc(3, getInt), true)
|
||||
|
||||
t.Assert(m.SetIfNotExistFuncLock(2, getInt), false)
|
||||
t.Assert(m.SetIfNotExistFuncLock(4, getInt), true)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_IntIntMap_Batch(t *testing.T) {
|
||||
m := gmap.NewIntIntMap()
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gmap.NewIntIntMap()
|
||||
|
||||
m.Sets(map[int]int{1: 1, 2: 2, 3: 3})
|
||||
m.Iterator(intIntCallBack)
|
||||
gtest.Assert(m.Map(), map[int]int{1: 1, 2: 2, 3: 3})
|
||||
m.Removes([]int{1, 2})
|
||||
gtest.Assert(m.Map(), map[int]int{3: 3})
|
||||
m.Sets(map[int]int{1: 1, 2: 2, 3: 3})
|
||||
m.Iterator(intIntCallBack)
|
||||
t.Assert(m.Map(), map[int]int{1: 1, 2: 2, 3: 3})
|
||||
m.Removes([]int{1, 2})
|
||||
t.Assert(m.Map(), map[int]int{3: 3})
|
||||
})
|
||||
}
|
||||
|
||||
func Test_IntIntMap_Iterator(t *testing.T) {
|
||||
expect := map[int]int{1: 1, 2: 2}
|
||||
m := gmap.NewIntIntMapFrom(expect)
|
||||
m.Iterator(func(k int, v int) bool {
|
||||
gtest.Assert(expect[k], v)
|
||||
return true
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
expect := map[int]int{1: 1, 2: 2}
|
||||
m := gmap.NewIntIntMapFrom(expect)
|
||||
m.Iterator(func(k int, v int) bool {
|
||||
t.Assert(expect[k], v)
|
||||
return true
|
||||
})
|
||||
// 断言返回值对遍历控制
|
||||
i := 0
|
||||
j := 0
|
||||
m.Iterator(func(k int, v int) bool {
|
||||
i++
|
||||
return true
|
||||
})
|
||||
m.Iterator(func(k int, v int) bool {
|
||||
j++
|
||||
return false
|
||||
})
|
||||
t.Assert(i, 2)
|
||||
t.Assert(j, 1)
|
||||
})
|
||||
// 断言返回值对遍历控制
|
||||
i := 0
|
||||
j := 0
|
||||
m.Iterator(func(k int, v int) bool {
|
||||
i++
|
||||
return true
|
||||
})
|
||||
m.Iterator(func(k int, v int) bool {
|
||||
j++
|
||||
return false
|
||||
})
|
||||
gtest.Assert(i, 2)
|
||||
gtest.Assert(j, 1)
|
||||
}
|
||||
|
||||
func Test_IntIntMap_Lock(t *testing.T) {
|
||||
expect := map[int]int{1: 1, 2: 2}
|
||||
m := gmap.NewIntIntMapFrom(expect)
|
||||
m.LockFunc(func(m map[int]int) {
|
||||
gtest.Assert(m, expect)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
expect := map[int]int{1: 1, 2: 2}
|
||||
m := gmap.NewIntIntMapFrom(expect)
|
||||
m.LockFunc(func(m map[int]int) {
|
||||
t.Assert(m, expect)
|
||||
})
|
||||
m.RLockFunc(func(m map[int]int) {
|
||||
t.Assert(m, expect)
|
||||
})
|
||||
})
|
||||
m.RLockFunc(func(m map[int]int) {
|
||||
gtest.Assert(m, expect)
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
func Test_IntIntMap_Clone(t *testing.T) {
|
||||
//clone 方法是深克隆
|
||||
m := gmap.NewIntIntMapFrom(map[int]int{1: 1, 2: 2})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
//clone 方法是深克隆
|
||||
m := gmap.NewIntIntMapFrom(map[int]int{1: 1, 2: 2})
|
||||
|
||||
m_clone := m.Clone()
|
||||
m.Remove(1)
|
||||
//修改原 map,clone 后的 map 不影响
|
||||
gtest.AssertIN(1, m_clone.Keys())
|
||||
m_clone := m.Clone()
|
||||
m.Remove(1)
|
||||
//修改原 map,clone 后的 map 不影响
|
||||
t.AssertIN(1, m_clone.Keys())
|
||||
|
||||
m_clone.Remove(2)
|
||||
//修改clone map,原 map 不影响
|
||||
gtest.AssertIN(2, m.Keys())
|
||||
m_clone.Remove(2)
|
||||
//修改clone map,原 map 不影响
|
||||
t.AssertIN(2, m.Keys())
|
||||
})
|
||||
}
|
||||
|
||||
func Test_IntIntMap_Merge(t *testing.T) {
|
||||
m1 := gmap.NewIntIntMap()
|
||||
m2 := gmap.NewIntIntMap()
|
||||
m1.Set(1, 1)
|
||||
m2.Set(2, 2)
|
||||
m1.Merge(m2)
|
||||
gtest.Assert(m1.Map(), map[int]int{1: 1, 2: 2})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m1 := gmap.NewIntIntMap()
|
||||
m2 := gmap.NewIntIntMap()
|
||||
m1.Set(1, 1)
|
||||
m2.Set(2, 2)
|
||||
m1.Merge(m2)
|
||||
t.Assert(m1.Map(), map[int]int{1: 1, 2: 2})
|
||||
})
|
||||
}
|
||||
|
||||
func Test_IntIntMap_Map(t *testing.T) {
|
||||
m := gmap.NewIntIntMap()
|
||||
m.Set(1, 0)
|
||||
m.Set(2, 2)
|
||||
gtest.Assert(m.Get(1), 0)
|
||||
gtest.Assert(m.Get(2), 2)
|
||||
data := m.Map()
|
||||
gtest.Assert(data[1], 0)
|
||||
gtest.Assert(data[2], 2)
|
||||
data[3] = 3
|
||||
gtest.Assert(m.Get(3), 3)
|
||||
m.Set(4, 4)
|
||||
gtest.Assert(data[4], 4)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gmap.NewIntIntMap()
|
||||
m.Set(1, 0)
|
||||
m.Set(2, 2)
|
||||
t.Assert(m.Get(1), 0)
|
||||
t.Assert(m.Get(2), 2)
|
||||
data := m.Map()
|
||||
t.Assert(data[1], 0)
|
||||
t.Assert(data[2], 2)
|
||||
data[3] = 3
|
||||
t.Assert(m.Get(3), 3)
|
||||
m.Set(4, 4)
|
||||
t.Assert(data[4], 4)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_IntIntMap_MapCopy(t *testing.T) {
|
||||
m := gmap.NewIntIntMap()
|
||||
m.Set(1, 0)
|
||||
m.Set(2, 2)
|
||||
gtest.Assert(m.Get(1), 0)
|
||||
gtest.Assert(m.Get(2), 2)
|
||||
data := m.MapCopy()
|
||||
gtest.Assert(data[1], 0)
|
||||
gtest.Assert(data[2], 2)
|
||||
data[3] = 3
|
||||
gtest.Assert(m.Get(3), 0)
|
||||
m.Set(4, 4)
|
||||
gtest.Assert(data[4], 0)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gmap.NewIntIntMap()
|
||||
m.Set(1, 0)
|
||||
m.Set(2, 2)
|
||||
t.Assert(m.Get(1), 0)
|
||||
t.Assert(m.Get(2), 2)
|
||||
data := m.MapCopy()
|
||||
t.Assert(data[1], 0)
|
||||
t.Assert(data[2], 2)
|
||||
data[3] = 3
|
||||
t.Assert(m.Get(3), 0)
|
||||
m.Set(4, 4)
|
||||
t.Assert(data[4], 0)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_IntIntMap_FilterEmpty(t *testing.T) {
|
||||
m := gmap.NewIntIntMap()
|
||||
m.Set(1, 0)
|
||||
m.Set(2, 2)
|
||||
gtest.Assert(m.Size(), 2)
|
||||
gtest.Assert(m.Get(1), 0)
|
||||
gtest.Assert(m.Get(2), 2)
|
||||
m.FilterEmpty()
|
||||
gtest.Assert(m.Size(), 1)
|
||||
gtest.Assert(m.Get(2), 2)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gmap.NewIntIntMap()
|
||||
m.Set(1, 0)
|
||||
m.Set(2, 2)
|
||||
t.Assert(m.Size(), 2)
|
||||
t.Assert(m.Get(1), 0)
|
||||
t.Assert(m.Get(2), 2)
|
||||
m.FilterEmpty()
|
||||
t.Assert(m.Size(), 1)
|
||||
t.Assert(m.Get(2), 2)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_IntIntMap_Json(t *testing.T) {
|
||||
// Marshal
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
data := g.MapIntInt{
|
||||
1: 10,
|
||||
2: 20,
|
||||
@ -189,111 +205,111 @@ func Test_IntIntMap_Json(t *testing.T) {
|
||||
m1 := gmap.NewIntIntMapFrom(data)
|
||||
b1, err1 := json.Marshal(m1)
|
||||
b2, err2 := json.Marshal(data)
|
||||
gtest.Assert(err1, err2)
|
||||
gtest.Assert(b1, b2)
|
||||
t.Assert(err1, err2)
|
||||
t.Assert(b1, b2)
|
||||
})
|
||||
// Unmarshal
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
data := g.MapIntInt{
|
||||
1: 10,
|
||||
2: 20,
|
||||
}
|
||||
b, err := json.Marshal(data)
|
||||
gtest.Assert(err, nil)
|
||||
t.Assert(err, nil)
|
||||
|
||||
m := gmap.NewIntIntMap()
|
||||
err = json.Unmarshal(b, m)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(m.Get(1), data[1])
|
||||
gtest.Assert(m.Get(2), data[2])
|
||||
t.Assert(err, nil)
|
||||
t.Assert(m.Get(1), data[1])
|
||||
t.Assert(m.Get(2), data[2])
|
||||
})
|
||||
}
|
||||
|
||||
func Test_IntIntMap_Pop(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gmap.NewIntIntMapFrom(g.MapIntInt{
|
||||
1: 11,
|
||||
2: 22,
|
||||
})
|
||||
gtest.Assert(m.Size(), 2)
|
||||
t.Assert(m.Size(), 2)
|
||||
|
||||
k1, v1 := m.Pop()
|
||||
gtest.AssertIN(k1, g.Slice{1, 2})
|
||||
gtest.AssertIN(v1, g.Slice{11, 22})
|
||||
gtest.Assert(m.Size(), 1)
|
||||
t.AssertIN(k1, g.Slice{1, 2})
|
||||
t.AssertIN(v1, g.Slice{11, 22})
|
||||
t.Assert(m.Size(), 1)
|
||||
k2, v2 := m.Pop()
|
||||
gtest.AssertIN(k2, g.Slice{1, 2})
|
||||
gtest.AssertIN(v2, g.Slice{11, 22})
|
||||
gtest.Assert(m.Size(), 0)
|
||||
t.AssertIN(k2, g.Slice{1, 2})
|
||||
t.AssertIN(v2, g.Slice{11, 22})
|
||||
t.Assert(m.Size(), 0)
|
||||
|
||||
gtest.AssertNE(k1, k2)
|
||||
gtest.AssertNE(v1, v2)
|
||||
t.AssertNE(k1, k2)
|
||||
t.AssertNE(v1, v2)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_IntIntMap_Pops(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gmap.NewIntIntMapFrom(g.MapIntInt{
|
||||
1: 11,
|
||||
2: 22,
|
||||
3: 33,
|
||||
})
|
||||
gtest.Assert(m.Size(), 3)
|
||||
t.Assert(m.Size(), 3)
|
||||
|
||||
kArray := garray.New()
|
||||
vArray := garray.New()
|
||||
for k, v := range m.Pops(1) {
|
||||
gtest.AssertIN(k, g.Slice{1, 2, 3})
|
||||
gtest.AssertIN(v, g.Slice{11, 22, 33})
|
||||
t.AssertIN(k, g.Slice{1, 2, 3})
|
||||
t.AssertIN(v, g.Slice{11, 22, 33})
|
||||
kArray.Append(k)
|
||||
vArray.Append(v)
|
||||
}
|
||||
gtest.Assert(m.Size(), 2)
|
||||
t.Assert(m.Size(), 2)
|
||||
for k, v := range m.Pops(2) {
|
||||
gtest.AssertIN(k, g.Slice{1, 2, 3})
|
||||
gtest.AssertIN(v, g.Slice{11, 22, 33})
|
||||
t.AssertIN(k, g.Slice{1, 2, 3})
|
||||
t.AssertIN(v, g.Slice{11, 22, 33})
|
||||
kArray.Append(k)
|
||||
vArray.Append(v)
|
||||
}
|
||||
gtest.Assert(m.Size(), 0)
|
||||
t.Assert(m.Size(), 0)
|
||||
|
||||
gtest.Assert(kArray.Unique().Len(), 3)
|
||||
gtest.Assert(vArray.Unique().Len(), 3)
|
||||
t.Assert(kArray.Unique().Len(), 3)
|
||||
t.Assert(vArray.Unique().Len(), 3)
|
||||
})
|
||||
}
|
||||
|
||||
func TestIntIntMap_UnmarshalValue(t *testing.T) {
|
||||
type T struct {
|
||||
type V struct {
|
||||
Name string
|
||||
Map *gmap.IntIntMap
|
||||
}
|
||||
// JSON
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var v *V
|
||||
err := gconv.Struct(map[string]interface{}{
|
||||
"name": "john",
|
||||
"map": []byte(`{"1":1,"2":2}`),
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Map.Size(), 2)
|
||||
gtest.Assert(t.Map.Get(1), "1")
|
||||
gtest.Assert(t.Map.Get(2), "2")
|
||||
}, &v)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(v.Name, "john")
|
||||
t.Assert(v.Map.Size(), 2)
|
||||
t.Assert(v.Map.Get(1), "1")
|
||||
t.Assert(v.Map.Get(2), "2")
|
||||
})
|
||||
// Map
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var v *V
|
||||
err := gconv.Struct(map[string]interface{}{
|
||||
"name": "john",
|
||||
"map": g.MapIntAny{
|
||||
1: 1,
|
||||
2: 2,
|
||||
},
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Map.Size(), 2)
|
||||
gtest.Assert(t.Map.Get(1), "1")
|
||||
gtest.Assert(t.Map.Get(2), "2")
|
||||
}, &v)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(v.Name, "john")
|
||||
t.Assert(v.Map.Size(), 2)
|
||||
t.Assert(v.Map.Get(1), "1")
|
||||
t.Assert(v.Map.Get(2), "2")
|
||||
})
|
||||
}
|
||||
|
||||
@ -24,165 +24,179 @@ func intStrCallBack(int, string) bool {
|
||||
return true
|
||||
}
|
||||
func Test_IntStrMap_Basic(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gmap.NewIntStrMap()
|
||||
m.Set(1, "a")
|
||||
|
||||
gtest.Assert(m.Get(1), "a")
|
||||
gtest.Assert(m.Size(), 1)
|
||||
gtest.Assert(m.IsEmpty(), false)
|
||||
t.Assert(m.Get(1), "a")
|
||||
t.Assert(m.Size(), 1)
|
||||
t.Assert(m.IsEmpty(), false)
|
||||
|
||||
gtest.Assert(m.GetOrSet(2, "b"), "b")
|
||||
gtest.Assert(m.SetIfNotExist(2, "b"), false)
|
||||
t.Assert(m.GetOrSet(2, "b"), "b")
|
||||
t.Assert(m.SetIfNotExist(2, "b"), false)
|
||||
|
||||
gtest.Assert(m.SetIfNotExist(3, "c"), true)
|
||||
t.Assert(m.SetIfNotExist(3, "c"), true)
|
||||
|
||||
gtest.Assert(m.Remove(2), "b")
|
||||
gtest.Assert(m.Contains(2), false)
|
||||
t.Assert(m.Remove(2), "b")
|
||||
t.Assert(m.Contains(2), false)
|
||||
|
||||
gtest.AssertIN(3, m.Keys())
|
||||
gtest.AssertIN(1, m.Keys())
|
||||
gtest.AssertIN("a", m.Values())
|
||||
gtest.AssertIN("c", m.Values())
|
||||
t.AssertIN(3, m.Keys())
|
||||
t.AssertIN(1, m.Keys())
|
||||
t.AssertIN("a", m.Values())
|
||||
t.AssertIN("c", m.Values())
|
||||
|
||||
//反转之后不成为以下 map,flip 操作只是翻转原 map
|
||||
//gtest.Assert(m.Map(), map[string]int{"a": 1, "c": 3})
|
||||
//t.Assert(m.Map(), map[string]int{"a": 1, "c": 3})
|
||||
m_f := gmap.NewIntStrMap()
|
||||
m_f.Set(1, "2")
|
||||
m_f.Flip()
|
||||
gtest.Assert(m_f.Map(), map[int]string{2: "1"})
|
||||
t.Assert(m_f.Map(), map[int]string{2: "1"})
|
||||
|
||||
m.Clear()
|
||||
gtest.Assert(m.Size(), 0)
|
||||
gtest.Assert(m.IsEmpty(), true)
|
||||
t.Assert(m.Size(), 0)
|
||||
t.Assert(m.IsEmpty(), true)
|
||||
|
||||
m2 := gmap.NewIntStrMapFrom(map[int]string{1: "a", 2: "b"})
|
||||
gtest.Assert(m2.Map(), map[int]string{1: "a", 2: "b"})
|
||||
t.Assert(m2.Map(), map[int]string{1: "a", 2: "b"})
|
||||
})
|
||||
}
|
||||
func Test_IntStrMap_Set_Fun(t *testing.T) {
|
||||
m := gmap.NewIntStrMap()
|
||||
|
||||
m.GetOrSetFunc(1, getStr)
|
||||
m.GetOrSetFuncLock(2, getStr)
|
||||
gtest.Assert(m.Get(1), "z")
|
||||
gtest.Assert(m.Get(2), "z")
|
||||
gtest.Assert(m.SetIfNotExistFunc(1, getStr), false)
|
||||
gtest.Assert(m.SetIfNotExistFunc(3, getStr), true)
|
||||
|
||||
gtest.Assert(m.SetIfNotExistFuncLock(2, getStr), false)
|
||||
gtest.Assert(m.SetIfNotExistFuncLock(4, getStr), true)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gmap.NewIntStrMap()
|
||||
m.GetOrSetFunc(1, getStr)
|
||||
m.GetOrSetFuncLock(2, getStr)
|
||||
t.Assert(m.Get(1), "z")
|
||||
t.Assert(m.Get(2), "z")
|
||||
t.Assert(m.SetIfNotExistFunc(1, getStr), false)
|
||||
t.Assert(m.SetIfNotExistFunc(3, getStr), true)
|
||||
|
||||
t.Assert(m.SetIfNotExistFuncLock(2, getStr), false)
|
||||
t.Assert(m.SetIfNotExistFuncLock(4, getStr), true)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_IntStrMap_Batch(t *testing.T) {
|
||||
m := gmap.NewIntStrMap()
|
||||
|
||||
m.Sets(map[int]string{1: "a", 2: "b", 3: "c"})
|
||||
gtest.Assert(m.Map(), map[int]string{1: "a", 2: "b", 3: "c"})
|
||||
m.Removes([]int{1, 2})
|
||||
gtest.Assert(m.Map(), map[int]interface{}{3: "c"})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gmap.NewIntStrMap()
|
||||
m.Sets(map[int]string{1: "a", 2: "b", 3: "c"})
|
||||
t.Assert(m.Map(), map[int]string{1: "a", 2: "b", 3: "c"})
|
||||
m.Removes([]int{1, 2})
|
||||
t.Assert(m.Map(), map[int]interface{}{3: "c"})
|
||||
})
|
||||
}
|
||||
func Test_IntStrMap_Iterator(t *testing.T) {
|
||||
expect := map[int]string{1: "a", 2: "b"}
|
||||
m := gmap.NewIntStrMapFrom(expect)
|
||||
m.Iterator(func(k int, v string) bool {
|
||||
gtest.Assert(expect[k], v)
|
||||
return true
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
expect := map[int]string{1: "a", 2: "b"}
|
||||
m := gmap.NewIntStrMapFrom(expect)
|
||||
m.Iterator(func(k int, v string) bool {
|
||||
t.Assert(expect[k], v)
|
||||
return true
|
||||
})
|
||||
// 断言返回值对遍历控制
|
||||
i := 0
|
||||
j := 0
|
||||
m.Iterator(func(k int, v string) bool {
|
||||
i++
|
||||
return true
|
||||
})
|
||||
m.Iterator(func(k int, v string) bool {
|
||||
j++
|
||||
return false
|
||||
})
|
||||
t.Assert(i, 2)
|
||||
t.Assert(j, 1)
|
||||
})
|
||||
// 断言返回值对遍历控制
|
||||
i := 0
|
||||
j := 0
|
||||
m.Iterator(func(k int, v string) bool {
|
||||
i++
|
||||
return true
|
||||
})
|
||||
m.Iterator(func(k int, v string) bool {
|
||||
j++
|
||||
return false
|
||||
})
|
||||
gtest.Assert(i, 2)
|
||||
gtest.Assert(j, 1)
|
||||
}
|
||||
|
||||
func Test_IntStrMap_Lock(t *testing.T) {
|
||||
|
||||
expect := map[int]string{1: "a", 2: "b", 3: "c"}
|
||||
m := gmap.NewIntStrMapFrom(expect)
|
||||
m.LockFunc(func(m map[int]string) {
|
||||
gtest.Assert(m, expect)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
expect := map[int]string{1: "a", 2: "b", 3: "c"}
|
||||
m := gmap.NewIntStrMapFrom(expect)
|
||||
m.LockFunc(func(m map[int]string) {
|
||||
t.Assert(m, expect)
|
||||
})
|
||||
m.RLockFunc(func(m map[int]string) {
|
||||
t.Assert(m, expect)
|
||||
})
|
||||
})
|
||||
m.RLockFunc(func(m map[int]string) {
|
||||
gtest.Assert(m, expect)
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
func Test_IntStrMap_Clone(t *testing.T) {
|
||||
//clone 方法是深克隆
|
||||
m := gmap.NewIntStrMapFrom(map[int]string{1: "a", 2: "b", 3: "c"})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
//clone 方法是深克隆
|
||||
m := gmap.NewIntStrMapFrom(map[int]string{1: "a", 2: "b", 3: "c"})
|
||||
|
||||
m_clone := m.Clone()
|
||||
m.Remove(1)
|
||||
//修改原 map,clone 后的 map 不影响
|
||||
gtest.AssertIN(1, m_clone.Keys())
|
||||
m_clone := m.Clone()
|
||||
m.Remove(1)
|
||||
//修改原 map,clone 后的 map 不影响
|
||||
t.AssertIN(1, m_clone.Keys())
|
||||
|
||||
m_clone.Remove(2)
|
||||
//修改clone map,原 map 不影响
|
||||
gtest.AssertIN(2, m.Keys())
|
||||
m_clone.Remove(2)
|
||||
//修改clone map,原 map 不影响
|
||||
t.AssertIN(2, m.Keys())
|
||||
})
|
||||
}
|
||||
func Test_IntStrMap_Merge(t *testing.T) {
|
||||
m1 := gmap.NewIntStrMap()
|
||||
m2 := gmap.NewIntStrMap()
|
||||
m1.Set(1, "a")
|
||||
m2.Set(2, "b")
|
||||
m1.Merge(m2)
|
||||
gtest.Assert(m1.Map(), map[int]string{1: "a", 2: "b"})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m1 := gmap.NewIntStrMap()
|
||||
m2 := gmap.NewIntStrMap()
|
||||
m1.Set(1, "a")
|
||||
m2.Set(2, "b")
|
||||
m1.Merge(m2)
|
||||
t.Assert(m1.Map(), map[int]string{1: "a", 2: "b"})
|
||||
})
|
||||
}
|
||||
|
||||
func Test_IntStrMap_Map(t *testing.T) {
|
||||
m := gmap.NewIntStrMap()
|
||||
m.Set(1, "0")
|
||||
m.Set(2, "2")
|
||||
gtest.Assert(m.Get(1), "0")
|
||||
gtest.Assert(m.Get(2), "2")
|
||||
data := m.Map()
|
||||
gtest.Assert(data[1], "0")
|
||||
gtest.Assert(data[2], "2")
|
||||
data[3] = "3"
|
||||
gtest.Assert(m.Get(3), "3")
|
||||
m.Set(4, "4")
|
||||
gtest.Assert(data[4], "4")
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gmap.NewIntStrMap()
|
||||
m.Set(1, "0")
|
||||
m.Set(2, "2")
|
||||
t.Assert(m.Get(1), "0")
|
||||
t.Assert(m.Get(2), "2")
|
||||
data := m.Map()
|
||||
t.Assert(data[1], "0")
|
||||
t.Assert(data[2], "2")
|
||||
data[3] = "3"
|
||||
t.Assert(m.Get(3), "3")
|
||||
m.Set(4, "4")
|
||||
t.Assert(data[4], "4")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_IntStrMap_MapCopy(t *testing.T) {
|
||||
m := gmap.NewIntStrMap()
|
||||
m.Set(1, "0")
|
||||
m.Set(2, "2")
|
||||
gtest.Assert(m.Get(1), "0")
|
||||
gtest.Assert(m.Get(2), "2")
|
||||
data := m.MapCopy()
|
||||
gtest.Assert(data[1], "0")
|
||||
gtest.Assert(data[2], "2")
|
||||
data[3] = "3"
|
||||
gtest.Assert(m.Get(3), "")
|
||||
m.Set(4, "4")
|
||||
gtest.Assert(data[4], "")
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gmap.NewIntStrMap()
|
||||
m.Set(1, "0")
|
||||
m.Set(2, "2")
|
||||
t.Assert(m.Get(1), "0")
|
||||
t.Assert(m.Get(2), "2")
|
||||
data := m.MapCopy()
|
||||
t.Assert(data[1], "0")
|
||||
t.Assert(data[2], "2")
|
||||
data[3] = "3"
|
||||
t.Assert(m.Get(3), "")
|
||||
m.Set(4, "4")
|
||||
t.Assert(data[4], "")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_IntStrMap_FilterEmpty(t *testing.T) {
|
||||
m := gmap.NewIntStrMap()
|
||||
m.Set(1, "")
|
||||
m.Set(2, "2")
|
||||
gtest.Assert(m.Size(), 2)
|
||||
gtest.Assert(m.Get(2), "2")
|
||||
m.FilterEmpty()
|
||||
gtest.Assert(m.Size(), 1)
|
||||
gtest.Assert(m.Get(2), "2")
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gmap.NewIntStrMap()
|
||||
m.Set(1, "")
|
||||
m.Set(2, "2")
|
||||
t.Assert(m.Size(), 2)
|
||||
t.Assert(m.Get(2), "2")
|
||||
m.FilterEmpty()
|
||||
t.Assert(m.Size(), 1)
|
||||
t.Assert(m.Get(2), "2")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_IntStrMap_Json(t *testing.T) {
|
||||
// Marshal
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
data := g.MapIntStr{
|
||||
1: "v1",
|
||||
2: "v2",
|
||||
@ -190,111 +204,111 @@ func Test_IntStrMap_Json(t *testing.T) {
|
||||
m1 := gmap.NewIntStrMapFrom(data)
|
||||
b1, err1 := json.Marshal(m1)
|
||||
b2, err2 := json.Marshal(data)
|
||||
gtest.Assert(err1, err2)
|
||||
gtest.Assert(b1, b2)
|
||||
t.Assert(err1, err2)
|
||||
t.Assert(b1, b2)
|
||||
})
|
||||
// Unmarshal
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
data := g.MapIntStr{
|
||||
1: "v1",
|
||||
2: "v2",
|
||||
}
|
||||
b, err := json.Marshal(data)
|
||||
gtest.Assert(err, nil)
|
||||
t.Assert(err, nil)
|
||||
|
||||
m := gmap.NewIntStrMap()
|
||||
err = json.Unmarshal(b, m)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(m.Get(1), data[1])
|
||||
gtest.Assert(m.Get(2), data[2])
|
||||
t.Assert(err, nil)
|
||||
t.Assert(m.Get(1), data[1])
|
||||
t.Assert(m.Get(2), data[2])
|
||||
})
|
||||
}
|
||||
|
||||
func Test_IntStrMap_Pop(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gmap.NewIntStrMapFrom(g.MapIntStr{
|
||||
1: "v1",
|
||||
2: "v2",
|
||||
})
|
||||
gtest.Assert(m.Size(), 2)
|
||||
t.Assert(m.Size(), 2)
|
||||
|
||||
k1, v1 := m.Pop()
|
||||
gtest.AssertIN(k1, g.Slice{1, 2})
|
||||
gtest.AssertIN(v1, g.Slice{"v1", "v2"})
|
||||
gtest.Assert(m.Size(), 1)
|
||||
t.AssertIN(k1, g.Slice{1, 2})
|
||||
t.AssertIN(v1, g.Slice{"v1", "v2"})
|
||||
t.Assert(m.Size(), 1)
|
||||
k2, v2 := m.Pop()
|
||||
gtest.AssertIN(k2, g.Slice{1, 2})
|
||||
gtest.AssertIN(v2, g.Slice{"v1", "v2"})
|
||||
gtest.Assert(m.Size(), 0)
|
||||
t.AssertIN(k2, g.Slice{1, 2})
|
||||
t.AssertIN(v2, g.Slice{"v1", "v2"})
|
||||
t.Assert(m.Size(), 0)
|
||||
|
||||
gtest.AssertNE(k1, k2)
|
||||
gtest.AssertNE(v1, v2)
|
||||
t.AssertNE(k1, k2)
|
||||
t.AssertNE(v1, v2)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_IntStrMap_Pops(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gmap.NewIntStrMapFrom(g.MapIntStr{
|
||||
1: "v1",
|
||||
2: "v2",
|
||||
3: "v3",
|
||||
})
|
||||
gtest.Assert(m.Size(), 3)
|
||||
t.Assert(m.Size(), 3)
|
||||
|
||||
kArray := garray.New()
|
||||
vArray := garray.New()
|
||||
for k, v := range m.Pops(1) {
|
||||
gtest.AssertIN(k, g.Slice{1, 2, 3})
|
||||
gtest.AssertIN(v, g.Slice{"v1", "v2", "v3"})
|
||||
t.AssertIN(k, g.Slice{1, 2, 3})
|
||||
t.AssertIN(v, g.Slice{"v1", "v2", "v3"})
|
||||
kArray.Append(k)
|
||||
vArray.Append(v)
|
||||
}
|
||||
gtest.Assert(m.Size(), 2)
|
||||
t.Assert(m.Size(), 2)
|
||||
for k, v := range m.Pops(2) {
|
||||
gtest.AssertIN(k, g.Slice{1, 2, 3})
|
||||
gtest.AssertIN(v, g.Slice{"v1", "v2", "v3"})
|
||||
t.AssertIN(k, g.Slice{1, 2, 3})
|
||||
t.AssertIN(v, g.Slice{"v1", "v2", "v3"})
|
||||
kArray.Append(k)
|
||||
vArray.Append(v)
|
||||
}
|
||||
gtest.Assert(m.Size(), 0)
|
||||
t.Assert(m.Size(), 0)
|
||||
|
||||
gtest.Assert(kArray.Unique().Len(), 3)
|
||||
gtest.Assert(vArray.Unique().Len(), 3)
|
||||
t.Assert(kArray.Unique().Len(), 3)
|
||||
t.Assert(vArray.Unique().Len(), 3)
|
||||
})
|
||||
}
|
||||
|
||||
func TestIntStrMap_UnmarshalValue(t *testing.T) {
|
||||
type T struct {
|
||||
type V struct {
|
||||
Name string
|
||||
Map *gmap.IntStrMap
|
||||
}
|
||||
// JSON
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var v *V
|
||||
err := gconv.Struct(map[string]interface{}{
|
||||
"name": "john",
|
||||
"map": []byte(`{"1":"v1","2":"v2"}`),
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Map.Size(), 2)
|
||||
gtest.Assert(t.Map.Get(1), "v1")
|
||||
gtest.Assert(t.Map.Get(2), "v2")
|
||||
}, &v)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(v.Name, "john")
|
||||
t.Assert(v.Map.Size(), 2)
|
||||
t.Assert(v.Map.Get(1), "v1")
|
||||
t.Assert(v.Map.Get(2), "v2")
|
||||
})
|
||||
// Map
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var v *V
|
||||
err := gconv.Struct(map[string]interface{}{
|
||||
"name": "john",
|
||||
"map": g.MapIntAny{
|
||||
1: "v1",
|
||||
2: "v2",
|
||||
},
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Map.Size(), 2)
|
||||
gtest.Assert(t.Map.Get(1), "v1")
|
||||
gtest.Assert(t.Map.Get(2), "v2")
|
||||
}, &v)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(v.Name, "john")
|
||||
t.Assert(v.Map.Size(), 2)
|
||||
t.Assert(v.Map.Get(1), "v1")
|
||||
t.Assert(v.Map.Get(2), "v2")
|
||||
})
|
||||
}
|
||||
|
||||
@ -18,125 +18,139 @@ import (
|
||||
)
|
||||
|
||||
func Test_ListMap_Basic(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gmap.NewListMap()
|
||||
m.Set("key1", "val1")
|
||||
gtest.Assert(m.Keys(), []interface{}{"key1"})
|
||||
t.Assert(m.Keys(), []interface{}{"key1"})
|
||||
|
||||
gtest.Assert(m.Get("key1"), "val1")
|
||||
gtest.Assert(m.Size(), 1)
|
||||
gtest.Assert(m.IsEmpty(), false)
|
||||
t.Assert(m.Get("key1"), "val1")
|
||||
t.Assert(m.Size(), 1)
|
||||
t.Assert(m.IsEmpty(), false)
|
||||
|
||||
gtest.Assert(m.GetOrSet("key2", "val2"), "val2")
|
||||
gtest.Assert(m.SetIfNotExist("key2", "val2"), false)
|
||||
t.Assert(m.GetOrSet("key2", "val2"), "val2")
|
||||
t.Assert(m.SetIfNotExist("key2", "val2"), false)
|
||||
|
||||
gtest.Assert(m.SetIfNotExist("key3", "val3"), true)
|
||||
gtest.Assert(m.Remove("key2"), "val2")
|
||||
gtest.Assert(m.Contains("key2"), false)
|
||||
t.Assert(m.SetIfNotExist("key3", "val3"), true)
|
||||
t.Assert(m.Remove("key2"), "val2")
|
||||
t.Assert(m.Contains("key2"), false)
|
||||
|
||||
gtest.AssertIN("key3", m.Keys())
|
||||
gtest.AssertIN("key1", m.Keys())
|
||||
gtest.AssertIN("val3", m.Values())
|
||||
gtest.AssertIN("val1", m.Values())
|
||||
t.AssertIN("key3", m.Keys())
|
||||
t.AssertIN("key1", m.Keys())
|
||||
t.AssertIN("val3", m.Values())
|
||||
t.AssertIN("val1", m.Values())
|
||||
|
||||
m.Flip()
|
||||
|
||||
gtest.Assert(m.Map(), map[interface{}]interface{}{"val3": "key3", "val1": "key1"})
|
||||
t.Assert(m.Map(), map[interface{}]interface{}{"val3": "key3", "val1": "key1"})
|
||||
|
||||
m.Clear()
|
||||
gtest.Assert(m.Size(), 0)
|
||||
gtest.Assert(m.IsEmpty(), true)
|
||||
t.Assert(m.Size(), 0)
|
||||
t.Assert(m.IsEmpty(), true)
|
||||
|
||||
m2 := gmap.NewListMapFrom(map[interface{}]interface{}{1: 1, "key1": "val1"})
|
||||
gtest.Assert(m2.Map(), map[interface{}]interface{}{1: 1, "key1": "val1"})
|
||||
t.Assert(m2.Map(), map[interface{}]interface{}{1: 1, "key1": "val1"})
|
||||
})
|
||||
}
|
||||
func Test_ListMap_Set_Fun(t *testing.T) {
|
||||
m := gmap.NewListMap()
|
||||
m.GetOrSetFunc("fun", getValue)
|
||||
m.GetOrSetFuncLock("funlock", getValue)
|
||||
gtest.Assert(m.Get("funlock"), 3)
|
||||
gtest.Assert(m.Get("fun"), 3)
|
||||
m.GetOrSetFunc("fun", getValue)
|
||||
gtest.Assert(m.SetIfNotExistFunc("fun", getValue), false)
|
||||
gtest.Assert(m.SetIfNotExistFuncLock("funlock", getValue), false)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gmap.NewListMap()
|
||||
m.GetOrSetFunc("fun", getValue)
|
||||
m.GetOrSetFuncLock("funlock", getValue)
|
||||
t.Assert(m.Get("funlock"), 3)
|
||||
t.Assert(m.Get("fun"), 3)
|
||||
m.GetOrSetFunc("fun", getValue)
|
||||
t.Assert(m.SetIfNotExistFunc("fun", getValue), false)
|
||||
t.Assert(m.SetIfNotExistFuncLock("funlock", getValue), false)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_ListMap_Batch(t *testing.T) {
|
||||
m := gmap.NewListMap()
|
||||
m.Sets(map[interface{}]interface{}{1: 1, "key1": "val1", "key2": "val2", "key3": "val3"})
|
||||
gtest.Assert(m.Map(), map[interface{}]interface{}{1: 1, "key1": "val1", "key2": "val2", "key3": "val3"})
|
||||
m.Removes([]interface{}{"key1", 1})
|
||||
gtest.Assert(m.Map(), map[interface{}]interface{}{"key2": "val2", "key3": "val3"})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gmap.NewListMap()
|
||||
m.Sets(map[interface{}]interface{}{1: 1, "key1": "val1", "key2": "val2", "key3": "val3"})
|
||||
t.Assert(m.Map(), map[interface{}]interface{}{1: 1, "key1": "val1", "key2": "val2", "key3": "val3"})
|
||||
m.Removes([]interface{}{"key1", 1})
|
||||
t.Assert(m.Map(), map[interface{}]interface{}{"key2": "val2", "key3": "val3"})
|
||||
})
|
||||
}
|
||||
func Test_ListMap_Iterator(t *testing.T) {
|
||||
expect := map[interface{}]interface{}{1: 1, "key1": "val1"}
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
expect := map[interface{}]interface{}{1: 1, "key1": "val1"}
|
||||
|
||||
m := gmap.NewListMapFrom(expect)
|
||||
m.Iterator(func(k interface{}, v interface{}) bool {
|
||||
gtest.Assert(expect[k], v)
|
||||
return true
|
||||
m := gmap.NewListMapFrom(expect)
|
||||
m.Iterator(func(k interface{}, v interface{}) bool {
|
||||
t.Assert(expect[k], v)
|
||||
return true
|
||||
})
|
||||
// 断言返回值对遍历控制
|
||||
i := 0
|
||||
j := 0
|
||||
m.Iterator(func(k interface{}, v interface{}) bool {
|
||||
i++
|
||||
return true
|
||||
})
|
||||
m.Iterator(func(k interface{}, v interface{}) bool {
|
||||
j++
|
||||
return false
|
||||
})
|
||||
t.Assert(i, 2)
|
||||
t.Assert(j, 1)
|
||||
})
|
||||
// 断言返回值对遍历控制
|
||||
i := 0
|
||||
j := 0
|
||||
m.Iterator(func(k interface{}, v interface{}) bool {
|
||||
i++
|
||||
return true
|
||||
})
|
||||
m.Iterator(func(k interface{}, v interface{}) bool {
|
||||
j++
|
||||
return false
|
||||
})
|
||||
gtest.Assert(i, 2)
|
||||
gtest.Assert(j, 1)
|
||||
}
|
||||
|
||||
func Test_ListMap_Clone(t *testing.T) {
|
||||
//clone 方法是深克隆
|
||||
m := gmap.NewListMapFrom(map[interface{}]interface{}{1: 1, "key1": "val1"})
|
||||
m_clone := m.Clone()
|
||||
m.Remove(1)
|
||||
//修改原 map,clone 后的 map 不影响
|
||||
gtest.AssertIN(1, m_clone.Keys())
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
//clone 方法是深克隆
|
||||
m := gmap.NewListMapFrom(map[interface{}]interface{}{1: 1, "key1": "val1"})
|
||||
m_clone := m.Clone()
|
||||
m.Remove(1)
|
||||
//修改原 map,clone 后的 map 不影响
|
||||
t.AssertIN(1, m_clone.Keys())
|
||||
|
||||
m_clone.Remove("key1")
|
||||
//修改clone map,原 map 不影响
|
||||
gtest.AssertIN("key1", m.Keys())
|
||||
m_clone.Remove("key1")
|
||||
//修改clone map,原 map 不影响
|
||||
t.AssertIN("key1", m.Keys())
|
||||
})
|
||||
}
|
||||
|
||||
func Test_ListMap_Basic_Merge(t *testing.T) {
|
||||
m1 := gmap.NewListMap()
|
||||
m2 := gmap.NewListMap()
|
||||
m1.Set("key1", "val1")
|
||||
m2.Set("key2", "val2")
|
||||
m1.Merge(m2)
|
||||
gtest.Assert(m1.Map(), map[interface{}]interface{}{"key1": "val1", "key2": "val2"})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m1 := gmap.NewListMap()
|
||||
m2 := gmap.NewListMap()
|
||||
m1.Set("key1", "val1")
|
||||
m2.Set("key2", "val2")
|
||||
m1.Merge(m2)
|
||||
t.Assert(m1.Map(), map[interface{}]interface{}{"key1": "val1", "key2": "val2"})
|
||||
})
|
||||
}
|
||||
|
||||
func Test_ListMap_Order(t *testing.T) {
|
||||
m := gmap.NewListMap()
|
||||
m.Set("k1", "v1")
|
||||
m.Set("k2", "v2")
|
||||
m.Set("k3", "v3")
|
||||
gtest.Assert(m.Keys(), g.Slice{"k1", "k2", "k3"})
|
||||
gtest.Assert(m.Values(), g.Slice{"v1", "v2", "v3"})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gmap.NewListMap()
|
||||
m.Set("k1", "v1")
|
||||
m.Set("k2", "v2")
|
||||
m.Set("k3", "v3")
|
||||
t.Assert(m.Keys(), g.Slice{"k1", "k2", "k3"})
|
||||
t.Assert(m.Values(), g.Slice{"v1", "v2", "v3"})
|
||||
})
|
||||
}
|
||||
|
||||
func Test_ListMap_FilterEmpty(t *testing.T) {
|
||||
m := gmap.NewListMap()
|
||||
m.Set(1, "")
|
||||
m.Set(2, "2")
|
||||
gtest.Assert(m.Size(), 2)
|
||||
gtest.Assert(m.Get(2), "2")
|
||||
m.FilterEmpty()
|
||||
gtest.Assert(m.Size(), 1)
|
||||
gtest.Assert(m.Get(2), "2")
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gmap.NewListMap()
|
||||
m.Set(1, "")
|
||||
m.Set(2, "2")
|
||||
t.Assert(m.Size(), 2)
|
||||
t.Assert(m.Get(2), "2")
|
||||
m.FilterEmpty()
|
||||
t.Assert(m.Size(), 1)
|
||||
t.Assert(m.Get(2), "2")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_ListMap_Json(t *testing.T) {
|
||||
// Marshal
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
data := g.MapAnyAny{
|
||||
"k1": "v1",
|
||||
"k2": "v2",
|
||||
@ -144,126 +158,126 @@ func Test_ListMap_Json(t *testing.T) {
|
||||
m1 := gmap.NewListMapFrom(data)
|
||||
b1, err1 := json.Marshal(m1)
|
||||
b2, err2 := json.Marshal(gconv.Map(data))
|
||||
gtest.Assert(err1, err2)
|
||||
gtest.Assert(b1, b2)
|
||||
t.Assert(err1, err2)
|
||||
t.Assert(b1, b2)
|
||||
})
|
||||
// Unmarshal
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
data := g.MapAnyAny{
|
||||
"k1": "v1",
|
||||
"k2": "v2",
|
||||
}
|
||||
b, err := json.Marshal(gconv.Map(data))
|
||||
gtest.Assert(err, nil)
|
||||
t.Assert(err, nil)
|
||||
|
||||
m := gmap.NewListMap()
|
||||
err = json.Unmarshal(b, m)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(m.Get("k1"), data["k1"])
|
||||
gtest.Assert(m.Get("k2"), data["k2"])
|
||||
t.Assert(err, nil)
|
||||
t.Assert(m.Get("k1"), data["k1"])
|
||||
t.Assert(m.Get("k2"), data["k2"])
|
||||
})
|
||||
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
data := g.MapAnyAny{
|
||||
"k1": "v1",
|
||||
"k2": "v2",
|
||||
}
|
||||
b, err := json.Marshal(gconv.Map(data))
|
||||
gtest.Assert(err, nil)
|
||||
t.Assert(err, nil)
|
||||
|
||||
var m gmap.ListMap
|
||||
err = json.Unmarshal(b, &m)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(m.Get("k1"), data["k1"])
|
||||
gtest.Assert(m.Get("k2"), data["k2"])
|
||||
t.Assert(err, nil)
|
||||
t.Assert(m.Get("k1"), data["k1"])
|
||||
t.Assert(m.Get("k2"), data["k2"])
|
||||
})
|
||||
}
|
||||
|
||||
func Test_ListMap_Pop(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gmap.NewListMapFrom(g.MapAnyAny{
|
||||
"k1": "v1",
|
||||
"k2": "v2",
|
||||
})
|
||||
gtest.Assert(m.Size(), 2)
|
||||
t.Assert(m.Size(), 2)
|
||||
|
||||
k1, v1 := m.Pop()
|
||||
gtest.AssertIN(k1, g.Slice{"k1", "k2"})
|
||||
gtest.AssertIN(v1, g.Slice{"v1", "v2"})
|
||||
gtest.Assert(m.Size(), 1)
|
||||
t.AssertIN(k1, g.Slice{"k1", "k2"})
|
||||
t.AssertIN(v1, g.Slice{"v1", "v2"})
|
||||
t.Assert(m.Size(), 1)
|
||||
k2, v2 := m.Pop()
|
||||
gtest.AssertIN(k2, g.Slice{"k1", "k2"})
|
||||
gtest.AssertIN(v2, g.Slice{"v1", "v2"})
|
||||
gtest.Assert(m.Size(), 0)
|
||||
t.AssertIN(k2, g.Slice{"k1", "k2"})
|
||||
t.AssertIN(v2, g.Slice{"v1", "v2"})
|
||||
t.Assert(m.Size(), 0)
|
||||
|
||||
gtest.AssertNE(k1, k2)
|
||||
gtest.AssertNE(v1, v2)
|
||||
t.AssertNE(k1, k2)
|
||||
t.AssertNE(v1, v2)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_ListMap_Pops(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gmap.NewListMapFrom(g.MapAnyAny{
|
||||
"k1": "v1",
|
||||
"k2": "v2",
|
||||
"k3": "v3",
|
||||
})
|
||||
gtest.Assert(m.Size(), 3)
|
||||
t.Assert(m.Size(), 3)
|
||||
|
||||
kArray := garray.New()
|
||||
vArray := garray.New()
|
||||
for k, v := range m.Pops(1) {
|
||||
gtest.AssertIN(k, g.Slice{"k1", "k2", "k3"})
|
||||
gtest.AssertIN(v, g.Slice{"v1", "v2", "v3"})
|
||||
t.AssertIN(k, g.Slice{"k1", "k2", "k3"})
|
||||
t.AssertIN(v, g.Slice{"v1", "v2", "v3"})
|
||||
kArray.Append(k)
|
||||
vArray.Append(v)
|
||||
}
|
||||
gtest.Assert(m.Size(), 2)
|
||||
t.Assert(m.Size(), 2)
|
||||
for k, v := range m.Pops(2) {
|
||||
gtest.AssertIN(k, g.Slice{"k1", "k2", "k3"})
|
||||
gtest.AssertIN(v, g.Slice{"v1", "v2", "v3"})
|
||||
t.AssertIN(k, g.Slice{"k1", "k2", "k3"})
|
||||
t.AssertIN(v, g.Slice{"v1", "v2", "v3"})
|
||||
kArray.Append(k)
|
||||
vArray.Append(v)
|
||||
}
|
||||
gtest.Assert(m.Size(), 0)
|
||||
t.Assert(m.Size(), 0)
|
||||
|
||||
gtest.Assert(kArray.Unique().Len(), 3)
|
||||
gtest.Assert(vArray.Unique().Len(), 3)
|
||||
t.Assert(kArray.Unique().Len(), 3)
|
||||
t.Assert(vArray.Unique().Len(), 3)
|
||||
})
|
||||
}
|
||||
|
||||
func TestListMap_UnmarshalValue(t *testing.T) {
|
||||
type T struct {
|
||||
type V struct {
|
||||
Name string
|
||||
Map *gmap.ListMap
|
||||
}
|
||||
// JSON
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var v *V
|
||||
err := gconv.Struct(map[string]interface{}{
|
||||
"name": "john",
|
||||
"map": []byte(`{"1":"v1","2":"v2"}`),
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Map.Size(), 2)
|
||||
gtest.Assert(t.Map.Get("1"), "v1")
|
||||
gtest.Assert(t.Map.Get("2"), "v2")
|
||||
}, &v)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(v.Name, "john")
|
||||
t.Assert(v.Map.Size(), 2)
|
||||
t.Assert(v.Map.Get("1"), "v1")
|
||||
t.Assert(v.Map.Get("2"), "v2")
|
||||
})
|
||||
// Map
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var v *V
|
||||
err := gconv.Struct(map[string]interface{}{
|
||||
"name": "john",
|
||||
"map": g.MapIntAny{
|
||||
1: "v1",
|
||||
2: "v2",
|
||||
},
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Map.Size(), 2)
|
||||
gtest.Assert(t.Map.Get("1"), "v1")
|
||||
gtest.Assert(t.Map.Get("2"), "v2")
|
||||
}, &v)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(v.Name, "john")
|
||||
t.Assert(v.Map.Size(), 2)
|
||||
t.Assert(v.Map.Get("1"), "v1")
|
||||
t.Assert(v.Map.Get("2"), "v2")
|
||||
})
|
||||
}
|
||||
|
||||
@ -21,162 +21,179 @@ func stringAnyCallBack(string, interface{}) bool {
|
||||
return true
|
||||
}
|
||||
func Test_StrAnyMap_Basic(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gmap.NewStrAnyMap()
|
||||
m.Set("a", 1)
|
||||
|
||||
gtest.Assert(m.Get("a"), 1)
|
||||
gtest.Assert(m.Size(), 1)
|
||||
gtest.Assert(m.IsEmpty(), false)
|
||||
t.Assert(m.Get("a"), 1)
|
||||
t.Assert(m.Size(), 1)
|
||||
t.Assert(m.IsEmpty(), false)
|
||||
|
||||
gtest.Assert(m.GetOrSet("b", "2"), "2")
|
||||
gtest.Assert(m.SetIfNotExist("b", "2"), false)
|
||||
t.Assert(m.GetOrSet("b", "2"), "2")
|
||||
t.Assert(m.SetIfNotExist("b", "2"), false)
|
||||
|
||||
gtest.Assert(m.SetIfNotExist("c", 3), true)
|
||||
t.Assert(m.SetIfNotExist("c", 3), true)
|
||||
|
||||
gtest.Assert(m.Remove("b"), "2")
|
||||
gtest.Assert(m.Contains("b"), false)
|
||||
t.Assert(m.Remove("b"), "2")
|
||||
t.Assert(m.Contains("b"), false)
|
||||
|
||||
gtest.AssertIN("c", m.Keys())
|
||||
gtest.AssertIN("a", m.Keys())
|
||||
gtest.AssertIN(3, m.Values())
|
||||
gtest.AssertIN(1, m.Values())
|
||||
t.AssertIN("c", m.Keys())
|
||||
t.AssertIN("a", m.Keys())
|
||||
t.AssertIN(3, m.Values())
|
||||
t.AssertIN(1, m.Values())
|
||||
|
||||
m.Flip()
|
||||
gtest.Assert(m.Map(), map[string]interface{}{"1": "a", "3": "c"})
|
||||
t.Assert(m.Map(), map[string]interface{}{"1": "a", "3": "c"})
|
||||
|
||||
m.Clear()
|
||||
gtest.Assert(m.Size(), 0)
|
||||
gtest.Assert(m.IsEmpty(), true)
|
||||
t.Assert(m.Size(), 0)
|
||||
t.Assert(m.IsEmpty(), true)
|
||||
|
||||
m2 := gmap.NewStrAnyMapFrom(map[string]interface{}{"a": 1, "b": "2"})
|
||||
gtest.Assert(m2.Map(), map[string]interface{}{"a": 1, "b": "2"})
|
||||
t.Assert(m2.Map(), map[string]interface{}{"a": 1, "b": "2"})
|
||||
})
|
||||
}
|
||||
func Test_StrAnyMap_Set_Fun(t *testing.T) {
|
||||
m := gmap.NewStrAnyMap()
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gmap.NewStrAnyMap()
|
||||
|
||||
m.GetOrSetFunc("a", getAny)
|
||||
m.GetOrSetFuncLock("b", getAny)
|
||||
gtest.Assert(m.Get("a"), 123)
|
||||
gtest.Assert(m.Get("b"), 123)
|
||||
gtest.Assert(m.SetIfNotExistFunc("a", getAny), false)
|
||||
gtest.Assert(m.SetIfNotExistFunc("c", getAny), true)
|
||||
|
||||
gtest.Assert(m.SetIfNotExistFuncLock("b", getAny), false)
|
||||
gtest.Assert(m.SetIfNotExistFuncLock("d", getAny), true)
|
||||
m.GetOrSetFunc("a", getAny)
|
||||
m.GetOrSetFuncLock("b", getAny)
|
||||
t.Assert(m.Get("a"), 123)
|
||||
t.Assert(m.Get("b"), 123)
|
||||
t.Assert(m.SetIfNotExistFunc("a", getAny), false)
|
||||
t.Assert(m.SetIfNotExistFunc("c", getAny), true)
|
||||
|
||||
t.Assert(m.SetIfNotExistFuncLock("b", getAny), false)
|
||||
t.Assert(m.SetIfNotExistFuncLock("d", getAny), true)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_StrAnyMap_Batch(t *testing.T) {
|
||||
m := gmap.NewStrAnyMap()
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gmap.NewStrAnyMap()
|
||||
|
||||
m.Sets(map[string]interface{}{"a": 1, "b": "2", "c": 3})
|
||||
gtest.Assert(m.Map(), map[string]interface{}{"a": 1, "b": "2", "c": 3})
|
||||
m.Removes([]string{"a", "b"})
|
||||
gtest.Assert(m.Map(), map[string]interface{}{"c": 3})
|
||||
m.Sets(map[string]interface{}{"a": 1, "b": "2", "c": 3})
|
||||
t.Assert(m.Map(), map[string]interface{}{"a": 1, "b": "2", "c": 3})
|
||||
m.Removes([]string{"a", "b"})
|
||||
t.Assert(m.Map(), map[string]interface{}{"c": 3})
|
||||
})
|
||||
}
|
||||
|
||||
func Test_StrAnyMap_Iterator(t *testing.T) {
|
||||
expect := map[string]interface{}{"a": true, "b": false}
|
||||
m := gmap.NewStrAnyMapFrom(expect)
|
||||
m.Iterator(func(k string, v interface{}) bool {
|
||||
gtest.Assert(expect[k], v)
|
||||
return true
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
expect := map[string]interface{}{"a": true, "b": false}
|
||||
m := gmap.NewStrAnyMapFrom(expect)
|
||||
m.Iterator(func(k string, v interface{}) bool {
|
||||
t.Assert(expect[k], v)
|
||||
return true
|
||||
})
|
||||
// 断言返回值对遍历控制
|
||||
i := 0
|
||||
j := 0
|
||||
m.Iterator(func(k string, v interface{}) bool {
|
||||
i++
|
||||
return true
|
||||
})
|
||||
m.Iterator(func(k string, v interface{}) bool {
|
||||
j++
|
||||
return false
|
||||
})
|
||||
t.Assert(i, 2)
|
||||
t.Assert(j, 1)
|
||||
})
|
||||
// 断言返回值对遍历控制
|
||||
i := 0
|
||||
j := 0
|
||||
m.Iterator(func(k string, v interface{}) bool {
|
||||
i++
|
||||
return true
|
||||
})
|
||||
m.Iterator(func(k string, v interface{}) bool {
|
||||
j++
|
||||
return false
|
||||
})
|
||||
gtest.Assert(i, 2)
|
||||
gtest.Assert(j, 1)
|
||||
}
|
||||
|
||||
func Test_StrAnyMap_Lock(t *testing.T) {
|
||||
expect := map[string]interface{}{"a": true, "b": false}
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
expect := map[string]interface{}{"a": true, "b": false}
|
||||
|
||||
m := gmap.NewStrAnyMapFrom(expect)
|
||||
m.LockFunc(func(m map[string]interface{}) {
|
||||
gtest.Assert(m, expect)
|
||||
})
|
||||
m.RLockFunc(func(m map[string]interface{}) {
|
||||
gtest.Assert(m, expect)
|
||||
m := gmap.NewStrAnyMapFrom(expect)
|
||||
m.LockFunc(func(m map[string]interface{}) {
|
||||
t.Assert(m, expect)
|
||||
})
|
||||
m.RLockFunc(func(m map[string]interface{}) {
|
||||
t.Assert(m, expect)
|
||||
})
|
||||
})
|
||||
}
|
||||
func Test_StrAnyMap_Clone(t *testing.T) {
|
||||
//clone 方法是深克隆
|
||||
m := gmap.NewStrAnyMapFrom(map[string]interface{}{"a": 1, "b": "2"})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
//clone 方法是深克隆
|
||||
m := gmap.NewStrAnyMapFrom(map[string]interface{}{"a": 1, "b": "2"})
|
||||
|
||||
m_clone := m.Clone()
|
||||
m.Remove("a")
|
||||
//修改原 map,clone 后的 map 不影响
|
||||
gtest.AssertIN("a", m_clone.Keys())
|
||||
m_clone := m.Clone()
|
||||
m.Remove("a")
|
||||
//修改原 map,clone 后的 map 不影响
|
||||
t.AssertIN("a", m_clone.Keys())
|
||||
|
||||
m_clone.Remove("b")
|
||||
//修改clone map,原 map 不影响
|
||||
gtest.AssertIN("b", m.Keys())
|
||||
m_clone.Remove("b")
|
||||
//修改clone map,原 map 不影响
|
||||
t.AssertIN("b", m.Keys())
|
||||
})
|
||||
}
|
||||
func Test_StrAnyMap_Merge(t *testing.T) {
|
||||
m1 := gmap.NewStrAnyMap()
|
||||
m2 := gmap.NewStrAnyMap()
|
||||
m1.Set("a", 1)
|
||||
m2.Set("b", "2")
|
||||
m1.Merge(m2)
|
||||
gtest.Assert(m1.Map(), map[string]interface{}{"a": 1, "b": "2"})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m1 := gmap.NewStrAnyMap()
|
||||
m2 := gmap.NewStrAnyMap()
|
||||
m1.Set("a", 1)
|
||||
m2.Set("b", "2")
|
||||
m1.Merge(m2)
|
||||
t.Assert(m1.Map(), map[string]interface{}{"a": 1, "b": "2"})
|
||||
})
|
||||
}
|
||||
|
||||
func Test_StrAnyMap_Map(t *testing.T) {
|
||||
m := gmap.NewStrAnyMap()
|
||||
m.Set("1", 1)
|
||||
m.Set("2", 2)
|
||||
gtest.Assert(m.Get("1"), 1)
|
||||
gtest.Assert(m.Get("2"), 2)
|
||||
data := m.Map()
|
||||
gtest.Assert(data["1"], 1)
|
||||
gtest.Assert(data["2"], 2)
|
||||
data["3"] = 3
|
||||
gtest.Assert(m.Get("3"), 3)
|
||||
m.Set("4", 4)
|
||||
gtest.Assert(data["4"], 4)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gmap.NewStrAnyMap()
|
||||
m.Set("1", 1)
|
||||
m.Set("2", 2)
|
||||
t.Assert(m.Get("1"), 1)
|
||||
t.Assert(m.Get("2"), 2)
|
||||
data := m.Map()
|
||||
t.Assert(data["1"], 1)
|
||||
t.Assert(data["2"], 2)
|
||||
data["3"] = 3
|
||||
t.Assert(m.Get("3"), 3)
|
||||
m.Set("4", 4)
|
||||
t.Assert(data["4"], 4)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_StrAnyMap_MapCopy(t *testing.T) {
|
||||
m := gmap.NewStrAnyMap()
|
||||
m.Set("1", 1)
|
||||
m.Set("2", 2)
|
||||
gtest.Assert(m.Get("1"), 1)
|
||||
gtest.Assert(m.Get("2"), 2)
|
||||
data := m.MapCopy()
|
||||
gtest.Assert(data["1"], 1)
|
||||
gtest.Assert(data["2"], 2)
|
||||
data["3"] = 3
|
||||
gtest.Assert(m.Get("3"), nil)
|
||||
m.Set("4", 4)
|
||||
gtest.Assert(data["4"], nil)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gmap.NewStrAnyMap()
|
||||
m.Set("1", 1)
|
||||
m.Set("2", 2)
|
||||
t.Assert(m.Get("1"), 1)
|
||||
t.Assert(m.Get("2"), 2)
|
||||
data := m.MapCopy()
|
||||
t.Assert(data["1"], 1)
|
||||
t.Assert(data["2"], 2)
|
||||
data["3"] = 3
|
||||
t.Assert(m.Get("3"), nil)
|
||||
m.Set("4", 4)
|
||||
t.Assert(data["4"], nil)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_StrAnyMap_FilterEmpty(t *testing.T) {
|
||||
m := gmap.NewStrAnyMap()
|
||||
m.Set("1", 0)
|
||||
m.Set("2", 2)
|
||||
gtest.Assert(m.Size(), 2)
|
||||
gtest.Assert(m.Get("1"), 0)
|
||||
gtest.Assert(m.Get("2"), 2)
|
||||
m.FilterEmpty()
|
||||
gtest.Assert(m.Size(), 1)
|
||||
gtest.Assert(m.Get("2"), 2)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gmap.NewStrAnyMap()
|
||||
m.Set("1", 0)
|
||||
m.Set("2", 2)
|
||||
t.Assert(m.Size(), 2)
|
||||
t.Assert(m.Get("1"), 0)
|
||||
t.Assert(m.Get("2"), 2)
|
||||
m.FilterEmpty()
|
||||
t.Assert(m.Size(), 1)
|
||||
t.Assert(m.Get("2"), 2)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_StrAnyMap_Json(t *testing.T) {
|
||||
// Marshal
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
data := g.MapStrAny{
|
||||
"k1": "v1",
|
||||
"k2": "v2",
|
||||
@ -184,125 +201,125 @@ func Test_StrAnyMap_Json(t *testing.T) {
|
||||
m1 := gmap.NewStrAnyMapFrom(data)
|
||||
b1, err1 := json.Marshal(m1)
|
||||
b2, err2 := json.Marshal(data)
|
||||
gtest.Assert(err1, err2)
|
||||
gtest.Assert(b1, b2)
|
||||
t.Assert(err1, err2)
|
||||
t.Assert(b1, b2)
|
||||
})
|
||||
// Unmarshal
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
data := g.MapStrAny{
|
||||
"k1": "v1",
|
||||
"k2": "v2",
|
||||
}
|
||||
b, err := json.Marshal(data)
|
||||
gtest.Assert(err, nil)
|
||||
t.Assert(err, nil)
|
||||
|
||||
m := gmap.NewStrAnyMap()
|
||||
err = json.Unmarshal(b, m)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(m.Get("k1"), data["k1"])
|
||||
gtest.Assert(m.Get("k2"), data["k2"])
|
||||
t.Assert(err, nil)
|
||||
t.Assert(m.Get("k1"), data["k1"])
|
||||
t.Assert(m.Get("k2"), data["k2"])
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
data := g.MapStrAny{
|
||||
"k1": "v1",
|
||||
"k2": "v2",
|
||||
}
|
||||
b, err := json.Marshal(data)
|
||||
gtest.Assert(err, nil)
|
||||
t.Assert(err, nil)
|
||||
|
||||
var m gmap.StrAnyMap
|
||||
err = json.Unmarshal(b, &m)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(m.Get("k1"), data["k1"])
|
||||
gtest.Assert(m.Get("k2"), data["k2"])
|
||||
t.Assert(err, nil)
|
||||
t.Assert(m.Get("k1"), data["k1"])
|
||||
t.Assert(m.Get("k2"), data["k2"])
|
||||
})
|
||||
}
|
||||
|
||||
func Test_StrAnyMap_Pop(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gmap.NewStrAnyMapFrom(g.MapStrAny{
|
||||
"k1": "v1",
|
||||
"k2": "v2",
|
||||
})
|
||||
gtest.Assert(m.Size(), 2)
|
||||
t.Assert(m.Size(), 2)
|
||||
|
||||
k1, v1 := m.Pop()
|
||||
gtest.AssertIN(k1, g.Slice{"k1", "k2"})
|
||||
gtest.AssertIN(v1, g.Slice{"v1", "v2"})
|
||||
gtest.Assert(m.Size(), 1)
|
||||
t.AssertIN(k1, g.Slice{"k1", "k2"})
|
||||
t.AssertIN(v1, g.Slice{"v1", "v2"})
|
||||
t.Assert(m.Size(), 1)
|
||||
k2, v2 := m.Pop()
|
||||
gtest.AssertIN(k2, g.Slice{"k1", "k2"})
|
||||
gtest.AssertIN(v2, g.Slice{"v1", "v2"})
|
||||
gtest.Assert(m.Size(), 0)
|
||||
t.AssertIN(k2, g.Slice{"k1", "k2"})
|
||||
t.AssertIN(v2, g.Slice{"v1", "v2"})
|
||||
t.Assert(m.Size(), 0)
|
||||
|
||||
gtest.AssertNE(k1, k2)
|
||||
gtest.AssertNE(v1, v2)
|
||||
t.AssertNE(k1, k2)
|
||||
t.AssertNE(v1, v2)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_StrAnyMap_Pops(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gmap.NewStrAnyMapFrom(g.MapStrAny{
|
||||
"k1": "v1",
|
||||
"k2": "v2",
|
||||
"k3": "v3",
|
||||
})
|
||||
gtest.Assert(m.Size(), 3)
|
||||
t.Assert(m.Size(), 3)
|
||||
|
||||
kArray := garray.New()
|
||||
vArray := garray.New()
|
||||
for k, v := range m.Pops(1) {
|
||||
gtest.AssertIN(k, g.Slice{"k1", "k2", "k3"})
|
||||
gtest.AssertIN(v, g.Slice{"v1", "v2", "v3"})
|
||||
t.AssertIN(k, g.Slice{"k1", "k2", "k3"})
|
||||
t.AssertIN(v, g.Slice{"v1", "v2", "v3"})
|
||||
kArray.Append(k)
|
||||
vArray.Append(v)
|
||||
}
|
||||
gtest.Assert(m.Size(), 2)
|
||||
t.Assert(m.Size(), 2)
|
||||
for k, v := range m.Pops(2) {
|
||||
gtest.AssertIN(k, g.Slice{"k1", "k2", "k3"})
|
||||
gtest.AssertIN(v, g.Slice{"v1", "v2", "v3"})
|
||||
t.AssertIN(k, g.Slice{"k1", "k2", "k3"})
|
||||
t.AssertIN(v, g.Slice{"v1", "v2", "v3"})
|
||||
kArray.Append(k)
|
||||
vArray.Append(v)
|
||||
}
|
||||
gtest.Assert(m.Size(), 0)
|
||||
t.Assert(m.Size(), 0)
|
||||
|
||||
gtest.Assert(kArray.Unique().Len(), 3)
|
||||
gtest.Assert(vArray.Unique().Len(), 3)
|
||||
t.Assert(kArray.Unique().Len(), 3)
|
||||
t.Assert(vArray.Unique().Len(), 3)
|
||||
})
|
||||
}
|
||||
|
||||
func TestStrAnyMap_UnmarshalValue(t *testing.T) {
|
||||
type T struct {
|
||||
type V struct {
|
||||
Name string
|
||||
Map *gmap.StrAnyMap
|
||||
}
|
||||
// JSON
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var v *V
|
||||
err := gconv.Struct(map[string]interface{}{
|
||||
"name": "john",
|
||||
"map": []byte(`{"k1":"v1","k2":"v2"}`),
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Map.Size(), 2)
|
||||
gtest.Assert(t.Map.Get("k1"), "v1")
|
||||
gtest.Assert(t.Map.Get("k2"), "v2")
|
||||
}, &v)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(v.Name, "john")
|
||||
t.Assert(v.Map.Size(), 2)
|
||||
t.Assert(v.Map.Get("k1"), "v1")
|
||||
t.Assert(v.Map.Get("k2"), "v2")
|
||||
})
|
||||
// Map
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var v *V
|
||||
err := gconv.Struct(map[string]interface{}{
|
||||
"name": "john",
|
||||
"map": g.Map{
|
||||
"k1": "v1",
|
||||
"k2": "v2",
|
||||
},
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Map.Size(), 2)
|
||||
gtest.Assert(t.Map.Get("k1"), "v1")
|
||||
gtest.Assert(t.Map.Get("k2"), "v2")
|
||||
}, &v)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(v.Name, "john")
|
||||
t.Assert(v.Map.Size(), 2)
|
||||
t.Assert(v.Map.Get("k1"), "v1")
|
||||
t.Assert(v.Map.Get("k2"), "v2")
|
||||
})
|
||||
}
|
||||
|
||||
@ -21,165 +21,181 @@ func stringIntCallBack(string, int) bool {
|
||||
return true
|
||||
}
|
||||
func Test_StrIntMap_Basic(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gmap.NewStrIntMap()
|
||||
m.Set("a", 1)
|
||||
|
||||
gtest.Assert(m.Get("a"), 1)
|
||||
gtest.Assert(m.Size(), 1)
|
||||
gtest.Assert(m.IsEmpty(), false)
|
||||
t.Assert(m.Get("a"), 1)
|
||||
t.Assert(m.Size(), 1)
|
||||
t.Assert(m.IsEmpty(), false)
|
||||
|
||||
gtest.Assert(m.GetOrSet("b", 2), 2)
|
||||
gtest.Assert(m.SetIfNotExist("b", 2), false)
|
||||
t.Assert(m.GetOrSet("b", 2), 2)
|
||||
t.Assert(m.SetIfNotExist("b", 2), false)
|
||||
|
||||
gtest.Assert(m.SetIfNotExist("c", 3), true)
|
||||
t.Assert(m.SetIfNotExist("c", 3), true)
|
||||
|
||||
gtest.Assert(m.Remove("b"), 2)
|
||||
gtest.Assert(m.Contains("b"), false)
|
||||
t.Assert(m.Remove("b"), 2)
|
||||
t.Assert(m.Contains("b"), false)
|
||||
|
||||
gtest.AssertIN("c", m.Keys())
|
||||
gtest.AssertIN("a", m.Keys())
|
||||
gtest.AssertIN(3, m.Values())
|
||||
gtest.AssertIN(1, m.Values())
|
||||
t.AssertIN("c", m.Keys())
|
||||
t.AssertIN("a", m.Keys())
|
||||
t.AssertIN(3, m.Values())
|
||||
t.AssertIN(1, m.Values())
|
||||
|
||||
m_f := gmap.NewStrIntMap()
|
||||
m_f.Set("1", 2)
|
||||
m_f.Flip()
|
||||
gtest.Assert(m_f.Map(), map[string]int{"2": 1})
|
||||
t.Assert(m_f.Map(), map[string]int{"2": 1})
|
||||
|
||||
m.Clear()
|
||||
gtest.Assert(m.Size(), 0)
|
||||
gtest.Assert(m.IsEmpty(), true)
|
||||
t.Assert(m.Size(), 0)
|
||||
t.Assert(m.IsEmpty(), true)
|
||||
|
||||
m2 := gmap.NewStrIntMapFrom(map[string]int{"a": 1, "b": 2})
|
||||
gtest.Assert(m2.Map(), map[string]int{"a": 1, "b": 2})
|
||||
t.Assert(m2.Map(), map[string]int{"a": 1, "b": 2})
|
||||
})
|
||||
}
|
||||
func Test_StrIntMap_Set_Fun(t *testing.T) {
|
||||
m := gmap.NewStrIntMap()
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gmap.NewStrIntMap()
|
||||
|
||||
m.GetOrSetFunc("a", getInt)
|
||||
m.GetOrSetFuncLock("b", getInt)
|
||||
gtest.Assert(m.Get("a"), 123)
|
||||
gtest.Assert(m.Get("b"), 123)
|
||||
gtest.Assert(m.SetIfNotExistFunc("a", getInt), false)
|
||||
gtest.Assert(m.SetIfNotExistFunc("c", getInt), true)
|
||||
|
||||
gtest.Assert(m.SetIfNotExistFuncLock("b", getInt), false)
|
||||
gtest.Assert(m.SetIfNotExistFuncLock("d", getInt), true)
|
||||
m.GetOrSetFunc("a", getInt)
|
||||
m.GetOrSetFuncLock("b", getInt)
|
||||
t.Assert(m.Get("a"), 123)
|
||||
t.Assert(m.Get("b"), 123)
|
||||
t.Assert(m.SetIfNotExistFunc("a", getInt), false)
|
||||
t.Assert(m.SetIfNotExistFunc("c", getInt), true)
|
||||
|
||||
t.Assert(m.SetIfNotExistFuncLock("b", getInt), false)
|
||||
t.Assert(m.SetIfNotExistFuncLock("d", getInt), true)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_StrIntMap_Batch(t *testing.T) {
|
||||
m := gmap.NewStrIntMap()
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gmap.NewStrIntMap()
|
||||
|
||||
m.Sets(map[string]int{"a": 1, "b": 2, "c": 3})
|
||||
gtest.Assert(m.Map(), map[string]int{"a": 1, "b": 2, "c": 3})
|
||||
m.Removes([]string{"a", "b"})
|
||||
gtest.Assert(m.Map(), map[string]int{"c": 3})
|
||||
m.Sets(map[string]int{"a": 1, "b": 2, "c": 3})
|
||||
t.Assert(m.Map(), map[string]int{"a": 1, "b": 2, "c": 3})
|
||||
m.Removes([]string{"a", "b"})
|
||||
t.Assert(m.Map(), map[string]int{"c": 3})
|
||||
})
|
||||
}
|
||||
func Test_StrIntMap_Iterator(t *testing.T) {
|
||||
expect := map[string]int{"a": 1, "b": 2}
|
||||
m := gmap.NewStrIntMapFrom(expect)
|
||||
m.Iterator(func(k string, v int) bool {
|
||||
gtest.Assert(expect[k], v)
|
||||
return true
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
expect := map[string]int{"a": 1, "b": 2}
|
||||
m := gmap.NewStrIntMapFrom(expect)
|
||||
m.Iterator(func(k string, v int) bool {
|
||||
t.Assert(expect[k], v)
|
||||
return true
|
||||
})
|
||||
// 断言返回值对遍历控制
|
||||
i := 0
|
||||
j := 0
|
||||
m.Iterator(func(k string, v int) bool {
|
||||
i++
|
||||
return true
|
||||
})
|
||||
m.Iterator(func(k string, v int) bool {
|
||||
j++
|
||||
return false
|
||||
})
|
||||
t.Assert(i, 2)
|
||||
t.Assert(j, 1)
|
||||
})
|
||||
// 断言返回值对遍历控制
|
||||
i := 0
|
||||
j := 0
|
||||
m.Iterator(func(k string, v int) bool {
|
||||
i++
|
||||
return true
|
||||
})
|
||||
m.Iterator(func(k string, v int) bool {
|
||||
j++
|
||||
return false
|
||||
})
|
||||
gtest.Assert(i, 2)
|
||||
gtest.Assert(j, 1)
|
||||
|
||||
}
|
||||
|
||||
func Test_StrIntMap_Lock(t *testing.T) {
|
||||
expect := map[string]int{"a": 1, "b": 2}
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
expect := map[string]int{"a": 1, "b": 2}
|
||||
|
||||
m := gmap.NewStrIntMapFrom(expect)
|
||||
m.LockFunc(func(m map[string]int) {
|
||||
gtest.Assert(m, expect)
|
||||
})
|
||||
m.RLockFunc(func(m map[string]int) {
|
||||
gtest.Assert(m, expect)
|
||||
m := gmap.NewStrIntMapFrom(expect)
|
||||
m.LockFunc(func(m map[string]int) {
|
||||
t.Assert(m, expect)
|
||||
})
|
||||
m.RLockFunc(func(m map[string]int) {
|
||||
t.Assert(m, expect)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func Test_StrIntMap_Clone(t *testing.T) {
|
||||
//clone 方法是深克隆
|
||||
m := gmap.NewStrIntMapFrom(map[string]int{"a": 1, "b": 2, "c": 3})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
//clone 方法是深克隆
|
||||
m := gmap.NewStrIntMapFrom(map[string]int{"a": 1, "b": 2, "c": 3})
|
||||
|
||||
m_clone := m.Clone()
|
||||
m.Remove("a")
|
||||
//修改原 map,clone 后的 map 不影响
|
||||
gtest.AssertIN("a", m_clone.Keys())
|
||||
m_clone := m.Clone()
|
||||
m.Remove("a")
|
||||
//修改原 map,clone 后的 map 不影响
|
||||
t.AssertIN("a", m_clone.Keys())
|
||||
|
||||
m_clone.Remove("b")
|
||||
//修改clone map,原 map 不影响
|
||||
gtest.AssertIN("b", m.Keys())
|
||||
m_clone.Remove("b")
|
||||
//修改clone map,原 map 不影响
|
||||
t.AssertIN("b", m.Keys())
|
||||
})
|
||||
}
|
||||
func Test_StrIntMap_Merge(t *testing.T) {
|
||||
m1 := gmap.NewStrIntMap()
|
||||
m2 := gmap.NewStrIntMap()
|
||||
m1.Set("a", 1)
|
||||
m2.Set("b", 2)
|
||||
m1.Merge(m2)
|
||||
gtest.Assert(m1.Map(), map[string]int{"a": 1, "b": 2})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m1 := gmap.NewStrIntMap()
|
||||
m2 := gmap.NewStrIntMap()
|
||||
m1.Set("a", 1)
|
||||
m2.Set("b", 2)
|
||||
m1.Merge(m2)
|
||||
t.Assert(m1.Map(), map[string]int{"a": 1, "b": 2})
|
||||
})
|
||||
}
|
||||
|
||||
func Test_StrIntMap_Map(t *testing.T) {
|
||||
m := gmap.NewStrIntMap()
|
||||
m.Set("1", 1)
|
||||
m.Set("2", 2)
|
||||
gtest.Assert(m.Get("1"), 1)
|
||||
gtest.Assert(m.Get("2"), 2)
|
||||
data := m.Map()
|
||||
gtest.Assert(data["1"], 1)
|
||||
gtest.Assert(data["2"], 2)
|
||||
data["3"] = 3
|
||||
gtest.Assert(m.Get("3"), 3)
|
||||
m.Set("4", 4)
|
||||
gtest.Assert(data["4"], 4)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gmap.NewStrIntMap()
|
||||
m.Set("1", 1)
|
||||
m.Set("2", 2)
|
||||
t.Assert(m.Get("1"), 1)
|
||||
t.Assert(m.Get("2"), 2)
|
||||
data := m.Map()
|
||||
t.Assert(data["1"], 1)
|
||||
t.Assert(data["2"], 2)
|
||||
data["3"] = 3
|
||||
t.Assert(m.Get("3"), 3)
|
||||
m.Set("4", 4)
|
||||
t.Assert(data["4"], 4)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_StrIntMap_MapCopy(t *testing.T) {
|
||||
m := gmap.NewStrIntMap()
|
||||
m.Set("1", 1)
|
||||
m.Set("2", 2)
|
||||
gtest.Assert(m.Get("1"), 1)
|
||||
gtest.Assert(m.Get("2"), 2)
|
||||
data := m.MapCopy()
|
||||
gtest.Assert(data["1"], 1)
|
||||
gtest.Assert(data["2"], 2)
|
||||
data["3"] = 3
|
||||
gtest.Assert(m.Get("3"), 0)
|
||||
m.Set("4", 4)
|
||||
gtest.Assert(data["4"], 0)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gmap.NewStrIntMap()
|
||||
m.Set("1", 1)
|
||||
m.Set("2", 2)
|
||||
t.Assert(m.Get("1"), 1)
|
||||
t.Assert(m.Get("2"), 2)
|
||||
data := m.MapCopy()
|
||||
t.Assert(data["1"], 1)
|
||||
t.Assert(data["2"], 2)
|
||||
data["3"] = 3
|
||||
t.Assert(m.Get("3"), 0)
|
||||
m.Set("4", 4)
|
||||
t.Assert(data["4"], 0)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_StrIntMap_FilterEmpty(t *testing.T) {
|
||||
m := gmap.NewStrIntMap()
|
||||
m.Set("1", 0)
|
||||
m.Set("2", 2)
|
||||
gtest.Assert(m.Size(), 2)
|
||||
gtest.Assert(m.Get("1"), 0)
|
||||
gtest.Assert(m.Get("2"), 2)
|
||||
m.FilterEmpty()
|
||||
gtest.Assert(m.Size(), 1)
|
||||
gtest.Assert(m.Get("2"), 2)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gmap.NewStrIntMap()
|
||||
m.Set("1", 0)
|
||||
m.Set("2", 2)
|
||||
t.Assert(m.Size(), 2)
|
||||
t.Assert(m.Get("1"), 0)
|
||||
t.Assert(m.Get("2"), 2)
|
||||
m.FilterEmpty()
|
||||
t.Assert(m.Size(), 1)
|
||||
t.Assert(m.Get("2"), 2)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_StrIntMap_Json(t *testing.T) {
|
||||
// Marshal
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
data := g.MapStrInt{
|
||||
"k1": 1,
|
||||
"k2": 2,
|
||||
@ -187,125 +203,125 @@ func Test_StrIntMap_Json(t *testing.T) {
|
||||
m1 := gmap.NewStrIntMapFrom(data)
|
||||
b1, err1 := json.Marshal(m1)
|
||||
b2, err2 := json.Marshal(data)
|
||||
gtest.Assert(err1, err2)
|
||||
gtest.Assert(b1, b2)
|
||||
t.Assert(err1, err2)
|
||||
t.Assert(b1, b2)
|
||||
})
|
||||
// Unmarshal
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
data := g.MapStrInt{
|
||||
"k1": 1,
|
||||
"k2": 2,
|
||||
}
|
||||
b, err := json.Marshal(data)
|
||||
gtest.Assert(err, nil)
|
||||
t.Assert(err, nil)
|
||||
|
||||
m := gmap.NewStrIntMap()
|
||||
err = json.Unmarshal(b, m)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(m.Get("k1"), data["k1"])
|
||||
gtest.Assert(m.Get("k2"), data["k2"])
|
||||
t.Assert(err, nil)
|
||||
t.Assert(m.Get("k1"), data["k1"])
|
||||
t.Assert(m.Get("k2"), data["k2"])
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
data := g.MapStrInt{
|
||||
"k1": 1,
|
||||
"k2": 2,
|
||||
}
|
||||
b, err := json.Marshal(data)
|
||||
gtest.Assert(err, nil)
|
||||
t.Assert(err, nil)
|
||||
|
||||
var m gmap.StrIntMap
|
||||
err = json.Unmarshal(b, &m)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(m.Get("k1"), data["k1"])
|
||||
gtest.Assert(m.Get("k2"), data["k2"])
|
||||
t.Assert(err, nil)
|
||||
t.Assert(m.Get("k1"), data["k1"])
|
||||
t.Assert(m.Get("k2"), data["k2"])
|
||||
})
|
||||
}
|
||||
|
||||
func Test_StrIntMap_Pop(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gmap.NewStrIntMapFrom(g.MapStrInt{
|
||||
"k1": 11,
|
||||
"k2": 22,
|
||||
})
|
||||
gtest.Assert(m.Size(), 2)
|
||||
t.Assert(m.Size(), 2)
|
||||
|
||||
k1, v1 := m.Pop()
|
||||
gtest.AssertIN(k1, g.Slice{"k1", "k2"})
|
||||
gtest.AssertIN(v1, g.Slice{11, 22})
|
||||
gtest.Assert(m.Size(), 1)
|
||||
t.AssertIN(k1, g.Slice{"k1", "k2"})
|
||||
t.AssertIN(v1, g.Slice{11, 22})
|
||||
t.Assert(m.Size(), 1)
|
||||
k2, v2 := m.Pop()
|
||||
gtest.AssertIN(k2, g.Slice{"k1", "k2"})
|
||||
gtest.AssertIN(v2, g.Slice{11, 22})
|
||||
gtest.Assert(m.Size(), 0)
|
||||
t.AssertIN(k2, g.Slice{"k1", "k2"})
|
||||
t.AssertIN(v2, g.Slice{11, 22})
|
||||
t.Assert(m.Size(), 0)
|
||||
|
||||
gtest.AssertNE(k1, k2)
|
||||
gtest.AssertNE(v1, v2)
|
||||
t.AssertNE(k1, k2)
|
||||
t.AssertNE(v1, v2)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_StrIntMap_Pops(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gmap.NewStrIntMapFrom(g.MapStrInt{
|
||||
"k1": 11,
|
||||
"k2": 22,
|
||||
"k3": 33,
|
||||
})
|
||||
gtest.Assert(m.Size(), 3)
|
||||
t.Assert(m.Size(), 3)
|
||||
|
||||
kArray := garray.New()
|
||||
vArray := garray.New()
|
||||
for k, v := range m.Pops(1) {
|
||||
gtest.AssertIN(k, g.Slice{"k1", "k2", "k3"})
|
||||
gtest.AssertIN(v, g.Slice{11, 22, 33})
|
||||
t.AssertIN(k, g.Slice{"k1", "k2", "k3"})
|
||||
t.AssertIN(v, g.Slice{11, 22, 33})
|
||||
kArray.Append(k)
|
||||
vArray.Append(v)
|
||||
}
|
||||
gtest.Assert(m.Size(), 2)
|
||||
t.Assert(m.Size(), 2)
|
||||
for k, v := range m.Pops(2) {
|
||||
gtest.AssertIN(k, g.Slice{"k1", "k2", "k3"})
|
||||
gtest.AssertIN(v, g.Slice{11, 22, 33})
|
||||
t.AssertIN(k, g.Slice{"k1", "k2", "k3"})
|
||||
t.AssertIN(v, g.Slice{11, 22, 33})
|
||||
kArray.Append(k)
|
||||
vArray.Append(v)
|
||||
}
|
||||
gtest.Assert(m.Size(), 0)
|
||||
t.Assert(m.Size(), 0)
|
||||
|
||||
gtest.Assert(kArray.Unique().Len(), 3)
|
||||
gtest.Assert(vArray.Unique().Len(), 3)
|
||||
t.Assert(kArray.Unique().Len(), 3)
|
||||
t.Assert(vArray.Unique().Len(), 3)
|
||||
})
|
||||
}
|
||||
|
||||
func TestStrIntMap_UnmarshalValue(t *testing.T) {
|
||||
type T struct {
|
||||
type V struct {
|
||||
Name string
|
||||
Map *gmap.StrIntMap
|
||||
}
|
||||
// JSON
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var v *V
|
||||
err := gconv.Struct(map[string]interface{}{
|
||||
"name": "john",
|
||||
"map": []byte(`{"k1":1,"k2":2}`),
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Map.Size(), 2)
|
||||
gtest.Assert(t.Map.Get("k1"), 1)
|
||||
gtest.Assert(t.Map.Get("k2"), 2)
|
||||
}, &v)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(v.Name, "john")
|
||||
t.Assert(v.Map.Size(), 2)
|
||||
t.Assert(v.Map.Get("k1"), 1)
|
||||
t.Assert(v.Map.Get("k2"), 2)
|
||||
})
|
||||
// Map
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var v *V
|
||||
err := gconv.Struct(map[string]interface{}{
|
||||
"name": "john",
|
||||
"map": g.Map{
|
||||
"k1": 1,
|
||||
"k2": 2,
|
||||
},
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Map.Size(), 2)
|
||||
gtest.Assert(t.Map.Get("k1"), 1)
|
||||
gtest.Assert(t.Map.Get("k2"), 2)
|
||||
}, &v)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(v.Name, "john")
|
||||
t.Assert(v.Map.Size(), 2)
|
||||
t.Assert(v.Map.Get("k1"), 1)
|
||||
t.Assert(v.Map.Get("k2"), 2)
|
||||
})
|
||||
}
|
||||
|
||||
@ -21,162 +21,179 @@ func stringStrCallBack(string, string) bool {
|
||||
return true
|
||||
}
|
||||
func Test_StrStrMap_Basic(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gmap.NewStrStrMap()
|
||||
m.Set("a", "a")
|
||||
|
||||
gtest.Assert(m.Get("a"), "a")
|
||||
gtest.Assert(m.Size(), 1)
|
||||
gtest.Assert(m.IsEmpty(), false)
|
||||
t.Assert(m.Get("a"), "a")
|
||||
t.Assert(m.Size(), 1)
|
||||
t.Assert(m.IsEmpty(), false)
|
||||
|
||||
gtest.Assert(m.GetOrSet("b", "b"), "b")
|
||||
gtest.Assert(m.SetIfNotExist("b", "b"), false)
|
||||
t.Assert(m.GetOrSet("b", "b"), "b")
|
||||
t.Assert(m.SetIfNotExist("b", "b"), false)
|
||||
|
||||
gtest.Assert(m.SetIfNotExist("c", "c"), true)
|
||||
t.Assert(m.SetIfNotExist("c", "c"), true)
|
||||
|
||||
gtest.Assert(m.Remove("b"), "b")
|
||||
gtest.Assert(m.Contains("b"), false)
|
||||
t.Assert(m.Remove("b"), "b")
|
||||
t.Assert(m.Contains("b"), false)
|
||||
|
||||
gtest.AssertIN("c", m.Keys())
|
||||
gtest.AssertIN("a", m.Keys())
|
||||
gtest.AssertIN("a", m.Values())
|
||||
gtest.AssertIN("c", m.Values())
|
||||
t.AssertIN("c", m.Keys())
|
||||
t.AssertIN("a", m.Keys())
|
||||
t.AssertIN("a", m.Values())
|
||||
t.AssertIN("c", m.Values())
|
||||
|
||||
m.Flip()
|
||||
|
||||
gtest.Assert(m.Map(), map[string]string{"a": "a", "c": "c"})
|
||||
t.Assert(m.Map(), map[string]string{"a": "a", "c": "c"})
|
||||
|
||||
m.Clear()
|
||||
gtest.Assert(m.Size(), 0)
|
||||
gtest.Assert(m.IsEmpty(), true)
|
||||
t.Assert(m.Size(), 0)
|
||||
t.Assert(m.IsEmpty(), true)
|
||||
|
||||
m2 := gmap.NewStrStrMapFrom(map[string]string{"a": "a", "b": "b"})
|
||||
gtest.Assert(m2.Map(), map[string]string{"a": "a", "b": "b"})
|
||||
t.Assert(m2.Map(), map[string]string{"a": "a", "b": "b"})
|
||||
})
|
||||
}
|
||||
func Test_StrStrMap_Set_Fun(t *testing.T) {
|
||||
m := gmap.NewStrStrMap()
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gmap.NewStrStrMap()
|
||||
|
||||
m.GetOrSetFunc("a", getStr)
|
||||
m.GetOrSetFuncLock("b", getStr)
|
||||
gtest.Assert(m.Get("a"), "z")
|
||||
gtest.Assert(m.Get("b"), "z")
|
||||
gtest.Assert(m.SetIfNotExistFunc("a", getStr), false)
|
||||
gtest.Assert(m.SetIfNotExistFunc("c", getStr), true)
|
||||
|
||||
gtest.Assert(m.SetIfNotExistFuncLock("b", getStr), false)
|
||||
gtest.Assert(m.SetIfNotExistFuncLock("d", getStr), true)
|
||||
m.GetOrSetFunc("a", getStr)
|
||||
m.GetOrSetFuncLock("b", getStr)
|
||||
t.Assert(m.Get("a"), "z")
|
||||
t.Assert(m.Get("b"), "z")
|
||||
t.Assert(m.SetIfNotExistFunc("a", getStr), false)
|
||||
t.Assert(m.SetIfNotExistFunc("c", getStr), true)
|
||||
|
||||
t.Assert(m.SetIfNotExistFuncLock("b", getStr), false)
|
||||
t.Assert(m.SetIfNotExistFuncLock("d", getStr), true)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_StrStrMap_Batch(t *testing.T) {
|
||||
m := gmap.NewStrStrMap()
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gmap.NewStrStrMap()
|
||||
|
||||
m.Sets(map[string]string{"a": "a", "b": "b", "c": "c"})
|
||||
gtest.Assert(m.Map(), map[string]string{"a": "a", "b": "b", "c": "c"})
|
||||
m.Removes([]string{"a", "b"})
|
||||
gtest.Assert(m.Map(), map[string]string{"c": "c"})
|
||||
m.Sets(map[string]string{"a": "a", "b": "b", "c": "c"})
|
||||
t.Assert(m.Map(), map[string]string{"a": "a", "b": "b", "c": "c"})
|
||||
m.Removes([]string{"a", "b"})
|
||||
t.Assert(m.Map(), map[string]string{"c": "c"})
|
||||
})
|
||||
}
|
||||
func Test_StrStrMap_Iterator(t *testing.T) {
|
||||
expect := map[string]string{"a": "a", "b": "b"}
|
||||
m := gmap.NewStrStrMapFrom(expect)
|
||||
m.Iterator(func(k string, v string) bool {
|
||||
gtest.Assert(expect[k], v)
|
||||
return true
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
expect := map[string]string{"a": "a", "b": "b"}
|
||||
m := gmap.NewStrStrMapFrom(expect)
|
||||
m.Iterator(func(k string, v string) bool {
|
||||
t.Assert(expect[k], v)
|
||||
return true
|
||||
})
|
||||
// 断言返回值对遍历控制
|
||||
i := 0
|
||||
j := 0
|
||||
m.Iterator(func(k string, v string) bool {
|
||||
i++
|
||||
return true
|
||||
})
|
||||
m.Iterator(func(k string, v string) bool {
|
||||
j++
|
||||
return false
|
||||
})
|
||||
t.Assert(i, 2)
|
||||
t.Assert(j, 1)
|
||||
})
|
||||
// 断言返回值对遍历控制
|
||||
i := 0
|
||||
j := 0
|
||||
m.Iterator(func(k string, v string) bool {
|
||||
i++
|
||||
return true
|
||||
})
|
||||
m.Iterator(func(k string, v string) bool {
|
||||
j++
|
||||
return false
|
||||
})
|
||||
gtest.Assert(i, 2)
|
||||
gtest.Assert(j, 1)
|
||||
}
|
||||
|
||||
func Test_StrStrMap_Lock(t *testing.T) {
|
||||
expect := map[string]string{"a": "a", "b": "b"}
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
expect := map[string]string{"a": "a", "b": "b"}
|
||||
|
||||
m := gmap.NewStrStrMapFrom(expect)
|
||||
m.LockFunc(func(m map[string]string) {
|
||||
gtest.Assert(m, expect)
|
||||
})
|
||||
m.RLockFunc(func(m map[string]string) {
|
||||
gtest.Assert(m, expect)
|
||||
m := gmap.NewStrStrMapFrom(expect)
|
||||
m.LockFunc(func(m map[string]string) {
|
||||
t.Assert(m, expect)
|
||||
})
|
||||
m.RLockFunc(func(m map[string]string) {
|
||||
t.Assert(m, expect)
|
||||
})
|
||||
})
|
||||
}
|
||||
func Test_StrStrMap_Clone(t *testing.T) {
|
||||
//clone 方法是深克隆
|
||||
m := gmap.NewStrStrMapFrom(map[string]string{"a": "a", "b": "b", "c": "c"})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
//clone 方法是深克隆
|
||||
m := gmap.NewStrStrMapFrom(map[string]string{"a": "a", "b": "b", "c": "c"})
|
||||
|
||||
m_clone := m.Clone()
|
||||
m.Remove("a")
|
||||
//修改原 map,clone 后的 map 不影响
|
||||
gtest.AssertIN("a", m_clone.Keys())
|
||||
m_clone := m.Clone()
|
||||
m.Remove("a")
|
||||
//修改原 map,clone 后的 map 不影响
|
||||
t.AssertIN("a", m_clone.Keys())
|
||||
|
||||
m_clone.Remove("b")
|
||||
//修改clone map,原 map 不影响
|
||||
gtest.AssertIN("b", m.Keys())
|
||||
m_clone.Remove("b")
|
||||
//修改clone map,原 map 不影响
|
||||
t.AssertIN("b", m.Keys())
|
||||
})
|
||||
}
|
||||
func Test_StrStrMap_Merge(t *testing.T) {
|
||||
m1 := gmap.NewStrStrMap()
|
||||
m2 := gmap.NewStrStrMap()
|
||||
m1.Set("a", "a")
|
||||
m2.Set("b", "b")
|
||||
m1.Merge(m2)
|
||||
gtest.Assert(m1.Map(), map[string]string{"a": "a", "b": "b"})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m1 := gmap.NewStrStrMap()
|
||||
m2 := gmap.NewStrStrMap()
|
||||
m1.Set("a", "a")
|
||||
m2.Set("b", "b")
|
||||
m1.Merge(m2)
|
||||
t.Assert(m1.Map(), map[string]string{"a": "a", "b": "b"})
|
||||
})
|
||||
}
|
||||
|
||||
func Test_StrStrMap_Map(t *testing.T) {
|
||||
m := gmap.NewStrStrMap()
|
||||
m.Set("1", "1")
|
||||
m.Set("2", "2")
|
||||
gtest.Assert(m.Get("1"), "1")
|
||||
gtest.Assert(m.Get("2"), "2")
|
||||
data := m.Map()
|
||||
gtest.Assert(data["1"], "1")
|
||||
gtest.Assert(data["2"], "2")
|
||||
data["3"] = "3"
|
||||
gtest.Assert(m.Get("3"), "3")
|
||||
m.Set("4", "4")
|
||||
gtest.Assert(data["4"], "4")
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gmap.NewStrStrMap()
|
||||
m.Set("1", "1")
|
||||
m.Set("2", "2")
|
||||
t.Assert(m.Get("1"), "1")
|
||||
t.Assert(m.Get("2"), "2")
|
||||
data := m.Map()
|
||||
t.Assert(data["1"], "1")
|
||||
t.Assert(data["2"], "2")
|
||||
data["3"] = "3"
|
||||
t.Assert(m.Get("3"), "3")
|
||||
m.Set("4", "4")
|
||||
t.Assert(data["4"], "4")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_StrStrMap_MapCopy(t *testing.T) {
|
||||
m := gmap.NewStrStrMap()
|
||||
m.Set("1", "1")
|
||||
m.Set("2", "2")
|
||||
gtest.Assert(m.Get("1"), "1")
|
||||
gtest.Assert(m.Get("2"), "2")
|
||||
data := m.MapCopy()
|
||||
gtest.Assert(data["1"], "1")
|
||||
gtest.Assert(data["2"], "2")
|
||||
data["3"] = "3"
|
||||
gtest.Assert(m.Get("3"), "")
|
||||
m.Set("4", "4")
|
||||
gtest.Assert(data["4"], "")
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gmap.NewStrStrMap()
|
||||
m.Set("1", "1")
|
||||
m.Set("2", "2")
|
||||
t.Assert(m.Get("1"), "1")
|
||||
t.Assert(m.Get("2"), "2")
|
||||
data := m.MapCopy()
|
||||
t.Assert(data["1"], "1")
|
||||
t.Assert(data["2"], "2")
|
||||
data["3"] = "3"
|
||||
t.Assert(m.Get("3"), "")
|
||||
m.Set("4", "4")
|
||||
t.Assert(data["4"], "")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_StrStrMap_FilterEmpty(t *testing.T) {
|
||||
m := gmap.NewStrStrMap()
|
||||
m.Set("1", "")
|
||||
m.Set("2", "2")
|
||||
gtest.Assert(m.Size(), 2)
|
||||
gtest.Assert(m.Get("1"), "")
|
||||
gtest.Assert(m.Get("2"), "2")
|
||||
m.FilterEmpty()
|
||||
gtest.Assert(m.Size(), 1)
|
||||
gtest.Assert(m.Get("2"), "2")
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gmap.NewStrStrMap()
|
||||
m.Set("1", "")
|
||||
m.Set("2", "2")
|
||||
t.Assert(m.Size(), 2)
|
||||
t.Assert(m.Get("1"), "")
|
||||
t.Assert(m.Get("2"), "2")
|
||||
m.FilterEmpty()
|
||||
t.Assert(m.Size(), 1)
|
||||
t.Assert(m.Get("2"), "2")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_StrStrMap_Json(t *testing.T) {
|
||||
// Marshal
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
data := g.MapStrStr{
|
||||
"k1": "v1",
|
||||
"k2": "v2",
|
||||
@ -184,125 +201,125 @@ func Test_StrStrMap_Json(t *testing.T) {
|
||||
m1 := gmap.NewStrStrMapFrom(data)
|
||||
b1, err1 := json.Marshal(m1)
|
||||
b2, err2 := json.Marshal(data)
|
||||
gtest.Assert(err1, err2)
|
||||
gtest.Assert(b1, b2)
|
||||
t.Assert(err1, err2)
|
||||
t.Assert(b1, b2)
|
||||
})
|
||||
// Unmarshal
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
data := g.MapStrStr{
|
||||
"k1": "v1",
|
||||
"k2": "v2",
|
||||
}
|
||||
b, err := json.Marshal(data)
|
||||
gtest.Assert(err, nil)
|
||||
t.Assert(err, nil)
|
||||
|
||||
m := gmap.NewStrStrMap()
|
||||
err = json.Unmarshal(b, m)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(m.Get("k1"), data["k1"])
|
||||
gtest.Assert(m.Get("k2"), data["k2"])
|
||||
t.Assert(err, nil)
|
||||
t.Assert(m.Get("k1"), data["k1"])
|
||||
t.Assert(m.Get("k2"), data["k2"])
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
data := g.MapStrStr{
|
||||
"k1": "v1",
|
||||
"k2": "v2",
|
||||
}
|
||||
b, err := json.Marshal(data)
|
||||
gtest.Assert(err, nil)
|
||||
t.Assert(err, nil)
|
||||
|
||||
var m gmap.StrStrMap
|
||||
err = json.Unmarshal(b, &m)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(m.Get("k1"), data["k1"])
|
||||
gtest.Assert(m.Get("k2"), data["k2"])
|
||||
t.Assert(err, nil)
|
||||
t.Assert(m.Get("k1"), data["k1"])
|
||||
t.Assert(m.Get("k2"), data["k2"])
|
||||
})
|
||||
}
|
||||
|
||||
func Test_StrStrMap_Pop(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gmap.NewStrStrMapFrom(g.MapStrStr{
|
||||
"k1": "v1",
|
||||
"k2": "v2",
|
||||
})
|
||||
gtest.Assert(m.Size(), 2)
|
||||
t.Assert(m.Size(), 2)
|
||||
|
||||
k1, v1 := m.Pop()
|
||||
gtest.AssertIN(k1, g.Slice{"k1", "k2"})
|
||||
gtest.AssertIN(v1, g.Slice{"v1", "v2"})
|
||||
gtest.Assert(m.Size(), 1)
|
||||
t.AssertIN(k1, g.Slice{"k1", "k2"})
|
||||
t.AssertIN(v1, g.Slice{"v1", "v2"})
|
||||
t.Assert(m.Size(), 1)
|
||||
k2, v2 := m.Pop()
|
||||
gtest.AssertIN(k2, g.Slice{"k1", "k2"})
|
||||
gtest.AssertIN(v2, g.Slice{"v1", "v2"})
|
||||
gtest.Assert(m.Size(), 0)
|
||||
t.AssertIN(k2, g.Slice{"k1", "k2"})
|
||||
t.AssertIN(v2, g.Slice{"v1", "v2"})
|
||||
t.Assert(m.Size(), 0)
|
||||
|
||||
gtest.AssertNE(k1, k2)
|
||||
gtest.AssertNE(v1, v2)
|
||||
t.AssertNE(k1, k2)
|
||||
t.AssertNE(v1, v2)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_StrStrMap_Pops(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gmap.NewStrStrMapFrom(g.MapStrStr{
|
||||
"k1": "v1",
|
||||
"k2": "v2",
|
||||
"k3": "v3",
|
||||
})
|
||||
gtest.Assert(m.Size(), 3)
|
||||
t.Assert(m.Size(), 3)
|
||||
|
||||
kArray := garray.New()
|
||||
vArray := garray.New()
|
||||
for k, v := range m.Pops(1) {
|
||||
gtest.AssertIN(k, g.Slice{"k1", "k2", "k3"})
|
||||
gtest.AssertIN(v, g.Slice{"v1", "v2", "v3"})
|
||||
t.AssertIN(k, g.Slice{"k1", "k2", "k3"})
|
||||
t.AssertIN(v, g.Slice{"v1", "v2", "v3"})
|
||||
kArray.Append(k)
|
||||
vArray.Append(v)
|
||||
}
|
||||
gtest.Assert(m.Size(), 2)
|
||||
t.Assert(m.Size(), 2)
|
||||
for k, v := range m.Pops(2) {
|
||||
gtest.AssertIN(k, g.Slice{"k1", "k2", "k3"})
|
||||
gtest.AssertIN(v, g.Slice{"v1", "v2", "v3"})
|
||||
t.AssertIN(k, g.Slice{"k1", "k2", "k3"})
|
||||
t.AssertIN(v, g.Slice{"v1", "v2", "v3"})
|
||||
kArray.Append(k)
|
||||
vArray.Append(v)
|
||||
}
|
||||
gtest.Assert(m.Size(), 0)
|
||||
t.Assert(m.Size(), 0)
|
||||
|
||||
gtest.Assert(kArray.Unique().Len(), 3)
|
||||
gtest.Assert(vArray.Unique().Len(), 3)
|
||||
t.Assert(kArray.Unique().Len(), 3)
|
||||
t.Assert(vArray.Unique().Len(), 3)
|
||||
})
|
||||
}
|
||||
|
||||
func TestStrStrMap_UnmarshalValue(t *testing.T) {
|
||||
type T struct {
|
||||
type V struct {
|
||||
Name string
|
||||
Map *gmap.StrStrMap
|
||||
}
|
||||
// JSON
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var v *V
|
||||
err := gconv.Struct(map[string]interface{}{
|
||||
"name": "john",
|
||||
"map": []byte(`{"k1":"v1","k2":"v2"}`),
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Map.Size(), 2)
|
||||
gtest.Assert(t.Map.Get("k1"), "v1")
|
||||
gtest.Assert(t.Map.Get("k2"), "v2")
|
||||
}, &v)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(v.Name, "john")
|
||||
t.Assert(v.Map.Size(), 2)
|
||||
t.Assert(v.Map.Get("k1"), "v1")
|
||||
t.Assert(v.Map.Get("k2"), "v2")
|
||||
})
|
||||
// Map
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var v *V
|
||||
err := gconv.Struct(map[string]interface{}{
|
||||
"name": "john",
|
||||
"map": g.Map{
|
||||
"k1": "v1",
|
||||
"k2": "v2",
|
||||
},
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Map.Size(), 2)
|
||||
gtest.Assert(t.Map.Get("k1"), "v1")
|
||||
gtest.Assert(t.Map.Get("k2"), "v2")
|
||||
}, &v)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(v.Name, "john")
|
||||
t.Assert(v.Map.Size(), 2)
|
||||
t.Assert(v.Map.Get("k1"), "v1")
|
||||
t.Assert(v.Map.Get("k2"), "v2")
|
||||
})
|
||||
}
|
||||
|
||||
@ -18,96 +18,122 @@ import (
|
||||
)
|
||||
|
||||
func Test_TreeMap_Basic(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gmap.NewTreeMap(gutil.ComparatorString)
|
||||
m.Set("key1", "val1")
|
||||
gtest.Assert(m.Keys(), []interface{}{"key1"})
|
||||
t.Assert(m.Keys(), []interface{}{"key1"})
|
||||
|
||||
gtest.Assert(m.Get("key1"), "val1")
|
||||
gtest.Assert(m.Size(), 1)
|
||||
gtest.Assert(m.IsEmpty(), false)
|
||||
t.Assert(m.Get("key1"), "val1")
|
||||
t.Assert(m.Size(), 1)
|
||||
t.Assert(m.IsEmpty(), false)
|
||||
|
||||
gtest.Assert(m.GetOrSet("key2", "val2"), "val2")
|
||||
gtest.Assert(m.SetIfNotExist("key2", "val2"), false)
|
||||
t.Assert(m.GetOrSet("key2", "val2"), "val2")
|
||||
t.Assert(m.SetIfNotExist("key2", "val2"), false)
|
||||
|
||||
gtest.Assert(m.SetIfNotExist("key3", "val3"), true)
|
||||
t.Assert(m.SetIfNotExist("key3", "val3"), true)
|
||||
|
||||
gtest.Assert(m.Remove("key2"), "val2")
|
||||
gtest.Assert(m.Contains("key2"), false)
|
||||
t.Assert(m.Remove("key2"), "val2")
|
||||
t.Assert(m.Contains("key2"), false)
|
||||
|
||||
gtest.AssertIN("key3", m.Keys())
|
||||
gtest.AssertIN("key1", m.Keys())
|
||||
gtest.AssertIN("val3", m.Values())
|
||||
gtest.AssertIN("val1", m.Values())
|
||||
t.AssertIN("key3", m.Keys())
|
||||
t.AssertIN("key1", m.Keys())
|
||||
t.AssertIN("val3", m.Values())
|
||||
t.AssertIN("val1", m.Values())
|
||||
|
||||
m.Flip()
|
||||
gtest.Assert(m.Map(), map[interface{}]interface{}{"val3": "key3", "val1": "key1"})
|
||||
t.Assert(m.Map(), map[interface{}]interface{}{"val3": "key3", "val1": "key1"})
|
||||
|
||||
m.Clear()
|
||||
gtest.Assert(m.Size(), 0)
|
||||
gtest.Assert(m.IsEmpty(), true)
|
||||
t.Assert(m.Size(), 0)
|
||||
t.Assert(m.IsEmpty(), true)
|
||||
|
||||
m2 := gmap.NewTreeMapFrom(gutil.ComparatorString, map[interface{}]interface{}{1: 1, "key1": "val1"})
|
||||
gtest.Assert(m2.Map(), map[interface{}]interface{}{1: 1, "key1": "val1"})
|
||||
t.Assert(m2.Map(), map[interface{}]interface{}{1: 1, "key1": "val1"})
|
||||
})
|
||||
}
|
||||
func Test_TreeMap_Set_Fun(t *testing.T) {
|
||||
m := gmap.NewTreeMap(gutil.ComparatorString)
|
||||
m.GetOrSetFunc("fun", getValue)
|
||||
m.GetOrSetFuncLock("funlock", getValue)
|
||||
gtest.Assert(m.Get("funlock"), 3)
|
||||
gtest.Assert(m.Get("fun"), 3)
|
||||
m.GetOrSetFunc("fun", getValue)
|
||||
gtest.Assert(m.SetIfNotExistFunc("fun", getValue), false)
|
||||
gtest.Assert(m.SetIfNotExistFuncLock("funlock", getValue), false)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gmap.NewTreeMap(gutil.ComparatorString)
|
||||
m.GetOrSetFunc("fun", getValue)
|
||||
m.GetOrSetFuncLock("funlock", getValue)
|
||||
t.Assert(m.Get("funlock"), 3)
|
||||
t.Assert(m.Get("fun"), 3)
|
||||
m.GetOrSetFunc("fun", getValue)
|
||||
t.Assert(m.SetIfNotExistFunc("fun", getValue), false)
|
||||
t.Assert(m.SetIfNotExistFuncLock("funlock", getValue), false)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_TreeMap_Batch(t *testing.T) {
|
||||
m := gmap.NewTreeMap(gutil.ComparatorString)
|
||||
m.Sets(map[interface{}]interface{}{1: 1, "key1": "val1", "key2": "val2", "key3": "val3"})
|
||||
gtest.Assert(m.Map(), map[interface{}]interface{}{1: 1, "key1": "val1", "key2": "val2", "key3": "val3"})
|
||||
m.Removes([]interface{}{"key1", 1})
|
||||
gtest.Assert(m.Map(), map[interface{}]interface{}{"key2": "val2", "key3": "val3"})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gmap.NewTreeMap(gutil.ComparatorString)
|
||||
m.Sets(map[interface{}]interface{}{1: 1, "key1": "val1", "key2": "val2", "key3": "val3"})
|
||||
t.Assert(m.Map(), map[interface{}]interface{}{1: 1, "key1": "val1", "key2": "val2", "key3": "val3"})
|
||||
m.Removes([]interface{}{"key1", 1})
|
||||
t.Assert(m.Map(), map[interface{}]interface{}{"key2": "val2", "key3": "val3"})
|
||||
})
|
||||
}
|
||||
func Test_TreeMap_Iterator(t *testing.T) {
|
||||
expect := map[interface{}]interface{}{1: 1, "key1": "val1"}
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
expect := map[interface{}]interface{}{1: 1, "key1": "val1"}
|
||||
m := gmap.NewTreeMapFrom(gutil.ComparatorString, expect)
|
||||
m.Iterator(func(k interface{}, v interface{}) bool {
|
||||
t.Assert(expect[k], v)
|
||||
return true
|
||||
})
|
||||
// 断言返回值对遍历控制
|
||||
i := 0
|
||||
j := 0
|
||||
m.Iterator(func(k interface{}, v interface{}) bool {
|
||||
i++
|
||||
return true
|
||||
})
|
||||
m.Iterator(func(k interface{}, v interface{}) bool {
|
||||
j++
|
||||
return false
|
||||
})
|
||||
t.Assert(i, 2)
|
||||
t.Assert(j, 1)
|
||||
})
|
||||
|
||||
m := gmap.NewTreeMapFrom(gutil.ComparatorString, expect)
|
||||
m.Iterator(func(k interface{}, v interface{}) bool {
|
||||
gtest.Assert(expect[k], v)
|
||||
return true
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
expect := map[interface{}]interface{}{1: 1, "key1": "val1"}
|
||||
m := gmap.NewTreeMapFrom(gutil.ComparatorString, expect)
|
||||
for i := 0; i < 10; i++ {
|
||||
m.IteratorAsc(func(k interface{}, v interface{}) bool {
|
||||
t.Assert(expect[k], v)
|
||||
return true
|
||||
})
|
||||
}
|
||||
j := 0
|
||||
for i := 0; i < 10; i++ {
|
||||
m.IteratorAsc(func(k interface{}, v interface{}) bool {
|
||||
j++
|
||||
return false
|
||||
})
|
||||
}
|
||||
t.Assert(j, 10)
|
||||
})
|
||||
// 断言返回值对遍历控制
|
||||
i := 0
|
||||
j := 0
|
||||
m.Iterator(func(k interface{}, v interface{}) bool {
|
||||
i++
|
||||
return true
|
||||
})
|
||||
m.Iterator(func(k interface{}, v interface{}) bool {
|
||||
j++
|
||||
return false
|
||||
})
|
||||
gtest.Assert(i, 2)
|
||||
gtest.Assert(j, 1)
|
||||
}
|
||||
|
||||
func Test_TreeMap_Clone(t *testing.T) {
|
||||
//clone 方法是深克隆
|
||||
m := gmap.NewTreeMapFrom(gutil.ComparatorString, map[interface{}]interface{}{1: 1, "key1": "val1"})
|
||||
m_clone := m.Clone()
|
||||
m.Remove(1)
|
||||
//修改原 map,clone 后的 map 不影响
|
||||
gtest.AssertIN(1, m_clone.Keys())
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
//clone 方法是深克隆
|
||||
m := gmap.NewTreeMapFrom(gutil.ComparatorString, map[interface{}]interface{}{1: 1, "key1": "val1"})
|
||||
m_clone := m.Clone()
|
||||
m.Remove(1)
|
||||
//修改原 map,clone 后的 map 不影响
|
||||
t.AssertIN(1, m_clone.Keys())
|
||||
|
||||
m_clone.Remove("key1")
|
||||
//修改clone map,原 map 不影响
|
||||
gtest.AssertIN("key1", m.Keys())
|
||||
m_clone.Remove("key1")
|
||||
//修改clone map,原 map 不影响
|
||||
t.AssertIN("key1", m.Keys())
|
||||
})
|
||||
}
|
||||
|
||||
func Test_TreeMap_Json(t *testing.T) {
|
||||
// Marshal
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
data := g.MapAnyAny{
|
||||
"k1": "v1",
|
||||
"k2": "v2",
|
||||
@ -115,72 +141,72 @@ func Test_TreeMap_Json(t *testing.T) {
|
||||
m1 := gmap.NewTreeMapFrom(gutil.ComparatorString, data)
|
||||
b1, err1 := json.Marshal(m1)
|
||||
b2, err2 := json.Marshal(gconv.Map(data))
|
||||
gtest.Assert(err1, err2)
|
||||
gtest.Assert(b1, b2)
|
||||
t.Assert(err1, err2)
|
||||
t.Assert(b1, b2)
|
||||
})
|
||||
// Unmarshal
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
data := g.MapAnyAny{
|
||||
"k1": "v1",
|
||||
"k2": "v2",
|
||||
}
|
||||
b, err := json.Marshal(gconv.Map(data))
|
||||
gtest.Assert(err, nil)
|
||||
t.Assert(err, nil)
|
||||
|
||||
m := gmap.NewTreeMap(gutil.ComparatorString)
|
||||
err = json.Unmarshal(b, m)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(m.Get("k1"), data["k1"])
|
||||
gtest.Assert(m.Get("k2"), data["k2"])
|
||||
t.Assert(err, nil)
|
||||
t.Assert(m.Get("k1"), data["k1"])
|
||||
t.Assert(m.Get("k2"), data["k2"])
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
data := g.MapAnyAny{
|
||||
"k1": "v1",
|
||||
"k2": "v2",
|
||||
}
|
||||
b, err := json.Marshal(gconv.Map(data))
|
||||
gtest.Assert(err, nil)
|
||||
t.Assert(err, nil)
|
||||
|
||||
var m gmap.TreeMap
|
||||
err = json.Unmarshal(b, &m)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(m.Get("k1"), data["k1"])
|
||||
gtest.Assert(m.Get("k2"), data["k2"])
|
||||
t.Assert(err, nil)
|
||||
t.Assert(m.Get("k1"), data["k1"])
|
||||
t.Assert(m.Get("k2"), data["k2"])
|
||||
})
|
||||
}
|
||||
|
||||
func TestTreeMap_UnmarshalValue(t *testing.T) {
|
||||
type T struct {
|
||||
type V struct {
|
||||
Name string
|
||||
Map *gmap.TreeMap
|
||||
}
|
||||
// JSON
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var v *V
|
||||
err := gconv.Struct(map[string]interface{}{
|
||||
"name": "john",
|
||||
"map": []byte(`{"k1":"v1","k2":"v2"}`),
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Map.Size(), 2)
|
||||
gtest.Assert(t.Map.Get("k1"), "v1")
|
||||
gtest.Assert(t.Map.Get("k2"), "v2")
|
||||
}, &v)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(v.Name, "john")
|
||||
t.Assert(v.Map.Size(), 2)
|
||||
t.Assert(v.Map.Get("k1"), "v1")
|
||||
t.Assert(v.Map.Get("k2"), "v2")
|
||||
})
|
||||
// Map
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var v *V
|
||||
err := gconv.Struct(map[string]interface{}{
|
||||
"name": "john",
|
||||
"map": g.Map{
|
||||
"k1": "v1",
|
||||
"k2": "v2",
|
||||
},
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Map.Size(), 2)
|
||||
gtest.Assert(t.Map.Get("k1"), "v1")
|
||||
gtest.Assert(t.Map.Get("k2"), "v2")
|
||||
}, &v)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(v.Name, "john")
|
||||
t.Assert(v.Map.Size(), 2)
|
||||
t.Assert(v.Map.Get("k1"), "v1")
|
||||
t.Assert(v.Map.Get("k2"), "v2")
|
||||
})
|
||||
}
|
||||
|
||||
@ -17,22 +17,31 @@ import (
|
||||
"github.com/gogf/gf/os/gtimer"
|
||||
)
|
||||
|
||||
// Object-Reusable Pool.
|
||||
// Pool is an Object-Reusable Pool.
|
||||
type Pool struct {
|
||||
list *glist.List // Available/idle list.
|
||||
closed *gtype.Bool // Whether the pool is closed.
|
||||
Expire int64 // Max idle time(ms), after which it is recycled.
|
||||
NewFunc func() (interface{}, error) // Callback function to create item.
|
||||
ExpireFunc func(interface{}) // Expired destruction function for objects.
|
||||
// This function needs to be defined when the pool object
|
||||
// needs to perform additional destruction operations.
|
||||
// Available/idle items list.
|
||||
list *glist.List
|
||||
|
||||
// Whether the pool is closed.
|
||||
closed *gtype.Bool
|
||||
|
||||
// Time To Live for pool items.
|
||||
TTL time.Duration
|
||||
|
||||
// Callback function to create pool item.
|
||||
NewFunc func() (interface{}, error)
|
||||
|
||||
// ExpireFunc is the for expired items destruction.
|
||||
// This function needs to be defined when the pool items
|
||||
// need to perform additional destruction operations.
|
||||
// Eg: net.Conn, os.File, etc.
|
||||
ExpireFunc func(interface{})
|
||||
}
|
||||
|
||||
// Pool item.
|
||||
type poolItem struct {
|
||||
expire int64 // Expire time(millisecond).
|
||||
value interface{} // Value.
|
||||
expire int64 // Expire timestamp in milliseconds.
|
||||
value interface{} // Item value.
|
||||
}
|
||||
|
||||
// Creation function for object.
|
||||
@ -41,47 +50,64 @@ type NewFunc func() (interface{}, error)
|
||||
// Destruction function for object.
|
||||
type ExpireFunc func(interface{})
|
||||
|
||||
// New returns a new object pool.
|
||||
// New creates and returns a new object pool.
|
||||
// To ensure execution efficiency, the expiration time cannot be modified once it is set.
|
||||
//
|
||||
// Expiration logic:
|
||||
// expire = 0 : not expired;
|
||||
// expire < 0 : immediate expired after use;
|
||||
// expire > 0 : timeout expired;
|
||||
// Note that the expiration time unit is ** milliseconds **.
|
||||
func New(expire int, newFunc NewFunc, expireFunc ...ExpireFunc) *Pool {
|
||||
// Note the expiration logic:
|
||||
// ttl = 0 : not expired;
|
||||
// ttl < 0 : immediate expired after use;
|
||||
// ttl > 0 : timeout expired;
|
||||
func New(ttl time.Duration, newFunc NewFunc, expireFunc ...ExpireFunc) *Pool {
|
||||
r := &Pool{
|
||||
list: glist.New(true),
|
||||
closed: gtype.NewBool(),
|
||||
Expire: int64(expire),
|
||||
TTL: ttl,
|
||||
NewFunc: newFunc,
|
||||
}
|
||||
if len(expireFunc) > 0 {
|
||||
r.ExpireFunc = expireFunc[0]
|
||||
}
|
||||
gtimer.AddSingleton(time.Second, r.checkExpire)
|
||||
gtimer.AddSingleton(time.Second, r.checkExpireItems)
|
||||
return r
|
||||
}
|
||||
|
||||
// Put puts an item to pool.
|
||||
func (p *Pool) Put(value interface{}) {
|
||||
func (p *Pool) Put(value interface{}) error {
|
||||
if p.closed.Val() {
|
||||
return errors.New("pool is closed")
|
||||
}
|
||||
item := &poolItem{
|
||||
value: value,
|
||||
}
|
||||
if p.Expire == 0 {
|
||||
if p.TTL == 0 {
|
||||
item.expire = 0
|
||||
} else {
|
||||
item.expire = gtime.TimestampMilli() + p.Expire
|
||||
// As for Golang version < 1.13, there's no method Milliseconds for time.Duration.
|
||||
// So we need calculate the milliseconds using its nanoseconds value.
|
||||
item.expire = gtime.TimestampMilli() + p.TTL.Nanoseconds()/1000000
|
||||
}
|
||||
p.list.PushBack(item)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Clear clears pool, which means it will remove all items from pool.
|
||||
func (p *Pool) Clear() {
|
||||
p.list.RemoveAll()
|
||||
if p.ExpireFunc != nil {
|
||||
for {
|
||||
if r := p.list.PopFront(); r != nil {
|
||||
p.ExpireFunc(r.(*poolItem).value)
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
} else {
|
||||
p.list.RemoveAll()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Get picks an item from pool.
|
||||
// Get picks and returns an item from pool. If the pool is empty and NewFunc is defined,
|
||||
// it creates and returns one from NewFunc.
|
||||
func (p *Pool) Get() (interface{}, error) {
|
||||
for !p.closed.Val() {
|
||||
if r := p.list.PopFront(); r != nil {
|
||||
@ -106,12 +132,13 @@ func (p *Pool) Size() int {
|
||||
|
||||
// Close closes the pool. If <p> has ExpireFunc,
|
||||
// then it automatically closes all items using this function before it's closed.
|
||||
// Commonly you do not need call this function manually.
|
||||
func (p *Pool) Close() {
|
||||
p.closed.Set(true)
|
||||
}
|
||||
|
||||
// checkExpire removes expired items from pool every second.
|
||||
func (p *Pool) checkExpire() {
|
||||
// checkExpire removes expired items from pool in every second.
|
||||
func (p *Pool) checkExpireItems() {
|
||||
if p.closed.Val() {
|
||||
// If p has ExpireFunc,
|
||||
// then it must close all items using this function.
|
||||
@ -126,11 +153,24 @@ func (p *Pool) checkExpire() {
|
||||
}
|
||||
gtimer.Exit()
|
||||
}
|
||||
// All items do not expire.
|
||||
if p.TTL == 0 {
|
||||
return
|
||||
}
|
||||
// The latest item expire timestamp in milliseconds.
|
||||
var latestExpire int64 = -1
|
||||
// Retrieve the current timestamp in milliseconds, it expires the items
|
||||
// by comparing with this timestamp. It is not accurate comparison for
|
||||
// every items expired, but high performance.
|
||||
var timestampMilli = gtime.TimestampMilli()
|
||||
for {
|
||||
// TODO Do not use Pop and Push mechanism, which is not graceful.
|
||||
if latestExpire > timestampMilli {
|
||||
break
|
||||
}
|
||||
if r := p.list.PopFront(); r != nil {
|
||||
item := r.(*poolItem)
|
||||
if item.expire == 0 || item.expire > gtime.TimestampMilli() {
|
||||
latestExpire = item.expire
|
||||
if item.expire > timestampMilli {
|
||||
p.list.PushFront(item)
|
||||
break
|
||||
}
|
||||
|
||||
@ -11,11 +11,12 @@ package gpool_test
|
||||
import (
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/gogf/gf/container/gpool"
|
||||
)
|
||||
|
||||
var pool = gpool.New(99999999, nil)
|
||||
var pool = gpool.New(time.Hour, nil)
|
||||
var syncp = sync.Pool{}
|
||||
|
||||
func BenchmarkGPoolPut(b *testing.B) {
|
||||
|
||||
@ -28,7 +28,7 @@ var ef gpool.ExpireFunc = func(i interface{}) {
|
||||
}
|
||||
|
||||
func Test_Gpool(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
//
|
||||
//expire = 0
|
||||
p1 := gpool.New(0, nf)
|
||||
@ -37,63 +37,63 @@ func Test_Gpool(t *testing.T) {
|
||||
time.Sleep(1 * time.Second)
|
||||
//test won't be timeout
|
||||
v1, err1 := p1.Get()
|
||||
gtest.Assert(err1, nil)
|
||||
gtest.AssertIN(v1, g.Slice{1, 2})
|
||||
t.Assert(err1, nil)
|
||||
t.AssertIN(v1, g.Slice{1, 2})
|
||||
//test clear
|
||||
p1.Clear()
|
||||
gtest.Assert(p1.Size(), 0)
|
||||
t.Assert(p1.Size(), 0)
|
||||
//test newFunc
|
||||
v1, err1 = p1.Get()
|
||||
gtest.Assert(err1, nil)
|
||||
gtest.Assert(v1, "hello")
|
||||
t.Assert(err1, nil)
|
||||
t.Assert(v1, "hello")
|
||||
//put data again
|
||||
p1.Put(3)
|
||||
p1.Put(4)
|
||||
v1, err1 = p1.Get()
|
||||
gtest.Assert(err1, nil)
|
||||
gtest.AssertIN(v1, g.Slice{3, 4})
|
||||
t.Assert(err1, nil)
|
||||
t.AssertIN(v1, g.Slice{3, 4})
|
||||
//test close
|
||||
p1.Close()
|
||||
v1, err1 = p1.Get()
|
||||
gtest.Assert(err1, nil)
|
||||
gtest.Assert(v1, "hello")
|
||||
t.Assert(err1, nil)
|
||||
t.Assert(v1, "hello")
|
||||
})
|
||||
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
//
|
||||
//expire > 0
|
||||
p2 := gpool.New(2000, nil, ef)
|
||||
p2 := gpool.New(2*time.Second, nil, ef)
|
||||
for index := 0; index < 10; index++ {
|
||||
p2.Put(index)
|
||||
}
|
||||
gtest.Assert(p2.Size(), 10)
|
||||
t.Assert(p2.Size(), 10)
|
||||
v2, err2 := p2.Get()
|
||||
gtest.Assert(err2, nil)
|
||||
gtest.Assert(v2, 0)
|
||||
t.Assert(err2, nil)
|
||||
t.Assert(v2, 0)
|
||||
//test timeout expireFunc
|
||||
time.Sleep(3 * time.Second)
|
||||
v2, err2 = p2.Get()
|
||||
gtest.Assert(err2, errors.New("pool is empty"))
|
||||
gtest.Assert(v2, nil)
|
||||
t.Assert(err2, errors.New("pool is empty"))
|
||||
t.Assert(v2, nil)
|
||||
//test close expireFunc
|
||||
for index := 0; index < 10; index++ {
|
||||
p2.Put(index)
|
||||
}
|
||||
gtest.Assert(p2.Size(), 10)
|
||||
t.Assert(p2.Size(), 10)
|
||||
v2, err2 = p2.Get()
|
||||
gtest.Assert(err2, nil)
|
||||
gtest.Assert(v2, 0)
|
||||
t.Assert(err2, nil)
|
||||
t.Assert(v2, 0)
|
||||
assertIndex = 0
|
||||
p2.Close()
|
||||
time.Sleep(3 * time.Second)
|
||||
})
|
||||
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
//
|
||||
//expire < 0
|
||||
p3 := gpool.New(-1, nil)
|
||||
v3, err3 := p3.Get()
|
||||
gtest.Assert(err3, errors.New("pool is empty"))
|
||||
gtest.Assert(v3, nil)
|
||||
t.Assert(err3, errors.New("pool is empty"))
|
||||
t.Assert(v3, nil)
|
||||
})
|
||||
}
|
||||
|
||||
@ -17,41 +17,49 @@ import (
|
||||
)
|
||||
|
||||
func TestQueue_Len(t *testing.T) {
|
||||
max := 100
|
||||
for n := 10; n < max; n++ {
|
||||
q1 := gqueue.New(max)
|
||||
for i := 0; i < max; i++ {
|
||||
q1.Push(i)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
max := 100
|
||||
for n := 10; n < max; n++ {
|
||||
q1 := gqueue.New(max)
|
||||
for i := 0; i < max; i++ {
|
||||
q1.Push(i)
|
||||
}
|
||||
t.Assert(q1.Len(), max)
|
||||
t.Assert(q1.Size(), max)
|
||||
}
|
||||
gtest.Assert(q1.Len(), max)
|
||||
gtest.Assert(q1.Size(), max)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestQueue_Basic(t *testing.T) {
|
||||
q := gqueue.New()
|
||||
for i := 0; i < 100; i++ {
|
||||
q.Push(i)
|
||||
}
|
||||
gtest.Assert(q.Pop(), 0)
|
||||
gtest.Assert(q.Pop(), 1)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
q := gqueue.New()
|
||||
for i := 0; i < 100; i++ {
|
||||
q.Push(i)
|
||||
}
|
||||
t.Assert(q.Pop(), 0)
|
||||
t.Assert(q.Pop(), 1)
|
||||
})
|
||||
}
|
||||
|
||||
func TestQueue_Pop(t *testing.T) {
|
||||
q1 := gqueue.New()
|
||||
q1.Push(1)
|
||||
q1.Push(2)
|
||||
q1.Push(3)
|
||||
q1.Push(4)
|
||||
i1 := q1.Pop()
|
||||
gtest.Assert(i1, 1)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
q1 := gqueue.New()
|
||||
q1.Push(1)
|
||||
q1.Push(2)
|
||||
q1.Push(3)
|
||||
q1.Push(4)
|
||||
i1 := q1.Pop()
|
||||
t.Assert(i1, 1)
|
||||
})
|
||||
}
|
||||
|
||||
func TestQueue_Close(t *testing.T) {
|
||||
q1 := gqueue.New()
|
||||
q1.Push(1)
|
||||
q1.Push(2)
|
||||
time.Sleep(time.Millisecond)
|
||||
gtest.Assert(q1.Len(), 2)
|
||||
q1.Close()
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
q1 := gqueue.New()
|
||||
q1.Push(1)
|
||||
q1.Push(2)
|
||||
time.Sleep(time.Millisecond)
|
||||
t.Assert(q1.Len(), 2)
|
||||
q1.Close()
|
||||
})
|
||||
}
|
||||
|
||||
@ -16,7 +16,7 @@ type Student struct {
|
||||
}
|
||||
|
||||
func TestRing_Val(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
//定义cap 为3的ring类型数据
|
||||
r := gring.New(3, true)
|
||||
//分别给3个元素初始化赋值
|
||||
@ -25,46 +25,46 @@ func TestRing_Val(t *testing.T) {
|
||||
r.Put(&Student{3, "alon", false})
|
||||
|
||||
//元素取值并判断和预设值是否相等
|
||||
gtest.Assert(r.Val().(*Student).name, "jimmy")
|
||||
t.Assert(r.Val().(*Student).name, "jimmy")
|
||||
//从当前位置往后移两个元素
|
||||
r.Move(2)
|
||||
gtest.Assert(r.Val().(*Student).name, "alon")
|
||||
t.Assert(r.Val().(*Student).name, "alon")
|
||||
//更新元素值
|
||||
//测试 value == nil
|
||||
r.Set(nil)
|
||||
gtest.Assert(r.Val(), nil)
|
||||
t.Assert(r.Val(), nil)
|
||||
//测试value != nil
|
||||
r.Set(&Student{3, "jack", true})
|
||||
})
|
||||
}
|
||||
func TestRing_CapLen(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
r := gring.New(10)
|
||||
r.Put("goframe")
|
||||
//cap长度 10
|
||||
gtest.Assert(r.Cap(), 10)
|
||||
t.Assert(r.Cap(), 10)
|
||||
//已有数据项 1
|
||||
gtest.Assert(r.Len(), 1)
|
||||
t.Assert(r.Len(), 1)
|
||||
})
|
||||
}
|
||||
|
||||
func TestRing_Position(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
r := gring.New(2)
|
||||
r.Put(1)
|
||||
r.Put(2)
|
||||
//往后移动1个元素
|
||||
r.Next()
|
||||
gtest.Assert(r.Val(), 2)
|
||||
t.Assert(r.Val(), 2)
|
||||
//往前移动1个元素
|
||||
r.Prev()
|
||||
gtest.Assert(r.Val(), 1)
|
||||
t.Assert(r.Val(), 1)
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
func TestRing_Link(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
r := gring.New(3)
|
||||
r.Put(1)
|
||||
r.Put(2)
|
||||
@ -74,13 +74,13 @@ func TestRing_Link(t *testing.T) {
|
||||
s.Put("b")
|
||||
|
||||
rs := r.Link(s)
|
||||
gtest.Assert(rs.Move(2).Val(), "b")
|
||||
t.Assert(rs.Move(2).Val(), "b")
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
func TestRing_Unlink(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
r := gring.New(5)
|
||||
for i := 0; i < 5; i++ {
|
||||
r.Put(i + 1)
|
||||
@ -89,13 +89,13 @@ func TestRing_Unlink(t *testing.T) {
|
||||
// 删除当前位置往后的2个数据,返回被删除的数据
|
||||
// 重新计算s len
|
||||
s := r.Unlink(2) // 2 3
|
||||
gtest.Assert(s.Val(), 2)
|
||||
gtest.Assert(s.Len(), 1)
|
||||
t.Assert(s.Val(), 2)
|
||||
t.Assert(s.Len(), 1)
|
||||
})
|
||||
}
|
||||
|
||||
func TestRing_Slice(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
ringLen := 5
|
||||
r := gring.New(ringLen)
|
||||
for i := 0; i < ringLen; i++ {
|
||||
@ -103,49 +103,49 @@ func TestRing_Slice(t *testing.T) {
|
||||
}
|
||||
r.Move(2) // 3
|
||||
array := r.SliceNext() // [3 4 5 1 2]
|
||||
gtest.Assert(array[0], 3)
|
||||
gtest.Assert(len(array), 5)
|
||||
t.Assert(array[0], 3)
|
||||
t.Assert(len(array), 5)
|
||||
|
||||
//判断array是否等于[3 4 5 1 2]
|
||||
ra := []int{3, 4, 5, 1, 2}
|
||||
gtest.Assert(ra, array)
|
||||
t.Assert(ra, array)
|
||||
|
||||
//第3个元素设为nil
|
||||
r.Set(nil)
|
||||
array2 := r.SliceNext() //[4 5 1 2]
|
||||
//返回当前位置往后不为空的元素数组,长度为4
|
||||
gtest.Assert(array2, g.Slice{4, 5, 1, 2})
|
||||
t.Assert(array2, g.Slice{4, 5, 1, 2})
|
||||
|
||||
array3 := r.SlicePrev() //[2 1 5 4]
|
||||
gtest.Assert(array3, g.Slice{2, 1, 5, 4})
|
||||
t.Assert(array3, g.Slice{2, 1, 5, 4})
|
||||
|
||||
s := gring.New(ringLen)
|
||||
for i := 0; i < ringLen; i++ {
|
||||
s.Put(i + 1)
|
||||
}
|
||||
array4 := s.SlicePrev() // []
|
||||
gtest.Assert(array4, g.Slice{1, 5, 4, 3, 2})
|
||||
t.Assert(array4, g.Slice{1, 5, 4, 3, 2})
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
func TestRing_RLockIterator(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
ringLen := 5
|
||||
r := gring.New(ringLen)
|
||||
|
||||
//ring不存在有值元素
|
||||
r.RLockIteratorNext(func(v interface{}) bool {
|
||||
gtest.Assert(v, nil)
|
||||
t.Assert(v, nil)
|
||||
return false
|
||||
})
|
||||
r.RLockIteratorNext(func(v interface{}) bool {
|
||||
gtest.Assert(v, nil)
|
||||
t.Assert(v, nil)
|
||||
return true
|
||||
})
|
||||
|
||||
r.RLockIteratorPrev(func(v interface{}) bool {
|
||||
gtest.Assert(v, nil)
|
||||
t.Assert(v, nil)
|
||||
return true
|
||||
})
|
||||
|
||||
@ -156,14 +156,14 @@ func TestRing_RLockIterator(t *testing.T) {
|
||||
//回调函数返回true,RLockIteratorNext遍历5次,期望值分别是1、2、3、4、5
|
||||
i := 0
|
||||
r.RLockIteratorNext(func(v interface{}) bool {
|
||||
gtest.Assert(v, i+1)
|
||||
t.Assert(v, i+1)
|
||||
i++
|
||||
return true
|
||||
})
|
||||
|
||||
//RLockIteratorPrev遍历1次返回 false,退出遍历
|
||||
r.RLockIteratorPrev(func(v interface{}) bool {
|
||||
gtest.Assert(v, 1)
|
||||
t.Assert(v, 1)
|
||||
return false
|
||||
})
|
||||
|
||||
@ -171,30 +171,30 @@ func TestRing_RLockIterator(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestRing_LockIterator(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
ringLen := 5
|
||||
r := gring.New(ringLen)
|
||||
|
||||
//不存在有值元素
|
||||
r.LockIteratorNext(func(item *ring.Ring) bool {
|
||||
gtest.Assert(item.Value, nil)
|
||||
t.Assert(item.Value, nil)
|
||||
return false
|
||||
})
|
||||
r.LockIteratorNext(func(item *ring.Ring) bool {
|
||||
gtest.Assert(item.Value, nil)
|
||||
t.Assert(item.Value, nil)
|
||||
return false
|
||||
})
|
||||
r.LockIteratorNext(func(item *ring.Ring) bool {
|
||||
gtest.Assert(item.Value, nil)
|
||||
t.Assert(item.Value, nil)
|
||||
return true
|
||||
})
|
||||
|
||||
r.LockIteratorPrev(func(item *ring.Ring) bool {
|
||||
gtest.Assert(item.Value, nil)
|
||||
t.Assert(item.Value, nil)
|
||||
return false
|
||||
})
|
||||
r.LockIteratorPrev(func(item *ring.Ring) bool {
|
||||
gtest.Assert(item.Value, nil)
|
||||
t.Assert(item.Value, nil)
|
||||
return true
|
||||
})
|
||||
|
||||
@ -208,7 +208,7 @@ func TestRing_LockIterator(t *testing.T) {
|
||||
ii := 0
|
||||
r.LockIteratorNext(func(item *ring.Ring) bool {
|
||||
//校验每一次遍历取值是否是期望值
|
||||
gtest.Assert(item.Value, array1[ii])
|
||||
t.Assert(item.Value, array1[ii])
|
||||
ii++
|
||||
return true
|
||||
})
|
||||
@ -221,7 +221,7 @@ func TestRing_LockIterator(t *testing.T) {
|
||||
if i > 2 {
|
||||
return false
|
||||
}
|
||||
gtest.Assert(item.Value, a[i])
|
||||
t.Assert(item.Value, a[i])
|
||||
i++
|
||||
return true
|
||||
})
|
||||
|
||||
@ -22,50 +22,50 @@ import (
|
||||
)
|
||||
|
||||
func TestSet_New(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s := gset.New()
|
||||
s.Add(1).Add(1).Add(2)
|
||||
s.Add([]interface{}{3, 4}...)
|
||||
gtest.Assert(s.Size(), 4)
|
||||
gtest.AssertIN(1, s.Slice())
|
||||
gtest.AssertIN(2, s.Slice())
|
||||
gtest.AssertIN(3, s.Slice())
|
||||
gtest.AssertIN(4, s.Slice())
|
||||
gtest.AssertNI(0, s.Slice())
|
||||
gtest.Assert(s.Contains(4), true)
|
||||
gtest.Assert(s.Contains(5), false)
|
||||
t.Assert(s.Size(), 4)
|
||||
t.AssertIN(1, s.Slice())
|
||||
t.AssertIN(2, s.Slice())
|
||||
t.AssertIN(3, s.Slice())
|
||||
t.AssertIN(4, s.Slice())
|
||||
t.AssertNI(0, s.Slice())
|
||||
t.Assert(s.Contains(4), true)
|
||||
t.Assert(s.Contains(5), false)
|
||||
s.Remove(1)
|
||||
gtest.Assert(s.Size(), 3)
|
||||
t.Assert(s.Size(), 3)
|
||||
s.Clear()
|
||||
gtest.Assert(s.Size(), 0)
|
||||
t.Assert(s.Size(), 0)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSet_Basic(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s := gset.NewSet()
|
||||
s.Add(1).Add(1).Add(2)
|
||||
s.Add([]interface{}{3, 4}...)
|
||||
gtest.Assert(s.Size(), 4)
|
||||
gtest.AssertIN(1, s.Slice())
|
||||
gtest.AssertIN(2, s.Slice())
|
||||
gtest.AssertIN(3, s.Slice())
|
||||
gtest.AssertIN(4, s.Slice())
|
||||
gtest.AssertNI(0, s.Slice())
|
||||
gtest.Assert(s.Contains(4), true)
|
||||
gtest.Assert(s.Contains(5), false)
|
||||
t.Assert(s.Size(), 4)
|
||||
t.AssertIN(1, s.Slice())
|
||||
t.AssertIN(2, s.Slice())
|
||||
t.AssertIN(3, s.Slice())
|
||||
t.AssertIN(4, s.Slice())
|
||||
t.AssertNI(0, s.Slice())
|
||||
t.Assert(s.Contains(4), true)
|
||||
t.Assert(s.Contains(5), false)
|
||||
s.Remove(1)
|
||||
gtest.Assert(s.Size(), 3)
|
||||
t.Assert(s.Size(), 3)
|
||||
s.Clear()
|
||||
gtest.Assert(s.Size(), 0)
|
||||
t.Assert(s.Size(), 0)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSet_Iterator(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s := gset.NewSet()
|
||||
s.Add(1).Add(2).Add(3)
|
||||
gtest.Assert(s.Size(), 3)
|
||||
t.Assert(s.Size(), 3)
|
||||
|
||||
a1 := garray.New(true)
|
||||
a2 := garray.New(true)
|
||||
@ -77,22 +77,22 @@ func TestSet_Iterator(t *testing.T) {
|
||||
a2.Append(1)
|
||||
return true
|
||||
})
|
||||
gtest.Assert(a1.Len(), 1)
|
||||
gtest.Assert(a2.Len(), 3)
|
||||
t.Assert(a1.Len(), 1)
|
||||
t.Assert(a2.Len(), 3)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSet_LockFunc(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s := gset.NewSet()
|
||||
s.Add(1).Add(2).Add(3)
|
||||
gtest.Assert(s.Size(), 3)
|
||||
t.Assert(s.Size(), 3)
|
||||
s.LockFunc(func(m map[interface{}]struct{}) {
|
||||
delete(m, 1)
|
||||
})
|
||||
gtest.Assert(s.Size(), 2)
|
||||
t.Assert(s.Size(), 2)
|
||||
s.RLockFunc(func(m map[interface{}]struct{}) {
|
||||
gtest.Assert(m, map[interface{}]struct{}{
|
||||
t.Assert(m, map[interface{}]struct{}{
|
||||
3: struct{}{},
|
||||
2: struct{}{},
|
||||
})
|
||||
@ -101,302 +101,302 @@ func TestSet_LockFunc(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestSet_Equal(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s1 := gset.NewSet()
|
||||
s2 := gset.NewSet()
|
||||
s3 := gset.NewSet()
|
||||
s1.Add(1).Add(2).Add(3)
|
||||
s2.Add(1).Add(2).Add(3)
|
||||
s3.Add(1).Add(2).Add(3).Add(4)
|
||||
gtest.Assert(s1.Equal(s2), true)
|
||||
gtest.Assert(s1.Equal(s3), false)
|
||||
t.Assert(s1.Equal(s2), true)
|
||||
t.Assert(s1.Equal(s3), false)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSet_IsSubsetOf(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s1 := gset.NewSet()
|
||||
s2 := gset.NewSet()
|
||||
s3 := gset.NewSet()
|
||||
s1.Add(1).Add(2)
|
||||
s2.Add(1).Add(2).Add(3)
|
||||
s3.Add(1).Add(2).Add(3).Add(4)
|
||||
gtest.Assert(s1.IsSubsetOf(s2), true)
|
||||
gtest.Assert(s2.IsSubsetOf(s3), true)
|
||||
gtest.Assert(s1.IsSubsetOf(s3), true)
|
||||
gtest.Assert(s2.IsSubsetOf(s1), false)
|
||||
gtest.Assert(s3.IsSubsetOf(s2), false)
|
||||
t.Assert(s1.IsSubsetOf(s2), true)
|
||||
t.Assert(s2.IsSubsetOf(s3), true)
|
||||
t.Assert(s1.IsSubsetOf(s3), true)
|
||||
t.Assert(s2.IsSubsetOf(s1), false)
|
||||
t.Assert(s3.IsSubsetOf(s2), false)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSet_Union(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s1 := gset.NewSet()
|
||||
s2 := gset.NewSet()
|
||||
s1.Add(1).Add(2)
|
||||
s2.Add(3).Add(4)
|
||||
s3 := s1.Union(s2)
|
||||
gtest.Assert(s3.Contains(1), true)
|
||||
gtest.Assert(s3.Contains(2), true)
|
||||
gtest.Assert(s3.Contains(3), true)
|
||||
gtest.Assert(s3.Contains(4), true)
|
||||
t.Assert(s3.Contains(1), true)
|
||||
t.Assert(s3.Contains(2), true)
|
||||
t.Assert(s3.Contains(3), true)
|
||||
t.Assert(s3.Contains(4), true)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSet_Diff(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s1 := gset.NewSet()
|
||||
s2 := gset.NewSet()
|
||||
s1.Add(1).Add(2).Add(3)
|
||||
s2.Add(3).Add(4).Add(5)
|
||||
s3 := s1.Diff(s2)
|
||||
gtest.Assert(s3.Contains(1), true)
|
||||
gtest.Assert(s3.Contains(2), true)
|
||||
gtest.Assert(s3.Contains(3), false)
|
||||
gtest.Assert(s3.Contains(4), false)
|
||||
t.Assert(s3.Contains(1), true)
|
||||
t.Assert(s3.Contains(2), true)
|
||||
t.Assert(s3.Contains(3), false)
|
||||
t.Assert(s3.Contains(4), false)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSet_Intersect(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s1 := gset.NewSet()
|
||||
s2 := gset.NewSet()
|
||||
s1.Add(1).Add(2).Add(3)
|
||||
s2.Add(3).Add(4).Add(5)
|
||||
s3 := s1.Intersect(s2)
|
||||
gtest.Assert(s3.Contains(1), false)
|
||||
gtest.Assert(s3.Contains(2), false)
|
||||
gtest.Assert(s3.Contains(3), true)
|
||||
gtest.Assert(s3.Contains(4), false)
|
||||
t.Assert(s3.Contains(1), false)
|
||||
t.Assert(s3.Contains(2), false)
|
||||
t.Assert(s3.Contains(3), true)
|
||||
t.Assert(s3.Contains(4), false)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSet_Complement(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s1 := gset.NewSet()
|
||||
s2 := gset.NewSet()
|
||||
s1.Add(1).Add(2).Add(3)
|
||||
s2.Add(3).Add(4).Add(5)
|
||||
s3 := s1.Complement(s2)
|
||||
gtest.Assert(s3.Contains(1), false)
|
||||
gtest.Assert(s3.Contains(2), false)
|
||||
gtest.Assert(s3.Contains(4), true)
|
||||
gtest.Assert(s3.Contains(5), true)
|
||||
t.Assert(s3.Contains(1), false)
|
||||
t.Assert(s3.Contains(2), false)
|
||||
t.Assert(s3.Contains(4), true)
|
||||
t.Assert(s3.Contains(5), true)
|
||||
})
|
||||
}
|
||||
|
||||
func TestNewFrom(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s1 := gset.NewFrom("a")
|
||||
s2 := gset.NewFrom("b", false)
|
||||
s3 := gset.NewFrom(3, true)
|
||||
s4 := gset.NewFrom([]string{"s1", "s2"}, true)
|
||||
gtest.Assert(s1.Contains("a"), true)
|
||||
gtest.Assert(s2.Contains("b"), true)
|
||||
gtest.Assert(s3.Contains(3), true)
|
||||
gtest.Assert(s4.Contains("s1"), true)
|
||||
gtest.Assert(s4.Contains("s3"), false)
|
||||
t.Assert(s1.Contains("a"), true)
|
||||
t.Assert(s2.Contains("b"), true)
|
||||
t.Assert(s3.Contains(3), true)
|
||||
t.Assert(s4.Contains("s1"), true)
|
||||
t.Assert(s4.Contains("s3"), false)
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
func TestNew(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s1 := gset.New()
|
||||
s1.Add("a").Add(2)
|
||||
s2 := gset.New(true)
|
||||
s2.Add("b").Add(3)
|
||||
gtest.Assert(s1.Contains("a"), true)
|
||||
t.Assert(s1.Contains("a"), true)
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
func TestSet_Join(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s1 := gset.New(true)
|
||||
s1.Add("a").Add("a1").Add("b").Add("c")
|
||||
str1 := s1.Join(",")
|
||||
gtest.Assert(strings.Contains(str1, "a1"), true)
|
||||
t.Assert(strings.Contains(str1, "a1"), true)
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s1 := gset.New(true)
|
||||
s1.Add("a").Add(`"b"`).Add(`\c`)
|
||||
str1 := s1.Join(",")
|
||||
gtest.Assert(strings.Contains(str1, `"b"`), true)
|
||||
gtest.Assert(strings.Contains(str1, `\c`), true)
|
||||
gtest.Assert(strings.Contains(str1, `a`), true)
|
||||
t.Assert(strings.Contains(str1, `"b"`), true)
|
||||
t.Assert(strings.Contains(str1, `\c`), true)
|
||||
t.Assert(strings.Contains(str1, `a`), true)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSet_String(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s1 := gset.New(true)
|
||||
s1.Add("a").Add("a2").Add("b").Add("c")
|
||||
str1 := s1.String()
|
||||
gtest.Assert(strings.Contains(str1, "["), true)
|
||||
gtest.Assert(strings.Contains(str1, "]"), true)
|
||||
gtest.Assert(strings.Contains(str1, "a2"), true)
|
||||
t.Assert(strings.Contains(str1, "["), true)
|
||||
t.Assert(strings.Contains(str1, "]"), true)
|
||||
t.Assert(strings.Contains(str1, "a2"), true)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSet_Merge(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s1 := gset.New(true)
|
||||
s2 := gset.New(true)
|
||||
s1.Add("a").Add("a2").Add("b").Add("c")
|
||||
s2.Add("b").Add("b1").Add("e").Add("f")
|
||||
ss := s1.Merge(s2)
|
||||
gtest.Assert(ss.Contains("a2"), true)
|
||||
gtest.Assert(ss.Contains("b1"), true)
|
||||
t.Assert(ss.Contains("a2"), true)
|
||||
t.Assert(ss.Contains("b1"), true)
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
func TestSet_Sum(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s1 := gset.New(true)
|
||||
s1.Add(1).Add(2).Add(3).Add(4)
|
||||
gtest.Assert(s1.Sum(), int(10))
|
||||
t.Assert(s1.Sum(), int(10))
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
func TestSet_Pop(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s := gset.New(true)
|
||||
s.Add(1).Add(2).Add(3).Add(4)
|
||||
gtest.Assert(s.Size(), 4)
|
||||
gtest.AssertIN(s.Pop(), []int{1, 2, 3, 4})
|
||||
gtest.Assert(s.Size(), 3)
|
||||
t.Assert(s.Size(), 4)
|
||||
t.AssertIN(s.Pop(), []int{1, 2, 3, 4})
|
||||
t.Assert(s.Size(), 3)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSet_Pops(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s := gset.New(true)
|
||||
s.Add(1).Add(2).Add(3).Add(4)
|
||||
gtest.Assert(s.Size(), 4)
|
||||
gtest.Assert(s.Pops(0), nil)
|
||||
gtest.AssertIN(s.Pops(1), []int{1, 2, 3, 4})
|
||||
gtest.Assert(s.Size(), 3)
|
||||
t.Assert(s.Size(), 4)
|
||||
t.Assert(s.Pops(0), nil)
|
||||
t.AssertIN(s.Pops(1), []int{1, 2, 3, 4})
|
||||
t.Assert(s.Size(), 3)
|
||||
a := s.Pops(6)
|
||||
gtest.Assert(len(a), 3)
|
||||
gtest.AssertIN(a, []int{1, 2, 3, 4})
|
||||
gtest.Assert(s.Size(), 0)
|
||||
t.Assert(len(a), 3)
|
||||
t.AssertIN(a, []int{1, 2, 3, 4})
|
||||
t.Assert(s.Size(), 0)
|
||||
})
|
||||
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s := gset.New(true)
|
||||
a := []interface{}{1, 2, 3, 4}
|
||||
s.Add(a...)
|
||||
gtest.Assert(s.Size(), 4)
|
||||
gtest.Assert(s.Pops(-2), nil)
|
||||
gtest.AssertIN(s.Pops(-1), a)
|
||||
t.Assert(s.Size(), 4)
|
||||
t.Assert(s.Pops(-2), nil)
|
||||
t.AssertIN(s.Pops(-1), a)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSet_Json(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s1 := []interface{}{"a", "b", "d", "c"}
|
||||
a1 := gset.NewFrom(s1)
|
||||
b1, err1 := json.Marshal(a1)
|
||||
b2, err2 := json.Marshal(s1)
|
||||
gtest.Assert(len(b1), len(b2))
|
||||
gtest.Assert(err1, err2)
|
||||
t.Assert(len(b1), len(b2))
|
||||
t.Assert(err1, err2)
|
||||
|
||||
a2 := gset.New()
|
||||
err2 = json.Unmarshal(b2, &a2)
|
||||
gtest.Assert(err2, nil)
|
||||
gtest.Assert(a2.Contains("a"), true)
|
||||
gtest.Assert(a2.Contains("b"), true)
|
||||
gtest.Assert(a2.Contains("c"), true)
|
||||
gtest.Assert(a2.Contains("d"), true)
|
||||
gtest.Assert(a2.Contains("e"), false)
|
||||
t.Assert(err2, nil)
|
||||
t.Assert(a2.Contains("a"), true)
|
||||
t.Assert(a2.Contains("b"), true)
|
||||
t.Assert(a2.Contains("c"), true)
|
||||
t.Assert(a2.Contains("d"), true)
|
||||
t.Assert(a2.Contains("e"), false)
|
||||
|
||||
var a3 gset.Set
|
||||
err := json.Unmarshal(b2, &a3)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(a3.Contains("a"), true)
|
||||
gtest.Assert(a3.Contains("b"), true)
|
||||
gtest.Assert(a3.Contains("c"), true)
|
||||
gtest.Assert(a3.Contains("d"), true)
|
||||
gtest.Assert(a3.Contains("e"), false)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(a3.Contains("a"), true)
|
||||
t.Assert(a3.Contains("b"), true)
|
||||
t.Assert(a3.Contains("c"), true)
|
||||
t.Assert(a3.Contains("d"), true)
|
||||
t.Assert(a3.Contains("e"), false)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSet_AddIfNotExistFunc(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s := gset.New(true)
|
||||
s.Add(1)
|
||||
gtest.Assert(s.Contains(1), true)
|
||||
gtest.Assert(s.Contains(2), false)
|
||||
t.Assert(s.Contains(1), true)
|
||||
t.Assert(s.Contains(2), false)
|
||||
|
||||
s.AddIfNotExistFunc(2, func() interface{} {
|
||||
return 3
|
||||
})
|
||||
gtest.Assert(s.Contains(2), false)
|
||||
gtest.Assert(s.Contains(3), true)
|
||||
t.Assert(s.Contains(2), false)
|
||||
t.Assert(s.Contains(3), true)
|
||||
|
||||
s.AddIfNotExistFunc(3, func() interface{} {
|
||||
return 4
|
||||
})
|
||||
gtest.Assert(s.Contains(3), true)
|
||||
gtest.Assert(s.Contains(4), false)
|
||||
t.Assert(s.Contains(3), true)
|
||||
t.Assert(s.Contains(4), false)
|
||||
})
|
||||
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s := gset.New(true)
|
||||
s.Add(1)
|
||||
gtest.Assert(s.Contains(1), true)
|
||||
gtest.Assert(s.Contains(2), false)
|
||||
t.Assert(s.Contains(1), true)
|
||||
t.Assert(s.Contains(2), false)
|
||||
|
||||
s.AddIfNotExistFuncLock(2, func() interface{} {
|
||||
return 3
|
||||
})
|
||||
gtest.Assert(s.Contains(2), false)
|
||||
gtest.Assert(s.Contains(3), true)
|
||||
t.Assert(s.Contains(2), false)
|
||||
t.Assert(s.Contains(3), true)
|
||||
|
||||
s.AddIfNotExistFuncLock(3, func() interface{} {
|
||||
return 4
|
||||
})
|
||||
gtest.Assert(s.Contains(3), true)
|
||||
gtest.Assert(s.Contains(4), false)
|
||||
t.Assert(s.Contains(3), true)
|
||||
t.Assert(s.Contains(4), false)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSet_UnmarshalValue(t *testing.T) {
|
||||
type T struct {
|
||||
type V struct {
|
||||
Name string
|
||||
Set *gset.Set
|
||||
}
|
||||
// JSON
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var v *V
|
||||
err := gconv.Struct(g.Map{
|
||||
"name": "john",
|
||||
"set": []byte(`["k1","k2","k3"]`),
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Set.Size(), 3)
|
||||
gtest.Assert(t.Set.Contains("k1"), true)
|
||||
gtest.Assert(t.Set.Contains("k2"), true)
|
||||
gtest.Assert(t.Set.Contains("k3"), true)
|
||||
gtest.Assert(t.Set.Contains("k4"), false)
|
||||
}, &v)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(v.Name, "john")
|
||||
t.Assert(v.Set.Size(), 3)
|
||||
t.Assert(v.Set.Contains("k1"), true)
|
||||
t.Assert(v.Set.Contains("k2"), true)
|
||||
t.Assert(v.Set.Contains("k3"), true)
|
||||
t.Assert(v.Set.Contains("k4"), false)
|
||||
})
|
||||
// Map
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var v *V
|
||||
err := gconv.Struct(g.Map{
|
||||
"name": "john",
|
||||
"set": g.Slice{"k1", "k2", "k3"},
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Set.Size(), 3)
|
||||
gtest.Assert(t.Set.Contains("k1"), true)
|
||||
gtest.Assert(t.Set.Contains("k2"), true)
|
||||
gtest.Assert(t.Set.Contains("k3"), true)
|
||||
gtest.Assert(t.Set.Contains("k4"), false)
|
||||
}, &v)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(v.Name, "john")
|
||||
t.Assert(v.Set.Size(), 3)
|
||||
t.Assert(v.Set.Contains("k1"), true)
|
||||
t.Assert(v.Set.Contains("k2"), true)
|
||||
t.Assert(v.Set.Contains("k3"), true)
|
||||
t.Assert(v.Set.Contains("k4"), false)
|
||||
})
|
||||
}
|
||||
|
||||
@ -21,30 +21,30 @@ import (
|
||||
)
|
||||
|
||||
func TestIntSet_Basic(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s := gset.NewIntSet()
|
||||
s.Add(1).Add(1).Add(2)
|
||||
s.Add([]int{3, 4}...)
|
||||
gtest.Assert(s.Size(), 4)
|
||||
gtest.AssertIN(1, s.Slice())
|
||||
gtest.AssertIN(2, s.Slice())
|
||||
gtest.AssertIN(3, s.Slice())
|
||||
gtest.AssertIN(4, s.Slice())
|
||||
gtest.AssertNI(0, s.Slice())
|
||||
gtest.Assert(s.Contains(4), true)
|
||||
gtest.Assert(s.Contains(5), false)
|
||||
t.Assert(s.Size(), 4)
|
||||
t.AssertIN(1, s.Slice())
|
||||
t.AssertIN(2, s.Slice())
|
||||
t.AssertIN(3, s.Slice())
|
||||
t.AssertIN(4, s.Slice())
|
||||
t.AssertNI(0, s.Slice())
|
||||
t.Assert(s.Contains(4), true)
|
||||
t.Assert(s.Contains(5), false)
|
||||
s.Remove(1)
|
||||
gtest.Assert(s.Size(), 3)
|
||||
t.Assert(s.Size(), 3)
|
||||
s.Clear()
|
||||
gtest.Assert(s.Size(), 0)
|
||||
t.Assert(s.Size(), 0)
|
||||
})
|
||||
}
|
||||
|
||||
func TestIntSet_Iterator(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s := gset.NewIntSet()
|
||||
s.Add(1).Add(2).Add(3)
|
||||
gtest.Assert(s.Size(), 3)
|
||||
t.Assert(s.Size(), 3)
|
||||
|
||||
a1 := garray.New(true)
|
||||
a2 := garray.New(true)
|
||||
@ -56,22 +56,22 @@ func TestIntSet_Iterator(t *testing.T) {
|
||||
a2.Append(1)
|
||||
return true
|
||||
})
|
||||
gtest.Assert(a1.Len(), 1)
|
||||
gtest.Assert(a2.Len(), 3)
|
||||
t.Assert(a1.Len(), 1)
|
||||
t.Assert(a2.Len(), 3)
|
||||
})
|
||||
}
|
||||
|
||||
func TestIntSet_LockFunc(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s := gset.NewIntSet()
|
||||
s.Add(1).Add(2).Add(3)
|
||||
gtest.Assert(s.Size(), 3)
|
||||
t.Assert(s.Size(), 3)
|
||||
s.LockFunc(func(m map[int]struct{}) {
|
||||
delete(m, 1)
|
||||
})
|
||||
gtest.Assert(s.Size(), 2)
|
||||
t.Assert(s.Size(), 2)
|
||||
s.RLockFunc(func(m map[int]struct{}) {
|
||||
gtest.Assert(m, map[int]struct{}{
|
||||
t.Assert(m, map[int]struct{}{
|
||||
3: struct{}{},
|
||||
2: struct{}{},
|
||||
})
|
||||
@ -80,286 +80,286 @@ func TestIntSet_LockFunc(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestIntSet_Equal(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s1 := gset.NewIntSet()
|
||||
s2 := gset.NewIntSet()
|
||||
s3 := gset.NewIntSet()
|
||||
s1.Add(1).Add(2).Add(3)
|
||||
s2.Add(1).Add(2).Add(3)
|
||||
s3.Add(1).Add(2).Add(3).Add(4)
|
||||
gtest.Assert(s1.Equal(s2), true)
|
||||
gtest.Assert(s1.Equal(s3), false)
|
||||
t.Assert(s1.Equal(s2), true)
|
||||
t.Assert(s1.Equal(s3), false)
|
||||
})
|
||||
}
|
||||
|
||||
func TestIntSet_IsSubsetOf(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s1 := gset.NewIntSet()
|
||||
s2 := gset.NewIntSet()
|
||||
s3 := gset.NewIntSet()
|
||||
s1.Add(1).Add(2)
|
||||
s2.Add(1).Add(2).Add(3)
|
||||
s3.Add(1).Add(2).Add(3).Add(4)
|
||||
gtest.Assert(s1.IsSubsetOf(s2), true)
|
||||
gtest.Assert(s2.IsSubsetOf(s3), true)
|
||||
gtest.Assert(s1.IsSubsetOf(s3), true)
|
||||
gtest.Assert(s2.IsSubsetOf(s1), false)
|
||||
gtest.Assert(s3.IsSubsetOf(s2), false)
|
||||
t.Assert(s1.IsSubsetOf(s2), true)
|
||||
t.Assert(s2.IsSubsetOf(s3), true)
|
||||
t.Assert(s1.IsSubsetOf(s3), true)
|
||||
t.Assert(s2.IsSubsetOf(s1), false)
|
||||
t.Assert(s3.IsSubsetOf(s2), false)
|
||||
})
|
||||
}
|
||||
|
||||
func TestIntSet_Union(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s1 := gset.NewIntSet()
|
||||
s2 := gset.NewIntSet()
|
||||
s1.Add(1).Add(2)
|
||||
s2.Add(3).Add(4)
|
||||
s3 := s1.Union(s2)
|
||||
gtest.Assert(s3.Contains(1), true)
|
||||
gtest.Assert(s3.Contains(2), true)
|
||||
gtest.Assert(s3.Contains(3), true)
|
||||
gtest.Assert(s3.Contains(4), true)
|
||||
t.Assert(s3.Contains(1), true)
|
||||
t.Assert(s3.Contains(2), true)
|
||||
t.Assert(s3.Contains(3), true)
|
||||
t.Assert(s3.Contains(4), true)
|
||||
})
|
||||
}
|
||||
|
||||
func TestIntSet_Diff(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s1 := gset.NewIntSet()
|
||||
s2 := gset.NewIntSet()
|
||||
s1.Add(1).Add(2).Add(3)
|
||||
s2.Add(3).Add(4).Add(5)
|
||||
s3 := s1.Diff(s2)
|
||||
gtest.Assert(s3.Contains(1), true)
|
||||
gtest.Assert(s3.Contains(2), true)
|
||||
gtest.Assert(s3.Contains(3), false)
|
||||
gtest.Assert(s3.Contains(4), false)
|
||||
t.Assert(s3.Contains(1), true)
|
||||
t.Assert(s3.Contains(2), true)
|
||||
t.Assert(s3.Contains(3), false)
|
||||
t.Assert(s3.Contains(4), false)
|
||||
})
|
||||
}
|
||||
|
||||
func TestIntSet_Intersect(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s1 := gset.NewIntSet()
|
||||
s2 := gset.NewIntSet()
|
||||
s1.Add(1).Add(2).Add(3)
|
||||
s2.Add(3).Add(4).Add(5)
|
||||
s3 := s1.Intersect(s2)
|
||||
gtest.Assert(s3.Contains(1), false)
|
||||
gtest.Assert(s3.Contains(2), false)
|
||||
gtest.Assert(s3.Contains(3), true)
|
||||
gtest.Assert(s3.Contains(4), false)
|
||||
t.Assert(s3.Contains(1), false)
|
||||
t.Assert(s3.Contains(2), false)
|
||||
t.Assert(s3.Contains(3), true)
|
||||
t.Assert(s3.Contains(4), false)
|
||||
})
|
||||
}
|
||||
|
||||
func TestIntSet_Complement(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s1 := gset.NewIntSet()
|
||||
s2 := gset.NewIntSet()
|
||||
s1.Add(1).Add(2).Add(3)
|
||||
s2.Add(3).Add(4).Add(5)
|
||||
s3 := s1.Complement(s2)
|
||||
gtest.Assert(s3.Contains(1), false)
|
||||
gtest.Assert(s3.Contains(2), false)
|
||||
gtest.Assert(s3.Contains(4), true)
|
||||
gtest.Assert(s3.Contains(5), true)
|
||||
t.Assert(s3.Contains(1), false)
|
||||
t.Assert(s3.Contains(2), false)
|
||||
t.Assert(s3.Contains(4), true)
|
||||
t.Assert(s3.Contains(5), true)
|
||||
})
|
||||
}
|
||||
|
||||
func TestIntSet_Size(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s1 := gset.NewIntSet(true)
|
||||
s1.Add(1).Add(2).Add(3)
|
||||
gtest.Assert(s1.Size(), 3)
|
||||
t.Assert(s1.Size(), 3)
|
||||
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
func TestIntSet_Merge(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s1 := gset.NewIntSet()
|
||||
s2 := gset.NewIntSet()
|
||||
s1.Add(1).Add(2).Add(3)
|
||||
s2.Add(3).Add(4).Add(5)
|
||||
s3 := s1.Merge(s2)
|
||||
gtest.Assert(s3.Contains(1), true)
|
||||
gtest.Assert(s3.Contains(5), true)
|
||||
gtest.Assert(s3.Contains(6), false)
|
||||
t.Assert(s3.Contains(1), true)
|
||||
t.Assert(s3.Contains(5), true)
|
||||
t.Assert(s3.Contains(6), false)
|
||||
})
|
||||
}
|
||||
|
||||
func TestIntSet_Join(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s1 := gset.NewIntSet()
|
||||
s1.Add(1).Add(2).Add(3)
|
||||
s3 := s1.Join(",")
|
||||
gtest.Assert(strings.Contains(s3, "1"), true)
|
||||
gtest.Assert(strings.Contains(s3, "2"), true)
|
||||
gtest.Assert(strings.Contains(s3, "3"), true)
|
||||
t.Assert(strings.Contains(s3, "1"), true)
|
||||
t.Assert(strings.Contains(s3, "2"), true)
|
||||
t.Assert(strings.Contains(s3, "3"), true)
|
||||
})
|
||||
}
|
||||
|
||||
func TestIntSet_String(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s1 := gset.NewIntSet()
|
||||
s1.Add(1).Add(2).Add(3)
|
||||
s3 := s1.String()
|
||||
gtest.Assert(strings.Contains(s3, "["), true)
|
||||
gtest.Assert(strings.Contains(s3, "]"), true)
|
||||
gtest.Assert(strings.Contains(s3, "1"), true)
|
||||
gtest.Assert(strings.Contains(s3, "2"), true)
|
||||
gtest.Assert(strings.Contains(s3, "3"), true)
|
||||
t.Assert(strings.Contains(s3, "["), true)
|
||||
t.Assert(strings.Contains(s3, "]"), true)
|
||||
t.Assert(strings.Contains(s3, "1"), true)
|
||||
t.Assert(strings.Contains(s3, "2"), true)
|
||||
t.Assert(strings.Contains(s3, "3"), true)
|
||||
})
|
||||
}
|
||||
|
||||
func TestIntSet_Sum(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s1 := gset.NewIntSet()
|
||||
s1.Add(1).Add(2).Add(3)
|
||||
s2 := gset.NewIntSet()
|
||||
s2.Add(5).Add(6).Add(7)
|
||||
gtest.Assert(s2.Sum(), 18)
|
||||
t.Assert(s2.Sum(), 18)
|
||||
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
func TestIntSet_Pop(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s := gset.NewIntSet()
|
||||
s.Add(4).Add(2).Add(3)
|
||||
gtest.Assert(s.Size(), 3)
|
||||
gtest.AssertIN(s.Pop(), []int{4, 2, 3})
|
||||
gtest.AssertIN(s.Pop(), []int{4, 2, 3})
|
||||
gtest.Assert(s.Size(), 1)
|
||||
t.Assert(s.Size(), 3)
|
||||
t.AssertIN(s.Pop(), []int{4, 2, 3})
|
||||
t.AssertIN(s.Pop(), []int{4, 2, 3})
|
||||
t.Assert(s.Size(), 1)
|
||||
})
|
||||
}
|
||||
|
||||
func TestIntSet_Pops(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s := gset.NewIntSet()
|
||||
s.Add(1).Add(4).Add(2).Add(3)
|
||||
gtest.Assert(s.Size(), 4)
|
||||
gtest.Assert(s.Pops(0), nil)
|
||||
gtest.AssertIN(s.Pops(1), []int{1, 4, 2, 3})
|
||||
gtest.Assert(s.Size(), 3)
|
||||
t.Assert(s.Size(), 4)
|
||||
t.Assert(s.Pops(0), nil)
|
||||
t.AssertIN(s.Pops(1), []int{1, 4, 2, 3})
|
||||
t.Assert(s.Size(), 3)
|
||||
a := s.Pops(2)
|
||||
gtest.Assert(len(a), 2)
|
||||
gtest.AssertIN(a, []int{1, 4, 2, 3})
|
||||
gtest.Assert(s.Size(), 1)
|
||||
t.Assert(len(a), 2)
|
||||
t.AssertIN(a, []int{1, 4, 2, 3})
|
||||
t.Assert(s.Size(), 1)
|
||||
})
|
||||
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s := gset.NewIntSet(true)
|
||||
a := []int{1, 2, 3, 4}
|
||||
s.Add(a...)
|
||||
gtest.Assert(s.Size(), 4)
|
||||
gtest.Assert(s.Pops(-2), nil)
|
||||
gtest.AssertIN(s.Pops(-1), a)
|
||||
t.Assert(s.Size(), 4)
|
||||
t.Assert(s.Pops(-2), nil)
|
||||
t.AssertIN(s.Pops(-1), a)
|
||||
})
|
||||
}
|
||||
|
||||
func TestIntSet_Json(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s1 := []int{1, 3, 2, 4}
|
||||
a1 := gset.NewIntSetFrom(s1)
|
||||
b1, err1 := json.Marshal(a1)
|
||||
b2, err2 := json.Marshal(s1)
|
||||
gtest.Assert(len(b1), len(b2))
|
||||
gtest.Assert(err1, err2)
|
||||
t.Assert(len(b1), len(b2))
|
||||
t.Assert(err1, err2)
|
||||
|
||||
a2 := gset.NewIntSet()
|
||||
err2 = json.Unmarshal(b2, &a2)
|
||||
gtest.Assert(err2, nil)
|
||||
gtest.Assert(a2.Contains(1), true)
|
||||
gtest.Assert(a2.Contains(2), true)
|
||||
gtest.Assert(a2.Contains(3), true)
|
||||
gtest.Assert(a2.Contains(4), true)
|
||||
gtest.Assert(a2.Contains(5), false)
|
||||
t.Assert(err2, nil)
|
||||
t.Assert(a2.Contains(1), true)
|
||||
t.Assert(a2.Contains(2), true)
|
||||
t.Assert(a2.Contains(3), true)
|
||||
t.Assert(a2.Contains(4), true)
|
||||
t.Assert(a2.Contains(5), false)
|
||||
|
||||
var a3 gset.IntSet
|
||||
err := json.Unmarshal(b2, &a3)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(a2.Contains(1), true)
|
||||
gtest.Assert(a2.Contains(2), true)
|
||||
gtest.Assert(a2.Contains(3), true)
|
||||
gtest.Assert(a2.Contains(4), true)
|
||||
gtest.Assert(a2.Contains(5), false)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(a2.Contains(1), true)
|
||||
t.Assert(a2.Contains(2), true)
|
||||
t.Assert(a2.Contains(3), true)
|
||||
t.Assert(a2.Contains(4), true)
|
||||
t.Assert(a2.Contains(5), false)
|
||||
})
|
||||
}
|
||||
|
||||
func TestIntSet_AddIfNotExistFunc(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s := gset.NewIntSet(true)
|
||||
s.Add(1)
|
||||
gtest.Assert(s.Contains(1), true)
|
||||
gtest.Assert(s.Contains(2), false)
|
||||
t.Assert(s.Contains(1), true)
|
||||
t.Assert(s.Contains(2), false)
|
||||
|
||||
s.AddIfNotExistFunc(2, func() int {
|
||||
return 3
|
||||
})
|
||||
gtest.Assert(s.Contains(2), false)
|
||||
gtest.Assert(s.Contains(3), true)
|
||||
t.Assert(s.Contains(2), false)
|
||||
t.Assert(s.Contains(3), true)
|
||||
|
||||
s.AddIfNotExistFunc(3, func() int {
|
||||
return 4
|
||||
})
|
||||
gtest.Assert(s.Contains(3), true)
|
||||
gtest.Assert(s.Contains(4), false)
|
||||
t.Assert(s.Contains(3), true)
|
||||
t.Assert(s.Contains(4), false)
|
||||
})
|
||||
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s := gset.NewIntSet(true)
|
||||
s.Add(1)
|
||||
gtest.Assert(s.Contains(1), true)
|
||||
gtest.Assert(s.Contains(2), false)
|
||||
t.Assert(s.Contains(1), true)
|
||||
t.Assert(s.Contains(2), false)
|
||||
|
||||
s.AddIfNotExistFuncLock(2, func() int {
|
||||
return 3
|
||||
})
|
||||
gtest.Assert(s.Contains(2), false)
|
||||
gtest.Assert(s.Contains(3), true)
|
||||
t.Assert(s.Contains(2), false)
|
||||
t.Assert(s.Contains(3), true)
|
||||
|
||||
s.AddIfNotExistFuncLock(3, func() int {
|
||||
return 4
|
||||
})
|
||||
gtest.Assert(s.Contains(3), true)
|
||||
gtest.Assert(s.Contains(4), false)
|
||||
t.Assert(s.Contains(3), true)
|
||||
t.Assert(s.Contains(4), false)
|
||||
})
|
||||
}
|
||||
|
||||
func TestIntSet_UnmarshalValue(t *testing.T) {
|
||||
type T struct {
|
||||
type V struct {
|
||||
Name string
|
||||
Set *gset.IntSet
|
||||
}
|
||||
// JSON
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var v *V
|
||||
err := gconv.Struct(g.Map{
|
||||
"name": "john",
|
||||
"set": []byte(`[1,2,3]`),
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Set.Size(), 3)
|
||||
gtest.Assert(t.Set.Contains(1), true)
|
||||
gtest.Assert(t.Set.Contains(2), true)
|
||||
gtest.Assert(t.Set.Contains(3), true)
|
||||
gtest.Assert(t.Set.Contains(4), false)
|
||||
}, &v)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(v.Name, "john")
|
||||
t.Assert(v.Set.Size(), 3)
|
||||
t.Assert(v.Set.Contains(1), true)
|
||||
t.Assert(v.Set.Contains(2), true)
|
||||
t.Assert(v.Set.Contains(3), true)
|
||||
t.Assert(v.Set.Contains(4), false)
|
||||
})
|
||||
// Map
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var v *V
|
||||
err := gconv.Struct(g.Map{
|
||||
"name": "john",
|
||||
"set": g.Slice{1, 2, 3},
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Set.Size(), 3)
|
||||
gtest.Assert(t.Set.Contains(1), true)
|
||||
gtest.Assert(t.Set.Contains(2), true)
|
||||
gtest.Assert(t.Set.Contains(3), true)
|
||||
gtest.Assert(t.Set.Contains(4), false)
|
||||
}, &v)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(v.Name, "john")
|
||||
t.Assert(v.Set.Size(), 3)
|
||||
t.Assert(v.Set.Contains(1), true)
|
||||
t.Assert(v.Set.Contains(2), true)
|
||||
t.Assert(v.Set.Contains(3), true)
|
||||
t.Assert(v.Set.Contains(4), false)
|
||||
})
|
||||
}
|
||||
|
||||
@ -21,30 +21,30 @@ import (
|
||||
)
|
||||
|
||||
func TestStrSet_Basic(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s := gset.NewStrSet()
|
||||
s.Add("1").Add("1").Add("2")
|
||||
s.Add([]string{"3", "4"}...)
|
||||
gtest.Assert(s.Size(), 4)
|
||||
gtest.AssertIN("1", s.Slice())
|
||||
gtest.AssertIN("2", s.Slice())
|
||||
gtest.AssertIN("3", s.Slice())
|
||||
gtest.AssertIN("4", s.Slice())
|
||||
gtest.AssertNI("0", s.Slice())
|
||||
gtest.Assert(s.Contains("4"), true)
|
||||
gtest.Assert(s.Contains("5"), false)
|
||||
t.Assert(s.Size(), 4)
|
||||
t.AssertIN("1", s.Slice())
|
||||
t.AssertIN("2", s.Slice())
|
||||
t.AssertIN("3", s.Slice())
|
||||
t.AssertIN("4", s.Slice())
|
||||
t.AssertNI("0", s.Slice())
|
||||
t.Assert(s.Contains("4"), true)
|
||||
t.Assert(s.Contains("5"), false)
|
||||
s.Remove("1")
|
||||
gtest.Assert(s.Size(), 3)
|
||||
t.Assert(s.Size(), 3)
|
||||
s.Clear()
|
||||
gtest.Assert(s.Size(), 0)
|
||||
t.Assert(s.Size(), 0)
|
||||
})
|
||||
}
|
||||
|
||||
func TestStrSet_Iterator(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s := gset.NewStrSet()
|
||||
s.Add("1").Add("2").Add("3")
|
||||
gtest.Assert(s.Size(), 3)
|
||||
t.Assert(s.Size(), 3)
|
||||
|
||||
a1 := garray.New(true)
|
||||
a2 := garray.New(true)
|
||||
@ -56,22 +56,22 @@ func TestStrSet_Iterator(t *testing.T) {
|
||||
a2.Append("1")
|
||||
return true
|
||||
})
|
||||
gtest.Assert(a1.Len(), 1)
|
||||
gtest.Assert(a2.Len(), 3)
|
||||
t.Assert(a1.Len(), 1)
|
||||
t.Assert(a2.Len(), 3)
|
||||
})
|
||||
}
|
||||
|
||||
func TestStrSet_LockFunc(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s := gset.NewStrSet()
|
||||
s.Add("1").Add("2").Add("3")
|
||||
gtest.Assert(s.Size(), 3)
|
||||
t.Assert(s.Size(), 3)
|
||||
s.LockFunc(func(m map[string]struct{}) {
|
||||
delete(m, "1")
|
||||
})
|
||||
gtest.Assert(s.Size(), 2)
|
||||
t.Assert(s.Size(), 2)
|
||||
s.RLockFunc(func(m map[string]struct{}) {
|
||||
gtest.Assert(m, map[string]struct{}{
|
||||
t.Assert(m, map[string]struct{}{
|
||||
"3": struct{}{},
|
||||
"2": struct{}{},
|
||||
})
|
||||
@ -80,322 +80,322 @@ func TestStrSet_LockFunc(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestStrSet_Equal(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s1 := gset.NewStrSet()
|
||||
s2 := gset.NewStrSet()
|
||||
s3 := gset.NewStrSet()
|
||||
s1.Add("1").Add("2").Add("3")
|
||||
s2.Add("1").Add("2").Add("3")
|
||||
s3.Add("1").Add("2").Add("3").Add("4")
|
||||
gtest.Assert(s1.Equal(s2), true)
|
||||
gtest.Assert(s1.Equal(s3), false)
|
||||
t.Assert(s1.Equal(s2), true)
|
||||
t.Assert(s1.Equal(s3), false)
|
||||
})
|
||||
}
|
||||
|
||||
func TestStrSet_IsSubsetOf(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s1 := gset.NewStrSet()
|
||||
s2 := gset.NewStrSet()
|
||||
s3 := gset.NewStrSet()
|
||||
s1.Add("1").Add("2")
|
||||
s2.Add("1").Add("2").Add("3")
|
||||
s3.Add("1").Add("2").Add("3").Add("4")
|
||||
gtest.Assert(s1.IsSubsetOf(s2), true)
|
||||
gtest.Assert(s2.IsSubsetOf(s3), true)
|
||||
gtest.Assert(s1.IsSubsetOf(s3), true)
|
||||
gtest.Assert(s2.IsSubsetOf(s1), false)
|
||||
gtest.Assert(s3.IsSubsetOf(s2), false)
|
||||
t.Assert(s1.IsSubsetOf(s2), true)
|
||||
t.Assert(s2.IsSubsetOf(s3), true)
|
||||
t.Assert(s1.IsSubsetOf(s3), true)
|
||||
t.Assert(s2.IsSubsetOf(s1), false)
|
||||
t.Assert(s3.IsSubsetOf(s2), false)
|
||||
})
|
||||
}
|
||||
|
||||
func TestStrSet_Union(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s1 := gset.NewStrSet()
|
||||
s2 := gset.NewStrSet()
|
||||
s1.Add("1").Add("2")
|
||||
s2.Add("3").Add("4")
|
||||
s3 := s1.Union(s2)
|
||||
gtest.Assert(s3.Contains("1"), true)
|
||||
gtest.Assert(s3.Contains("2"), true)
|
||||
gtest.Assert(s3.Contains("3"), true)
|
||||
gtest.Assert(s3.Contains("4"), true)
|
||||
t.Assert(s3.Contains("1"), true)
|
||||
t.Assert(s3.Contains("2"), true)
|
||||
t.Assert(s3.Contains("3"), true)
|
||||
t.Assert(s3.Contains("4"), true)
|
||||
})
|
||||
}
|
||||
|
||||
func TestStrSet_Diff(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s1 := gset.NewStrSet()
|
||||
s2 := gset.NewStrSet()
|
||||
s1.Add("1").Add("2").Add("3")
|
||||
s2.Add("3").Add("4").Add("5")
|
||||
s3 := s1.Diff(s2)
|
||||
gtest.Assert(s3.Contains("1"), true)
|
||||
gtest.Assert(s3.Contains("2"), true)
|
||||
gtest.Assert(s3.Contains("3"), false)
|
||||
gtest.Assert(s3.Contains("4"), false)
|
||||
t.Assert(s3.Contains("1"), true)
|
||||
t.Assert(s3.Contains("2"), true)
|
||||
t.Assert(s3.Contains("3"), false)
|
||||
t.Assert(s3.Contains("4"), false)
|
||||
})
|
||||
}
|
||||
|
||||
func TestStrSet_Intersect(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s1 := gset.NewStrSet()
|
||||
s2 := gset.NewStrSet()
|
||||
s1.Add("1").Add("2").Add("3")
|
||||
s2.Add("3").Add("4").Add("5")
|
||||
s3 := s1.Intersect(s2)
|
||||
gtest.Assert(s3.Contains("1"), false)
|
||||
gtest.Assert(s3.Contains("2"), false)
|
||||
gtest.Assert(s3.Contains("3"), true)
|
||||
gtest.Assert(s3.Contains("4"), false)
|
||||
t.Assert(s3.Contains("1"), false)
|
||||
t.Assert(s3.Contains("2"), false)
|
||||
t.Assert(s3.Contains("3"), true)
|
||||
t.Assert(s3.Contains("4"), false)
|
||||
})
|
||||
}
|
||||
|
||||
func TestStrSet_Complement(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s1 := gset.NewStrSet()
|
||||
s2 := gset.NewStrSet()
|
||||
s1.Add("1").Add("2").Add("3")
|
||||
s2.Add("3").Add("4").Add("5")
|
||||
s3 := s1.Complement(s2)
|
||||
gtest.Assert(s3.Contains("1"), false)
|
||||
gtest.Assert(s3.Contains("2"), false)
|
||||
gtest.Assert(s3.Contains("4"), true)
|
||||
gtest.Assert(s3.Contains("5"), true)
|
||||
t.Assert(s3.Contains("1"), false)
|
||||
t.Assert(s3.Contains("2"), false)
|
||||
t.Assert(s3.Contains("4"), true)
|
||||
t.Assert(s3.Contains("5"), true)
|
||||
})
|
||||
}
|
||||
|
||||
func TestNewIntSetFrom(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s1 := gset.NewIntSetFrom([]int{1, 2, 3, 4})
|
||||
s2 := gset.NewIntSetFrom([]int{5, 6, 7, 8})
|
||||
gtest.Assert(s1.Contains(3), true)
|
||||
gtest.Assert(s1.Contains(5), false)
|
||||
gtest.Assert(s2.Contains(3), false)
|
||||
gtest.Assert(s2.Contains(5), true)
|
||||
t.Assert(s1.Contains(3), true)
|
||||
t.Assert(s1.Contains(5), false)
|
||||
t.Assert(s2.Contains(3), false)
|
||||
t.Assert(s2.Contains(5), true)
|
||||
})
|
||||
}
|
||||
|
||||
func TestStrSet_Merge(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s1 := gset.NewStrSet()
|
||||
s2 := gset.NewStrSet()
|
||||
s1.Add("1").Add("2").Add("3")
|
||||
s2.Add("3").Add("4").Add("5")
|
||||
s3 := s1.Merge(s2)
|
||||
gtest.Assert(s3.Contains("1"), true)
|
||||
gtest.Assert(s3.Contains("6"), false)
|
||||
gtest.Assert(s3.Contains("4"), true)
|
||||
gtest.Assert(s3.Contains("5"), true)
|
||||
t.Assert(s3.Contains("1"), true)
|
||||
t.Assert(s3.Contains("6"), false)
|
||||
t.Assert(s3.Contains("4"), true)
|
||||
t.Assert(s3.Contains("5"), true)
|
||||
})
|
||||
}
|
||||
|
||||
func TestNewStrSetFrom(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s1 := gset.NewStrSetFrom([]string{"a", "b", "c"}, true)
|
||||
gtest.Assert(s1.Contains("b"), true)
|
||||
gtest.Assert(s1.Contains("d"), false)
|
||||
t.Assert(s1.Contains("b"), true)
|
||||
t.Assert(s1.Contains("d"), false)
|
||||
})
|
||||
}
|
||||
|
||||
func TestStrSet_Join(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s1 := gset.NewStrSetFrom([]string{"a", "b", "c"}, true)
|
||||
str1 := s1.Join(",")
|
||||
gtest.Assert(strings.Contains(str1, "b"), true)
|
||||
gtest.Assert(strings.Contains(str1, "d"), false)
|
||||
t.Assert(strings.Contains(str1, "b"), true)
|
||||
t.Assert(strings.Contains(str1, "d"), false)
|
||||
})
|
||||
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s1 := gset.NewStrSet()
|
||||
s1.Add("a").Add(`"b"`).Add(`\c`)
|
||||
str1 := s1.Join(",")
|
||||
gtest.Assert(strings.Contains(str1, `"b"`), true)
|
||||
gtest.Assert(strings.Contains(str1, `\c`), true)
|
||||
gtest.Assert(strings.Contains(str1, `a`), true)
|
||||
t.Assert(strings.Contains(str1, `"b"`), true)
|
||||
t.Assert(strings.Contains(str1, `\c`), true)
|
||||
t.Assert(strings.Contains(str1, `a`), true)
|
||||
})
|
||||
}
|
||||
|
||||
func TestStrSet_String(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s1 := gset.NewStrSetFrom([]string{"a", "b", "c"}, true)
|
||||
str1 := s1.String()
|
||||
gtest.Assert(strings.Contains(str1, "b"), true)
|
||||
gtest.Assert(strings.Contains(str1, "d"), false)
|
||||
t.Assert(strings.Contains(str1, "b"), true)
|
||||
t.Assert(strings.Contains(str1, "d"), false)
|
||||
})
|
||||
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s1 := gset.New(true)
|
||||
s1.Add("a").Add("a2").Add("b").Add("c")
|
||||
str1 := s1.String()
|
||||
gtest.Assert(strings.Contains(str1, "["), true)
|
||||
gtest.Assert(strings.Contains(str1, "]"), true)
|
||||
gtest.Assert(strings.Contains(str1, "a2"), true)
|
||||
t.Assert(strings.Contains(str1, "["), true)
|
||||
t.Assert(strings.Contains(str1, "]"), true)
|
||||
t.Assert(strings.Contains(str1, "a2"), true)
|
||||
})
|
||||
}
|
||||
|
||||
func TestStrSet_Sum(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s1 := gset.NewStrSetFrom([]string{"a", "b", "c"}, true)
|
||||
s2 := gset.NewIntSetFrom([]int{2, 3, 4}, true)
|
||||
gtest.Assert(s1.Sum(), 0)
|
||||
gtest.Assert(s2.Sum(), 9)
|
||||
t.Assert(s1.Sum(), 0)
|
||||
t.Assert(s2.Sum(), 9)
|
||||
})
|
||||
}
|
||||
|
||||
func TestStrSet_Size(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s1 := gset.NewStrSetFrom([]string{"a", "b", "c"}, true)
|
||||
gtest.Assert(s1.Size(), 3)
|
||||
t.Assert(s1.Size(), 3)
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
func TestStrSet_Remove(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s1 := gset.NewStrSetFrom([]string{"a", "b", "c"}, true)
|
||||
s1 = s1.Remove("b")
|
||||
gtest.Assert(s1.Contains("b"), false)
|
||||
gtest.Assert(s1.Contains("c"), true)
|
||||
t.Assert(s1.Contains("b"), false)
|
||||
t.Assert(s1.Contains("c"), true)
|
||||
})
|
||||
}
|
||||
|
||||
func TestStrSet_Pop(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a := []string{"a", "b", "c", "d"}
|
||||
s := gset.NewStrSetFrom(a, true)
|
||||
gtest.Assert(s.Size(), 4)
|
||||
gtest.AssertIN(s.Pop(), a)
|
||||
gtest.Assert(s.Size(), 3)
|
||||
gtest.AssertIN(s.Pop(), a)
|
||||
gtest.Assert(s.Size(), 2)
|
||||
t.Assert(s.Size(), 4)
|
||||
t.AssertIN(s.Pop(), a)
|
||||
t.Assert(s.Size(), 3)
|
||||
t.AssertIN(s.Pop(), a)
|
||||
t.Assert(s.Size(), 2)
|
||||
})
|
||||
}
|
||||
|
||||
func TestStrSet_Pops(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a := []string{"a", "b", "c", "d"}
|
||||
s := gset.NewStrSetFrom(a, true)
|
||||
array := s.Pops(2)
|
||||
gtest.Assert(len(array), 2)
|
||||
gtest.Assert(s.Size(), 2)
|
||||
gtest.AssertIN(array, a)
|
||||
gtest.Assert(s.Pops(0), nil)
|
||||
gtest.AssertIN(s.Pops(2), a)
|
||||
gtest.Assert(s.Size(), 0)
|
||||
t.Assert(len(array), 2)
|
||||
t.Assert(s.Size(), 2)
|
||||
t.AssertIN(array, a)
|
||||
t.Assert(s.Pops(0), nil)
|
||||
t.AssertIN(s.Pops(2), a)
|
||||
t.Assert(s.Size(), 0)
|
||||
})
|
||||
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s := gset.NewStrSet(true)
|
||||
a := []string{"1", "2", "3", "4"}
|
||||
s.Add(a...)
|
||||
gtest.Assert(s.Size(), 4)
|
||||
gtest.Assert(s.Pops(-2), nil)
|
||||
gtest.AssertIN(s.Pops(-1), a)
|
||||
t.Assert(s.Size(), 4)
|
||||
t.Assert(s.Pops(-2), nil)
|
||||
t.AssertIN(s.Pops(-1), a)
|
||||
})
|
||||
}
|
||||
|
||||
func TestStrSet_Json(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s1 := []string{"a", "b", "d", "c"}
|
||||
a1 := gset.NewStrSetFrom(s1)
|
||||
b1, err1 := json.Marshal(a1)
|
||||
b2, err2 := json.Marshal(s1)
|
||||
gtest.Assert(len(b1), len(b2))
|
||||
gtest.Assert(err1, err2)
|
||||
t.Assert(len(b1), len(b2))
|
||||
t.Assert(err1, err2)
|
||||
|
||||
a2 := gset.NewStrSet()
|
||||
err2 = json.Unmarshal(b2, &a2)
|
||||
gtest.Assert(err2, nil)
|
||||
gtest.Assert(a2.Contains("a"), true)
|
||||
gtest.Assert(a2.Contains("b"), true)
|
||||
gtest.Assert(a2.Contains("c"), true)
|
||||
gtest.Assert(a2.Contains("d"), true)
|
||||
gtest.Assert(a2.Contains("e"), false)
|
||||
t.Assert(err2, nil)
|
||||
t.Assert(a2.Contains("a"), true)
|
||||
t.Assert(a2.Contains("b"), true)
|
||||
t.Assert(a2.Contains("c"), true)
|
||||
t.Assert(a2.Contains("d"), true)
|
||||
t.Assert(a2.Contains("e"), false)
|
||||
|
||||
var a3 gset.StrSet
|
||||
err := json.Unmarshal(b2, &a3)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(a3.Contains("a"), true)
|
||||
gtest.Assert(a3.Contains("b"), true)
|
||||
gtest.Assert(a3.Contains("c"), true)
|
||||
gtest.Assert(a3.Contains("d"), true)
|
||||
gtest.Assert(a3.Contains("e"), false)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(a3.Contains("a"), true)
|
||||
t.Assert(a3.Contains("b"), true)
|
||||
t.Assert(a3.Contains("c"), true)
|
||||
t.Assert(a3.Contains("d"), true)
|
||||
t.Assert(a3.Contains("e"), false)
|
||||
})
|
||||
}
|
||||
|
||||
func TestStrSet_AddIfNotExistFunc(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s := gset.NewStrSet(true)
|
||||
s.Add("1")
|
||||
gtest.Assert(s.Contains("1"), true)
|
||||
gtest.Assert(s.Contains("2"), false)
|
||||
t.Assert(s.Contains("1"), true)
|
||||
t.Assert(s.Contains("2"), false)
|
||||
|
||||
s.AddIfNotExistFunc("2", func() string {
|
||||
return "3"
|
||||
})
|
||||
gtest.Assert(s.Contains("2"), false)
|
||||
gtest.Assert(s.Contains("3"), true)
|
||||
t.Assert(s.Contains("2"), false)
|
||||
t.Assert(s.Contains("3"), true)
|
||||
|
||||
s.AddIfNotExistFunc("3", func() string {
|
||||
return "4"
|
||||
})
|
||||
gtest.Assert(s.Contains("3"), true)
|
||||
gtest.Assert(s.Contains("4"), false)
|
||||
t.Assert(s.Contains("3"), true)
|
||||
t.Assert(s.Contains("4"), false)
|
||||
})
|
||||
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s := gset.NewStrSet(true)
|
||||
s.Add("1")
|
||||
gtest.Assert(s.Contains("1"), true)
|
||||
gtest.Assert(s.Contains("2"), false)
|
||||
t.Assert(s.Contains("1"), true)
|
||||
t.Assert(s.Contains("2"), false)
|
||||
|
||||
s.AddIfNotExistFuncLock("2", func() string {
|
||||
return "3"
|
||||
})
|
||||
gtest.Assert(s.Contains("2"), false)
|
||||
gtest.Assert(s.Contains("3"), true)
|
||||
t.Assert(s.Contains("2"), false)
|
||||
t.Assert(s.Contains("3"), true)
|
||||
|
||||
s.AddIfNotExistFuncLock("3", func() string {
|
||||
return "4"
|
||||
})
|
||||
gtest.Assert(s.Contains("3"), true)
|
||||
gtest.Assert(s.Contains("4"), false)
|
||||
t.Assert(s.Contains("3"), true)
|
||||
t.Assert(s.Contains("4"), false)
|
||||
})
|
||||
}
|
||||
|
||||
func TestStrSet_UnmarshalValue(t *testing.T) {
|
||||
type T struct {
|
||||
type V struct {
|
||||
Name string
|
||||
Set *gset.StrSet
|
||||
}
|
||||
// JSON
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var v *V
|
||||
err := gconv.Struct(g.Map{
|
||||
"name": "john",
|
||||
"set": []byte(`["1","2","3"]`),
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Set.Size(), 3)
|
||||
gtest.Assert(t.Set.Contains("1"), true)
|
||||
gtest.Assert(t.Set.Contains("2"), true)
|
||||
gtest.Assert(t.Set.Contains("3"), true)
|
||||
gtest.Assert(t.Set.Contains("4"), false)
|
||||
}, &v)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(v.Name, "john")
|
||||
t.Assert(v.Set.Size(), 3)
|
||||
t.Assert(v.Set.Contains("1"), true)
|
||||
t.Assert(v.Set.Contains("2"), true)
|
||||
t.Assert(v.Set.Contains("3"), true)
|
||||
t.Assert(v.Set.Contains("4"), false)
|
||||
})
|
||||
// Map
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var v *V
|
||||
err := gconv.Struct(g.Map{
|
||||
"name": "john",
|
||||
"set": g.SliceStr{"1", "2", "3"},
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Set.Size(), 3)
|
||||
gtest.Assert(t.Set.Contains("1"), true)
|
||||
gtest.Assert(t.Set.Contains("2"), true)
|
||||
gtest.Assert(t.Set.Contains("3"), true)
|
||||
gtest.Assert(t.Set.Contains("4"), false)
|
||||
}, &v)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(v.Name, "john")
|
||||
t.Assert(v.Set.Size(), 3)
|
||||
t.Assert(v.Set.Contains("1"), true)
|
||||
t.Assert(v.Set.Contains("2"), true)
|
||||
t.Assert(v.Set.Contains("3"), true)
|
||||
t.Assert(v.Set.Contains("4"), false)
|
||||
})
|
||||
}
|
||||
|
||||
@ -17,96 +17,99 @@ import (
|
||||
)
|
||||
|
||||
func Test_AVLTree_Basic(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gtree.NewAVLTree(gutil.ComparatorString)
|
||||
m.Set("key1", "val1")
|
||||
gtest.Assert(m.Keys(), []interface{}{"key1"})
|
||||
t.Assert(m.Keys(), []interface{}{"key1"})
|
||||
|
||||
gtest.Assert(m.Get("key1"), "val1")
|
||||
gtest.Assert(m.Size(), 1)
|
||||
gtest.Assert(m.IsEmpty(), false)
|
||||
t.Assert(m.Get("key1"), "val1")
|
||||
t.Assert(m.Size(), 1)
|
||||
t.Assert(m.IsEmpty(), false)
|
||||
|
||||
gtest.Assert(m.GetOrSet("key2", "val2"), "val2")
|
||||
gtest.Assert(m.GetOrSet("key2", "val2"), "val2")
|
||||
gtest.Assert(m.SetIfNotExist("key2", "val2"), false)
|
||||
t.Assert(m.GetOrSet("key2", "val2"), "val2")
|
||||
t.Assert(m.GetOrSet("key2", "val2"), "val2")
|
||||
t.Assert(m.SetIfNotExist("key2", "val2"), false)
|
||||
|
||||
gtest.Assert(m.SetIfNotExist("key3", "val3"), true)
|
||||
t.Assert(m.SetIfNotExist("key3", "val3"), true)
|
||||
|
||||
gtest.Assert(m.Remove("key2"), "val2")
|
||||
gtest.Assert(m.Contains("key2"), false)
|
||||
t.Assert(m.Remove("key2"), "val2")
|
||||
t.Assert(m.Contains("key2"), false)
|
||||
|
||||
gtest.AssertIN("key3", m.Keys())
|
||||
gtest.AssertIN("key1", m.Keys())
|
||||
gtest.AssertIN("val3", m.Values())
|
||||
gtest.AssertIN("val1", m.Values())
|
||||
t.AssertIN("key3", m.Keys())
|
||||
t.AssertIN("key1", m.Keys())
|
||||
t.AssertIN("val3", m.Values())
|
||||
t.AssertIN("val1", m.Values())
|
||||
|
||||
m.Sets(map[interface{}]interface{}{"key3": "val3", "key1": "val1"})
|
||||
|
||||
m.Flip()
|
||||
gtest.Assert(m.Map(), map[interface{}]interface{}{"val3": "key3", "val1": "key1"})
|
||||
t.Assert(m.Map(), map[interface{}]interface{}{"val3": "key3", "val1": "key1"})
|
||||
|
||||
m.Flip(gutil.ComparatorString)
|
||||
gtest.Assert(m.Map(), map[interface{}]interface{}{"key3": "val3", "key1": "val1"})
|
||||
t.Assert(m.Map(), map[interface{}]interface{}{"key3": "val3", "key1": "val1"})
|
||||
|
||||
m.Clear()
|
||||
gtest.Assert(m.Size(), 0)
|
||||
gtest.Assert(m.IsEmpty(), true)
|
||||
t.Assert(m.Size(), 0)
|
||||
t.Assert(m.IsEmpty(), true)
|
||||
|
||||
m2 := gtree.NewAVLTreeFrom(gutil.ComparatorString, map[interface{}]interface{}{1: 1, "key1": "val1"})
|
||||
gtest.Assert(m2.Map(), map[interface{}]interface{}{1: 1, "key1": "val1"})
|
||||
t.Assert(m2.Map(), map[interface{}]interface{}{1: 1, "key1": "val1"})
|
||||
})
|
||||
}
|
||||
func Test_AVLTree_Set_Fun(t *testing.T) {
|
||||
//GetOrSetFunc lock or unlock
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gtree.NewAVLTree(gutil.ComparatorString)
|
||||
gtest.Assert(m.GetOrSetFunc("fun", getValue), 3)
|
||||
gtest.Assert(m.GetOrSetFunc("fun", getValue), 3)
|
||||
gtest.Assert(m.GetOrSetFuncLock("funlock", getValue), 3)
|
||||
gtest.Assert(m.GetOrSetFuncLock("funlock", getValue), 3)
|
||||
gtest.Assert(m.Get("funlock"), 3)
|
||||
gtest.Assert(m.Get("fun"), 3)
|
||||
t.Assert(m.GetOrSetFunc("fun", getValue), 3)
|
||||
t.Assert(m.GetOrSetFunc("fun", getValue), 3)
|
||||
t.Assert(m.GetOrSetFuncLock("funlock", getValue), 3)
|
||||
t.Assert(m.GetOrSetFuncLock("funlock", getValue), 3)
|
||||
t.Assert(m.Get("funlock"), 3)
|
||||
t.Assert(m.Get("fun"), 3)
|
||||
})
|
||||
//SetIfNotExistFunc lock or unlock
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gtree.NewAVLTree(gutil.ComparatorString)
|
||||
gtest.Assert(m.SetIfNotExistFunc("fun", getValue), true)
|
||||
gtest.Assert(m.SetIfNotExistFunc("fun", getValue), false)
|
||||
gtest.Assert(m.SetIfNotExistFuncLock("funlock", getValue), true)
|
||||
gtest.Assert(m.SetIfNotExistFuncLock("funlock", getValue), false)
|
||||
gtest.Assert(m.Get("funlock"), 3)
|
||||
gtest.Assert(m.Get("fun"), 3)
|
||||
t.Assert(m.SetIfNotExistFunc("fun", getValue), true)
|
||||
t.Assert(m.SetIfNotExistFunc("fun", getValue), false)
|
||||
t.Assert(m.SetIfNotExistFuncLock("funlock", getValue), true)
|
||||
t.Assert(m.SetIfNotExistFuncLock("funlock", getValue), false)
|
||||
t.Assert(m.Get("funlock"), 3)
|
||||
t.Assert(m.Get("fun"), 3)
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
func Test_AVLTree_Get_Set_Var(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gtree.NewAVLTree(gutil.ComparatorString)
|
||||
gtest.AssertEQ(m.SetIfNotExist("key1", "val1"), true)
|
||||
gtest.AssertEQ(m.SetIfNotExist("key1", "val1"), false)
|
||||
gtest.AssertEQ(m.GetVarOrSet("key1", "val1"), gvar.New("val1", true))
|
||||
gtest.AssertEQ(m.GetVar("key1"), gvar.New("val1", true))
|
||||
t.AssertEQ(m.SetIfNotExist("key1", "val1"), true)
|
||||
t.AssertEQ(m.SetIfNotExist("key1", "val1"), false)
|
||||
t.AssertEQ(m.GetVarOrSet("key1", "val1"), gvar.New("val1", true))
|
||||
t.AssertEQ(m.GetVar("key1"), gvar.New("val1", true))
|
||||
})
|
||||
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gtree.NewAVLTree(gutil.ComparatorString)
|
||||
gtest.AssertEQ(m.GetVarOrSetFunc("fun", getValue), gvar.New(3, true))
|
||||
gtest.AssertEQ(m.GetVarOrSetFunc("fun", getValue), gvar.New(3, true))
|
||||
gtest.AssertEQ(m.GetVarOrSetFuncLock("funlock", getValue), gvar.New(3, true))
|
||||
gtest.AssertEQ(m.GetVarOrSetFuncLock("funlock", getValue), gvar.New(3, true))
|
||||
t.AssertEQ(m.GetVarOrSetFunc("fun", getValue), gvar.New(3, true))
|
||||
t.AssertEQ(m.GetVarOrSetFunc("fun", getValue), gvar.New(3, true))
|
||||
t.AssertEQ(m.GetVarOrSetFuncLock("funlock", getValue), gvar.New(3, true))
|
||||
t.AssertEQ(m.GetVarOrSetFuncLock("funlock", getValue), gvar.New(3, true))
|
||||
})
|
||||
}
|
||||
|
||||
func Test_AVLTree_Batch(t *testing.T) {
|
||||
m := gtree.NewAVLTree(gutil.ComparatorString)
|
||||
m.Sets(map[interface{}]interface{}{1: 1, "key1": "val1", "key2": "val2", "key3": "val3"})
|
||||
gtest.Assert(m.Map(), map[interface{}]interface{}{1: 1, "key1": "val1", "key2": "val2", "key3": "val3"})
|
||||
m.Removes([]interface{}{"key1", 1})
|
||||
gtest.Assert(m.Map(), map[interface{}]interface{}{"key2": "val2", "key3": "val3"})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gtree.NewAVLTree(gutil.ComparatorString)
|
||||
m.Sets(map[interface{}]interface{}{1: 1, "key1": "val1", "key2": "val2", "key3": "val3"})
|
||||
t.Assert(m.Map(), map[interface{}]interface{}{1: 1, "key1": "val1", "key2": "val2", "key3": "val3"})
|
||||
m.Removes([]interface{}{"key1", 1})
|
||||
t.Assert(m.Map(), map[interface{}]interface{}{"key2": "val2", "key3": "val3"})
|
||||
})
|
||||
}
|
||||
|
||||
func Test_AVLTree_Iterator(t *testing.T) {
|
||||
|
||||
keys := []string{"1", "key1", "key2", "key3", "key4"}
|
||||
keyLen := len(keys)
|
||||
index := 0
|
||||
@ -114,23 +117,26 @@ func Test_AVLTree_Iterator(t *testing.T) {
|
||||
expect := map[interface{}]interface{}{"key4": "val4", 1: 1, "key1": "val1", "key2": "val2", "key3": "val3"}
|
||||
|
||||
m := gtree.NewAVLTreeFrom(gutil.ComparatorString, expect)
|
||||
m.Iterator(func(k interface{}, v interface{}) bool {
|
||||
gtest.Assert(k, keys[index])
|
||||
index++
|
||||
gtest.Assert(expect[k], v)
|
||||
return true
|
||||
})
|
||||
|
||||
m.IteratorDesc(func(k interface{}, v interface{}) bool {
|
||||
index--
|
||||
gtest.Assert(k, keys[index])
|
||||
gtest.Assert(expect[k], v)
|
||||
return true
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m.Iterator(func(k interface{}, v interface{}) bool {
|
||||
t.Assert(k, keys[index])
|
||||
index++
|
||||
t.Assert(expect[k], v)
|
||||
return true
|
||||
})
|
||||
|
||||
m.IteratorDesc(func(k interface{}, v interface{}) bool {
|
||||
index--
|
||||
t.Assert(k, keys[index])
|
||||
t.Assert(expect[k], v)
|
||||
return true
|
||||
})
|
||||
})
|
||||
|
||||
m.Print()
|
||||
// 断言返回值对遍历控制
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
i := 0
|
||||
j := 0
|
||||
m.Iterator(func(k interface{}, v interface{}) bool {
|
||||
@ -141,11 +147,11 @@ func Test_AVLTree_Iterator(t *testing.T) {
|
||||
j++
|
||||
return false
|
||||
})
|
||||
gtest.Assert(i, keyLen)
|
||||
gtest.Assert(j, 1)
|
||||
t.Assert(i, keyLen)
|
||||
t.Assert(j, 1)
|
||||
})
|
||||
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
i := 0
|
||||
j := 0
|
||||
m.IteratorDesc(func(k interface{}, v interface{}) bool {
|
||||
@ -156,8 +162,8 @@ func Test_AVLTree_Iterator(t *testing.T) {
|
||||
j++
|
||||
return false
|
||||
})
|
||||
gtest.Assert(i, keyLen)
|
||||
gtest.Assert(j, 1)
|
||||
t.Assert(i, keyLen)
|
||||
t.Assert(j, 1)
|
||||
})
|
||||
|
||||
}
|
||||
@ -169,27 +175,27 @@ func Test_AVLTree_IteratorFrom(t *testing.T) {
|
||||
}
|
||||
tree := gtree.NewAVLTreeFrom(gutil.ComparatorInt, m)
|
||||
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
n := 5
|
||||
tree.IteratorFrom(5, true, func(key, value interface{}) bool {
|
||||
gtest.Assert(n, key)
|
||||
gtest.Assert(n*10, value)
|
||||
t.Assert(n, key)
|
||||
t.Assert(n*10, value)
|
||||
n++
|
||||
return true
|
||||
})
|
||||
|
||||
i := 5
|
||||
tree.IteratorAscFrom(5, true, func(key, value interface{}) bool {
|
||||
gtest.Assert(i, key)
|
||||
gtest.Assert(i*10, value)
|
||||
t.Assert(i, key)
|
||||
t.Assert(i*10, value)
|
||||
i++
|
||||
return true
|
||||
})
|
||||
|
||||
j := 5
|
||||
tree.IteratorDescFrom(5, true, func(key, value interface{}) bool {
|
||||
gtest.Assert(j, key)
|
||||
gtest.Assert(j*10, value)
|
||||
t.Assert(j, key)
|
||||
t.Assert(j*10, value)
|
||||
j--
|
||||
return true
|
||||
})
|
||||
@ -197,31 +203,33 @@ func Test_AVLTree_IteratorFrom(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_AVLTree_Clone(t *testing.T) {
|
||||
//clone 方法是深克隆
|
||||
m := gtree.NewAVLTreeFrom(gutil.ComparatorString, map[interface{}]interface{}{1: 1, "key1": "val1"})
|
||||
m_clone := m.Clone()
|
||||
m.Remove(1)
|
||||
//修改原 map,clone 后的 map 不影响
|
||||
gtest.AssertIN(1, m_clone.Keys())
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
//clone 方法是深克隆
|
||||
m := gtree.NewAVLTreeFrom(gutil.ComparatorString, map[interface{}]interface{}{1: 1, "key1": "val1"})
|
||||
m_clone := m.Clone()
|
||||
m.Remove(1)
|
||||
//修改原 map,clone 后的 map 不影响
|
||||
t.AssertIN(1, m_clone.Keys())
|
||||
|
||||
m_clone.Remove("key1")
|
||||
//修改clone map,原 map 不影响
|
||||
gtest.AssertIN("key1", m.Keys())
|
||||
m_clone.Remove("key1")
|
||||
//修改clone map,原 map 不影响
|
||||
t.AssertIN("key1", m.Keys())
|
||||
})
|
||||
}
|
||||
|
||||
func Test_AVLTree_LRNode(t *testing.T) {
|
||||
expect := map[interface{}]interface{}{"key4": "val4", "key1": "val1", "key2": "val2", "key3": "val3"}
|
||||
//safe
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gtree.NewAVLTreeFrom(gutil.ComparatorString, expect)
|
||||
gtest.Assert(m.Left().Key, "key1")
|
||||
gtest.Assert(m.Right().Key, "key4")
|
||||
t.Assert(m.Left().Key, "key1")
|
||||
t.Assert(m.Right().Key, "key4")
|
||||
})
|
||||
//unsafe
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gtree.NewAVLTreeFrom(gutil.ComparatorString, expect, true)
|
||||
gtest.Assert(m.Left().Key, "key1")
|
||||
gtest.Assert(m.Right().Key, "key4")
|
||||
t.Assert(m.Left().Key, "key1")
|
||||
t.Assert(m.Right().Key, "key4")
|
||||
})
|
||||
}
|
||||
|
||||
@ -237,34 +245,34 @@ func Test_AVLTree_CeilingFloor(t *testing.T) {
|
||||
8: "val8",
|
||||
4: "val4"}
|
||||
//found and eq
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gtree.NewAVLTreeFrom(gutil.ComparatorInt, expect)
|
||||
c, cf := m.Ceiling(8)
|
||||
gtest.Assert(cf, true)
|
||||
gtest.Assert(c.Value, "val8")
|
||||
t.Assert(cf, true)
|
||||
t.Assert(c.Value, "val8")
|
||||
f, ff := m.Floor(20)
|
||||
gtest.Assert(ff, true)
|
||||
gtest.Assert(f.Value, "val20")
|
||||
t.Assert(ff, true)
|
||||
t.Assert(f.Value, "val20")
|
||||
})
|
||||
//found and neq
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gtree.NewAVLTreeFrom(gutil.ComparatorInt, expect)
|
||||
c, cf := m.Ceiling(9)
|
||||
gtest.Assert(cf, true)
|
||||
gtest.Assert(c.Value, "val10")
|
||||
t.Assert(cf, true)
|
||||
t.Assert(c.Value, "val10")
|
||||
f, ff := m.Floor(5)
|
||||
gtest.Assert(ff, true)
|
||||
gtest.Assert(f.Value, "val4")
|
||||
t.Assert(ff, true)
|
||||
t.Assert(f.Value, "val4")
|
||||
})
|
||||
//nofound
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gtree.NewAVLTreeFrom(gutil.ComparatorInt, expect)
|
||||
c, cf := m.Ceiling(21)
|
||||
gtest.Assert(cf, false)
|
||||
gtest.Assert(c, nil)
|
||||
t.Assert(cf, false)
|
||||
t.Assert(c, nil)
|
||||
f, ff := m.Floor(-1)
|
||||
gtest.Assert(ff, false)
|
||||
gtest.Assert(f, nil)
|
||||
t.Assert(ff, false)
|
||||
t.Assert(f, nil)
|
||||
})
|
||||
}
|
||||
|
||||
@ -274,11 +282,11 @@ func Test_AVLTree_Remove(t *testing.T) {
|
||||
m.Set(i, fmt.Sprintf("val%d", i))
|
||||
}
|
||||
expect := m.Map()
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
for k, v := range expect {
|
||||
m1 := m.Clone()
|
||||
gtest.Assert(m1.Remove(k), v)
|
||||
gtest.Assert(m1.Remove(k), nil)
|
||||
t.Assert(m1.Remove(k), v)
|
||||
t.Assert(m1.Remove(k), nil)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@ -17,88 +17,90 @@ import (
|
||||
)
|
||||
|
||||
func Test_BTree_Basic(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gtree.NewBTree(3, gutil.ComparatorString)
|
||||
m.Set("key1", "val1")
|
||||
|
||||
gtest.Assert(m.Height(), 1)
|
||||
t.Assert(m.Height(), 1)
|
||||
|
||||
gtest.Assert(m.Keys(), []interface{}{"key1"})
|
||||
t.Assert(m.Keys(), []interface{}{"key1"})
|
||||
|
||||
gtest.Assert(m.Get("key1"), "val1")
|
||||
gtest.Assert(m.Size(), 1)
|
||||
gtest.Assert(m.IsEmpty(), false)
|
||||
t.Assert(m.Get("key1"), "val1")
|
||||
t.Assert(m.Size(), 1)
|
||||
t.Assert(m.IsEmpty(), false)
|
||||
|
||||
gtest.Assert(m.GetOrSet("key2", "val2"), "val2")
|
||||
gtest.Assert(m.SetIfNotExist("key2", "val2"), false)
|
||||
t.Assert(m.GetOrSet("key2", "val2"), "val2")
|
||||
t.Assert(m.SetIfNotExist("key2", "val2"), false)
|
||||
|
||||
gtest.Assert(m.SetIfNotExist("key3", "val3"), true)
|
||||
t.Assert(m.SetIfNotExist("key3", "val3"), true)
|
||||
|
||||
gtest.Assert(m.Remove("key2"), "val2")
|
||||
gtest.Assert(m.Contains("key2"), false)
|
||||
t.Assert(m.Remove("key2"), "val2")
|
||||
t.Assert(m.Contains("key2"), false)
|
||||
|
||||
gtest.AssertIN("key3", m.Keys())
|
||||
gtest.AssertIN("key1", m.Keys())
|
||||
gtest.AssertIN("val3", m.Values())
|
||||
gtest.AssertIN("val1", m.Values())
|
||||
t.AssertIN("key3", m.Keys())
|
||||
t.AssertIN("key1", m.Keys())
|
||||
t.AssertIN("val3", m.Values())
|
||||
t.AssertIN("val1", m.Values())
|
||||
|
||||
m.Clear()
|
||||
gtest.Assert(m.Size(), 0)
|
||||
gtest.Assert(m.IsEmpty(), true)
|
||||
t.Assert(m.Size(), 0)
|
||||
t.Assert(m.IsEmpty(), true)
|
||||
|
||||
m2 := gtree.NewBTreeFrom(3, gutil.ComparatorString, map[interface{}]interface{}{1: 1, "key1": "val1"})
|
||||
gtest.Assert(m2.Map(), map[interface{}]interface{}{1: 1, "key1": "val1"})
|
||||
t.Assert(m2.Map(), map[interface{}]interface{}{1: 1, "key1": "val1"})
|
||||
})
|
||||
}
|
||||
|
||||
func Test_BTree_Set_Fun(t *testing.T) {
|
||||
//GetOrSetFunc lock or unlock
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gtree.NewBTree(3, gutil.ComparatorString)
|
||||
gtest.Assert(m.GetOrSetFunc("fun", getValue), 3)
|
||||
gtest.Assert(m.GetOrSetFunc("fun", getValue), 3)
|
||||
gtest.Assert(m.GetOrSetFuncLock("funlock", getValue), 3)
|
||||
gtest.Assert(m.GetOrSetFuncLock("funlock", getValue), 3)
|
||||
gtest.Assert(m.Get("funlock"), 3)
|
||||
gtest.Assert(m.Get("fun"), 3)
|
||||
t.Assert(m.GetOrSetFunc("fun", getValue), 3)
|
||||
t.Assert(m.GetOrSetFunc("fun", getValue), 3)
|
||||
t.Assert(m.GetOrSetFuncLock("funlock", getValue), 3)
|
||||
t.Assert(m.GetOrSetFuncLock("funlock", getValue), 3)
|
||||
t.Assert(m.Get("funlock"), 3)
|
||||
t.Assert(m.Get("fun"), 3)
|
||||
})
|
||||
//SetIfNotExistFunc lock or unlock
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gtree.NewBTree(3, gutil.ComparatorString)
|
||||
gtest.Assert(m.SetIfNotExistFunc("fun", getValue), true)
|
||||
gtest.Assert(m.SetIfNotExistFunc("fun", getValue), false)
|
||||
gtest.Assert(m.SetIfNotExistFuncLock("funlock", getValue), true)
|
||||
gtest.Assert(m.SetIfNotExistFuncLock("funlock", getValue), false)
|
||||
gtest.Assert(m.Get("funlock"), 3)
|
||||
gtest.Assert(m.Get("fun"), 3)
|
||||
t.Assert(m.SetIfNotExistFunc("fun", getValue), true)
|
||||
t.Assert(m.SetIfNotExistFunc("fun", getValue), false)
|
||||
t.Assert(m.SetIfNotExistFuncLock("funlock", getValue), true)
|
||||
t.Assert(m.SetIfNotExistFuncLock("funlock", getValue), false)
|
||||
t.Assert(m.Get("funlock"), 3)
|
||||
t.Assert(m.Get("fun"), 3)
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
func Test_BTree_Get_Set_Var(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gtree.NewBTree(3, gutil.ComparatorString)
|
||||
gtest.AssertEQ(m.SetIfNotExist("key1", "val1"), true)
|
||||
gtest.AssertEQ(m.SetIfNotExist("key1", "val1"), false)
|
||||
gtest.AssertEQ(m.GetVarOrSet("key1", "val1"), gvar.New("val1", true))
|
||||
gtest.AssertEQ(m.GetVar("key1"), gvar.New("val1", true))
|
||||
t.AssertEQ(m.SetIfNotExist("key1", "val1"), true)
|
||||
t.AssertEQ(m.SetIfNotExist("key1", "val1"), false)
|
||||
t.AssertEQ(m.GetVarOrSet("key1", "val1"), gvar.New("val1", true))
|
||||
t.AssertEQ(m.GetVar("key1"), gvar.New("val1", true))
|
||||
})
|
||||
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gtree.NewBTree(3, gutil.ComparatorString)
|
||||
gtest.AssertEQ(m.GetVarOrSetFunc("fun", getValue), gvar.New(3, true))
|
||||
gtest.AssertEQ(m.GetVarOrSetFunc("fun", getValue), gvar.New(3, true))
|
||||
gtest.AssertEQ(m.GetVarOrSetFuncLock("funlock", getValue), gvar.New(3, true))
|
||||
gtest.AssertEQ(m.GetVarOrSetFuncLock("funlock", getValue), gvar.New(3, true))
|
||||
t.AssertEQ(m.GetVarOrSetFunc("fun", getValue), gvar.New(3, true))
|
||||
t.AssertEQ(m.GetVarOrSetFunc("fun", getValue), gvar.New(3, true))
|
||||
t.AssertEQ(m.GetVarOrSetFuncLock("funlock", getValue), gvar.New(3, true))
|
||||
t.AssertEQ(m.GetVarOrSetFuncLock("funlock", getValue), gvar.New(3, true))
|
||||
})
|
||||
}
|
||||
|
||||
func Test_BTree_Batch(t *testing.T) {
|
||||
m := gtree.NewBTree(3, gutil.ComparatorString)
|
||||
m.Sets(map[interface{}]interface{}{1: 1, "key1": "val1", "key2": "val2", "key3": "val3"})
|
||||
gtest.Assert(m.Map(), map[interface{}]interface{}{1: 1, "key1": "val1", "key2": "val2", "key3": "val3"})
|
||||
m.Removes([]interface{}{"key1", 1})
|
||||
gtest.Assert(m.Map(), map[interface{}]interface{}{"key2": "val2", "key3": "val3"})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gtree.NewBTree(3, gutil.ComparatorString)
|
||||
m.Sets(map[interface{}]interface{}{1: 1, "key1": "val1", "key2": "val2", "key3": "val3"})
|
||||
t.Assert(m.Map(), map[interface{}]interface{}{1: 1, "key1": "val1", "key2": "val2", "key3": "val3"})
|
||||
m.Removes([]interface{}{"key1", 1})
|
||||
t.Assert(m.Map(), map[interface{}]interface{}{"key2": "val2", "key3": "val3"})
|
||||
})
|
||||
}
|
||||
|
||||
func Test_BTree_Iterator(t *testing.T) {
|
||||
@ -109,23 +111,26 @@ func Test_BTree_Iterator(t *testing.T) {
|
||||
expect := map[interface{}]interface{}{"key4": "val4", 1: 1, "key1": "val1", "key2": "val2", "key3": "val3"}
|
||||
|
||||
m := gtree.NewBTreeFrom(3, gutil.ComparatorString, expect)
|
||||
m.Iterator(func(k interface{}, v interface{}) bool {
|
||||
gtest.Assert(k, keys[index])
|
||||
index++
|
||||
gtest.Assert(expect[k], v)
|
||||
return true
|
||||
})
|
||||
|
||||
m.IteratorDesc(func(k interface{}, v interface{}) bool {
|
||||
index--
|
||||
gtest.Assert(k, keys[index])
|
||||
gtest.Assert(expect[k], v)
|
||||
return true
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m.Iterator(func(k interface{}, v interface{}) bool {
|
||||
t.Assert(k, keys[index])
|
||||
index++
|
||||
t.Assert(expect[k], v)
|
||||
return true
|
||||
})
|
||||
|
||||
m.IteratorDesc(func(k interface{}, v interface{}) bool {
|
||||
index--
|
||||
t.Assert(k, keys[index])
|
||||
t.Assert(expect[k], v)
|
||||
return true
|
||||
})
|
||||
})
|
||||
|
||||
m.Print()
|
||||
// 断言返回值对遍历控制
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
i := 0
|
||||
j := 0
|
||||
m.Iterator(func(k interface{}, v interface{}) bool {
|
||||
@ -136,11 +141,11 @@ func Test_BTree_Iterator(t *testing.T) {
|
||||
j++
|
||||
return false
|
||||
})
|
||||
gtest.Assert(i, keyLen)
|
||||
gtest.Assert(j, 1)
|
||||
t.Assert(i, keyLen)
|
||||
t.Assert(j, 1)
|
||||
})
|
||||
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
i := 0
|
||||
j := 0
|
||||
m.IteratorDesc(func(k interface{}, v interface{}) bool {
|
||||
@ -151,8 +156,8 @@ func Test_BTree_Iterator(t *testing.T) {
|
||||
j++
|
||||
return false
|
||||
})
|
||||
gtest.Assert(i, keyLen)
|
||||
gtest.Assert(j, 1)
|
||||
t.Assert(i, keyLen)
|
||||
t.Assert(j, 1)
|
||||
})
|
||||
}
|
||||
|
||||
@ -163,27 +168,27 @@ func Test_BTree_IteratorFrom(t *testing.T) {
|
||||
}
|
||||
tree := gtree.NewBTreeFrom(3, gutil.ComparatorInt, m)
|
||||
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
n := 5
|
||||
tree.IteratorFrom(5, true, func(key, value interface{}) bool {
|
||||
gtest.Assert(n, key)
|
||||
gtest.Assert(n*10, value)
|
||||
t.Assert(n, key)
|
||||
t.Assert(n*10, value)
|
||||
n++
|
||||
return true
|
||||
})
|
||||
|
||||
i := 5
|
||||
tree.IteratorAscFrom(5, true, func(key, value interface{}) bool {
|
||||
gtest.Assert(i, key)
|
||||
gtest.Assert(i*10, value)
|
||||
t.Assert(i, key)
|
||||
t.Assert(i*10, value)
|
||||
i++
|
||||
return true
|
||||
})
|
||||
|
||||
j := 5
|
||||
tree.IteratorDescFrom(5, true, func(key, value interface{}) bool {
|
||||
gtest.Assert(j, key)
|
||||
gtest.Assert(j*10, value)
|
||||
t.Assert(j, key)
|
||||
t.Assert(j*10, value)
|
||||
j--
|
||||
return true
|
||||
})
|
||||
@ -191,31 +196,33 @@ func Test_BTree_IteratorFrom(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_BTree_Clone(t *testing.T) {
|
||||
//clone 方法是深克隆
|
||||
m := gtree.NewBTreeFrom(3, gutil.ComparatorString, map[interface{}]interface{}{1: 1, "key1": "val1"})
|
||||
m_clone := m.Clone()
|
||||
m.Remove(1)
|
||||
//修改原 map,clone 后的 map 不影响
|
||||
gtest.AssertIN(1, m_clone.Keys())
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
//clone 方法是深克隆
|
||||
m := gtree.NewBTreeFrom(3, gutil.ComparatorString, map[interface{}]interface{}{1: 1, "key1": "val1"})
|
||||
m_clone := m.Clone()
|
||||
m.Remove(1)
|
||||
//修改原 map,clone 后的 map 不影响
|
||||
t.AssertIN(1, m_clone.Keys())
|
||||
|
||||
m_clone.Remove("key1")
|
||||
//修改clone map,原 map 不影响
|
||||
gtest.AssertIN("key1", m.Keys())
|
||||
m_clone.Remove("key1")
|
||||
//修改clone map,原 map 不影响
|
||||
t.AssertIN("key1", m.Keys())
|
||||
})
|
||||
}
|
||||
|
||||
func Test_BTree_LRNode(t *testing.T) {
|
||||
expect := map[interface{}]interface{}{"key4": "val4", "key1": "val1", "key2": "val2", "key3": "val3"}
|
||||
//safe
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gtree.NewBTreeFrom(3, gutil.ComparatorString, expect)
|
||||
gtest.Assert(m.Left().Key, "key1")
|
||||
gtest.Assert(m.Right().Key, "key4")
|
||||
t.Assert(m.Left().Key, "key1")
|
||||
t.Assert(m.Right().Key, "key4")
|
||||
})
|
||||
//unsafe
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gtree.NewBTreeFrom(3, gutil.ComparatorString, expect, true)
|
||||
gtest.Assert(m.Left().Key, "key1")
|
||||
gtest.Assert(m.Right().Key, "key4")
|
||||
t.Assert(m.Left().Key, "key1")
|
||||
t.Assert(m.Right().Key, "key4")
|
||||
})
|
||||
}
|
||||
|
||||
@ -225,11 +232,11 @@ func Test_BTree_Remove(t *testing.T) {
|
||||
m.Set(i, fmt.Sprintf("val%d", i))
|
||||
}
|
||||
expect := m.Map()
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
for k, v := range expect {
|
||||
m1 := m.Clone()
|
||||
gtest.Assert(m1.Remove(k), v)
|
||||
gtest.Assert(m1.Remove(k), nil)
|
||||
t.Assert(m1.Remove(k), v)
|
||||
t.Assert(m1.Remove(k), nil)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@ -21,94 +21,96 @@ func getValue() interface{} {
|
||||
}
|
||||
|
||||
func Test_RedBlackTree_Basic(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gtree.NewRedBlackTree(gutil.ComparatorString)
|
||||
m.Set("key1", "val1")
|
||||
gtest.Assert(m.Keys(), []interface{}{"key1"})
|
||||
t.Assert(m.Keys(), []interface{}{"key1"})
|
||||
|
||||
gtest.Assert(m.Get("key1"), "val1")
|
||||
gtest.Assert(m.Size(), 1)
|
||||
gtest.Assert(m.IsEmpty(), false)
|
||||
t.Assert(m.Get("key1"), "val1")
|
||||
t.Assert(m.Size(), 1)
|
||||
t.Assert(m.IsEmpty(), false)
|
||||
|
||||
gtest.Assert(m.GetOrSet("key2", "val2"), "val2")
|
||||
gtest.Assert(m.GetOrSet("key2", "val2"), "val2")
|
||||
gtest.Assert(m.SetIfNotExist("key2", "val2"), false)
|
||||
t.Assert(m.GetOrSet("key2", "val2"), "val2")
|
||||
t.Assert(m.GetOrSet("key2", "val2"), "val2")
|
||||
t.Assert(m.SetIfNotExist("key2", "val2"), false)
|
||||
|
||||
gtest.Assert(m.SetIfNotExist("key3", "val3"), true)
|
||||
t.Assert(m.SetIfNotExist("key3", "val3"), true)
|
||||
|
||||
gtest.Assert(m.Remove("key2"), "val2")
|
||||
gtest.Assert(m.Contains("key2"), false)
|
||||
t.Assert(m.Remove("key2"), "val2")
|
||||
t.Assert(m.Contains("key2"), false)
|
||||
|
||||
gtest.AssertIN("key3", m.Keys())
|
||||
gtest.AssertIN("key1", m.Keys())
|
||||
gtest.AssertIN("val3", m.Values())
|
||||
gtest.AssertIN("val1", m.Values())
|
||||
t.AssertIN("key3", m.Keys())
|
||||
t.AssertIN("key1", m.Keys())
|
||||
t.AssertIN("val3", m.Values())
|
||||
t.AssertIN("val1", m.Values())
|
||||
|
||||
m.Sets(map[interface{}]interface{}{"key3": "val3", "key1": "val1"})
|
||||
|
||||
m.Flip()
|
||||
gtest.Assert(m.Map(), map[interface{}]interface{}{"val3": "key3", "val1": "key1"})
|
||||
t.Assert(m.Map(), map[interface{}]interface{}{"val3": "key3", "val1": "key1"})
|
||||
|
||||
m.Flip(gutil.ComparatorString)
|
||||
gtest.Assert(m.Map(), map[interface{}]interface{}{"key3": "val3", "key1": "val1"})
|
||||
t.Assert(m.Map(), map[interface{}]interface{}{"key3": "val3", "key1": "val1"})
|
||||
|
||||
m.Clear()
|
||||
gtest.Assert(m.Size(), 0)
|
||||
gtest.Assert(m.IsEmpty(), true)
|
||||
t.Assert(m.Size(), 0)
|
||||
t.Assert(m.IsEmpty(), true)
|
||||
|
||||
m2 := gtree.NewRedBlackTreeFrom(gutil.ComparatorString, map[interface{}]interface{}{1: 1, "key1": "val1"})
|
||||
gtest.Assert(m2.Map(), map[interface{}]interface{}{1: 1, "key1": "val1"})
|
||||
t.Assert(m2.Map(), map[interface{}]interface{}{1: 1, "key1": "val1"})
|
||||
})
|
||||
}
|
||||
|
||||
func Test_RedBlackTree_Set_Fun(t *testing.T) {
|
||||
//GetOrSetFunc lock or unlock
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gtree.NewRedBlackTree(gutil.ComparatorString)
|
||||
gtest.Assert(m.GetOrSetFunc("fun", getValue), 3)
|
||||
gtest.Assert(m.GetOrSetFunc("fun", getValue), 3)
|
||||
gtest.Assert(m.GetOrSetFuncLock("funlock", getValue), 3)
|
||||
gtest.Assert(m.GetOrSetFuncLock("funlock", getValue), 3)
|
||||
gtest.Assert(m.Get("funlock"), 3)
|
||||
gtest.Assert(m.Get("fun"), 3)
|
||||
t.Assert(m.GetOrSetFunc("fun", getValue), 3)
|
||||
t.Assert(m.GetOrSetFunc("fun", getValue), 3)
|
||||
t.Assert(m.GetOrSetFuncLock("funlock", getValue), 3)
|
||||
t.Assert(m.GetOrSetFuncLock("funlock", getValue), 3)
|
||||
t.Assert(m.Get("funlock"), 3)
|
||||
t.Assert(m.Get("fun"), 3)
|
||||
})
|
||||
//SetIfNotExistFunc lock or unlock
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gtree.NewRedBlackTree(gutil.ComparatorString)
|
||||
gtest.Assert(m.SetIfNotExistFunc("fun", getValue), true)
|
||||
gtest.Assert(m.SetIfNotExistFunc("fun", getValue), false)
|
||||
gtest.Assert(m.SetIfNotExistFuncLock("funlock", getValue), true)
|
||||
gtest.Assert(m.SetIfNotExistFuncLock("funlock", getValue), false)
|
||||
gtest.Assert(m.Get("funlock"), 3)
|
||||
gtest.Assert(m.Get("fun"), 3)
|
||||
t.Assert(m.SetIfNotExistFunc("fun", getValue), true)
|
||||
t.Assert(m.SetIfNotExistFunc("fun", getValue), false)
|
||||
t.Assert(m.SetIfNotExistFuncLock("funlock", getValue), true)
|
||||
t.Assert(m.SetIfNotExistFuncLock("funlock", getValue), false)
|
||||
t.Assert(m.Get("funlock"), 3)
|
||||
t.Assert(m.Get("fun"), 3)
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
func Test_RedBlackTree_Get_Set_Var(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gtree.NewRedBlackTree(gutil.ComparatorString)
|
||||
gtest.AssertEQ(m.SetIfNotExist("key1", "val1"), true)
|
||||
gtest.AssertEQ(m.SetIfNotExist("key1", "val1"), false)
|
||||
gtest.AssertEQ(m.GetVarOrSet("key1", "val1"), gvar.New("val1", true))
|
||||
gtest.AssertEQ(m.GetVar("key1"), gvar.New("val1", true))
|
||||
t.AssertEQ(m.SetIfNotExist("key1", "val1"), true)
|
||||
t.AssertEQ(m.SetIfNotExist("key1", "val1"), false)
|
||||
t.AssertEQ(m.GetVarOrSet("key1", "val1"), gvar.New("val1", true))
|
||||
t.AssertEQ(m.GetVar("key1"), gvar.New("val1", true))
|
||||
})
|
||||
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gtree.NewRedBlackTree(gutil.ComparatorString)
|
||||
gtest.AssertEQ(m.GetVarOrSetFunc("fun", getValue), gvar.New(3, true))
|
||||
gtest.AssertEQ(m.GetVarOrSetFunc("fun", getValue), gvar.New(3, true))
|
||||
gtest.AssertEQ(m.GetVarOrSetFuncLock("funlock", getValue), gvar.New(3, true))
|
||||
gtest.AssertEQ(m.GetVarOrSetFuncLock("funlock", getValue), gvar.New(3, true))
|
||||
t.AssertEQ(m.GetVarOrSetFunc("fun", getValue), gvar.New(3, true))
|
||||
t.AssertEQ(m.GetVarOrSetFunc("fun", getValue), gvar.New(3, true))
|
||||
t.AssertEQ(m.GetVarOrSetFuncLock("funlock", getValue), gvar.New(3, true))
|
||||
t.AssertEQ(m.GetVarOrSetFuncLock("funlock", getValue), gvar.New(3, true))
|
||||
})
|
||||
}
|
||||
|
||||
func Test_RedBlackTree_Batch(t *testing.T) {
|
||||
m := gtree.NewRedBlackTree(gutil.ComparatorString)
|
||||
m.Sets(map[interface{}]interface{}{1: 1, "key1": "val1", "key2": "val2", "key3": "val3"})
|
||||
gtest.Assert(m.Map(), map[interface{}]interface{}{1: 1, "key1": "val1", "key2": "val2", "key3": "val3"})
|
||||
m.Removes([]interface{}{"key1", 1})
|
||||
gtest.Assert(m.Map(), map[interface{}]interface{}{"key2": "val2", "key3": "val3"})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gtree.NewRedBlackTree(gutil.ComparatorString)
|
||||
m.Sets(map[interface{}]interface{}{1: 1, "key1": "val1", "key2": "val2", "key3": "val3"})
|
||||
t.Assert(m.Map(), map[interface{}]interface{}{1: 1, "key1": "val1", "key2": "val2", "key3": "val3"})
|
||||
m.Removes([]interface{}{"key1", 1})
|
||||
t.Assert(m.Map(), map[interface{}]interface{}{"key2": "val2", "key3": "val3"})
|
||||
})
|
||||
}
|
||||
|
||||
func Test_RedBlackTree_Iterator(t *testing.T) {
|
||||
@ -117,25 +119,27 @@ func Test_RedBlackTree_Iterator(t *testing.T) {
|
||||
index := 0
|
||||
|
||||
expect := map[interface{}]interface{}{"key4": "val4", 1: 1, "key1": "val1", "key2": "val2", "key3": "val3"}
|
||||
|
||||
m := gtree.NewRedBlackTreeFrom(gutil.ComparatorString, expect)
|
||||
m.Iterator(func(k interface{}, v interface{}) bool {
|
||||
gtest.Assert(k, keys[index])
|
||||
index++
|
||||
gtest.Assert(expect[k], v)
|
||||
return true
|
||||
})
|
||||
|
||||
m.IteratorDesc(func(k interface{}, v interface{}) bool {
|
||||
index--
|
||||
gtest.Assert(k, keys[index])
|
||||
gtest.Assert(expect[k], v)
|
||||
return true
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
|
||||
m.Iterator(func(k interface{}, v interface{}) bool {
|
||||
t.Assert(k, keys[index])
|
||||
index++
|
||||
t.Assert(expect[k], v)
|
||||
return true
|
||||
})
|
||||
|
||||
m.IteratorDesc(func(k interface{}, v interface{}) bool {
|
||||
index--
|
||||
t.Assert(k, keys[index])
|
||||
t.Assert(expect[k], v)
|
||||
return true
|
||||
})
|
||||
})
|
||||
m.Print()
|
||||
// 断言返回值对遍历控制
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
i := 0
|
||||
j := 0
|
||||
m.Iterator(func(k interface{}, v interface{}) bool {
|
||||
@ -146,11 +150,11 @@ func Test_RedBlackTree_Iterator(t *testing.T) {
|
||||
j++
|
||||
return false
|
||||
})
|
||||
gtest.Assert(i, keyLen)
|
||||
gtest.Assert(j, 1)
|
||||
t.Assert(i, keyLen)
|
||||
t.Assert(j, 1)
|
||||
})
|
||||
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
i := 0
|
||||
j := 0
|
||||
m.IteratorDesc(func(k interface{}, v interface{}) bool {
|
||||
@ -161,8 +165,8 @@ func Test_RedBlackTree_Iterator(t *testing.T) {
|
||||
j++
|
||||
return false
|
||||
})
|
||||
gtest.Assert(i, keyLen)
|
||||
gtest.Assert(j, 1)
|
||||
t.Assert(i, keyLen)
|
||||
t.Assert(j, 1)
|
||||
})
|
||||
}
|
||||
|
||||
@ -173,27 +177,27 @@ func Test_RedBlackTree_IteratorFrom(t *testing.T) {
|
||||
}
|
||||
tree := gtree.NewRedBlackTreeFrom(gutil.ComparatorInt, m)
|
||||
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
n := 5
|
||||
tree.IteratorFrom(5, true, func(key, value interface{}) bool {
|
||||
gtest.Assert(n, key)
|
||||
gtest.Assert(n*10, value)
|
||||
t.Assert(n, key)
|
||||
t.Assert(n*10, value)
|
||||
n++
|
||||
return true
|
||||
})
|
||||
|
||||
i := 5
|
||||
tree.IteratorAscFrom(5, true, func(key, value interface{}) bool {
|
||||
gtest.Assert(i, key)
|
||||
gtest.Assert(i*10, value)
|
||||
t.Assert(i, key)
|
||||
t.Assert(i*10, value)
|
||||
i++
|
||||
return true
|
||||
})
|
||||
|
||||
j := 5
|
||||
tree.IteratorDescFrom(5, true, func(key, value interface{}) bool {
|
||||
gtest.Assert(j, key)
|
||||
gtest.Assert(j*10, value)
|
||||
t.Assert(j, key)
|
||||
t.Assert(j*10, value)
|
||||
j--
|
||||
return true
|
||||
})
|
||||
@ -201,31 +205,33 @@ func Test_RedBlackTree_IteratorFrom(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_RedBlackTree_Clone(t *testing.T) {
|
||||
//clone 方法是深克隆
|
||||
m := gtree.NewRedBlackTreeFrom(gutil.ComparatorString, map[interface{}]interface{}{1: 1, "key1": "val1"})
|
||||
m_clone := m.Clone()
|
||||
m.Remove(1)
|
||||
//修改原 map,clone 后的 map 不影响
|
||||
gtest.AssertIN(1, m_clone.Keys())
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
//clone 方法是深克隆
|
||||
m := gtree.NewRedBlackTreeFrom(gutil.ComparatorString, map[interface{}]interface{}{1: 1, "key1": "val1"})
|
||||
m_clone := m.Clone()
|
||||
m.Remove(1)
|
||||
//修改原 map,clone 后的 map 不影响
|
||||
t.AssertIN(1, m_clone.Keys())
|
||||
|
||||
m_clone.Remove("key1")
|
||||
//修改clone map,原 map 不影响
|
||||
gtest.AssertIN("key1", m.Keys())
|
||||
m_clone.Remove("key1")
|
||||
//修改clone map,原 map 不影响
|
||||
t.AssertIN("key1", m.Keys())
|
||||
})
|
||||
}
|
||||
|
||||
func Test_RedBlackTree_LRNode(t *testing.T) {
|
||||
expect := map[interface{}]interface{}{"key4": "val4", "key1": "val1", "key2": "val2", "key3": "val3"}
|
||||
//safe
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gtree.NewRedBlackTreeFrom(gutil.ComparatorString, expect)
|
||||
gtest.Assert(m.Left().Key, "key1")
|
||||
gtest.Assert(m.Right().Key, "key4")
|
||||
t.Assert(m.Left().Key, "key1")
|
||||
t.Assert(m.Right().Key, "key4")
|
||||
})
|
||||
//unsafe
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gtree.NewRedBlackTreeFrom(gutil.ComparatorString, expect, true)
|
||||
gtest.Assert(m.Left().Key, "key1")
|
||||
gtest.Assert(m.Right().Key, "key4")
|
||||
t.Assert(m.Left().Key, "key1")
|
||||
t.Assert(m.Right().Key, "key4")
|
||||
})
|
||||
}
|
||||
|
||||
@ -241,34 +247,34 @@ func Test_RedBlackTree_CeilingFloor(t *testing.T) {
|
||||
8: "val8",
|
||||
4: "val4"}
|
||||
//found and eq
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gtree.NewRedBlackTreeFrom(gutil.ComparatorInt, expect)
|
||||
c, cf := m.Ceiling(8)
|
||||
gtest.Assert(cf, true)
|
||||
gtest.Assert(c.Value, "val8")
|
||||
t.Assert(cf, true)
|
||||
t.Assert(c.Value, "val8")
|
||||
f, ff := m.Floor(20)
|
||||
gtest.Assert(ff, true)
|
||||
gtest.Assert(f.Value, "val20")
|
||||
t.Assert(ff, true)
|
||||
t.Assert(f.Value, "val20")
|
||||
})
|
||||
//found and neq
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gtree.NewRedBlackTreeFrom(gutil.ComparatorInt, expect)
|
||||
c, cf := m.Ceiling(9)
|
||||
gtest.Assert(cf, true)
|
||||
gtest.Assert(c.Value, "val10")
|
||||
t.Assert(cf, true)
|
||||
t.Assert(c.Value, "val10")
|
||||
f, ff := m.Floor(5)
|
||||
gtest.Assert(ff, true)
|
||||
gtest.Assert(f.Value, "val4")
|
||||
t.Assert(ff, true)
|
||||
t.Assert(f.Value, "val4")
|
||||
})
|
||||
//nofound
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gtree.NewRedBlackTreeFrom(gutil.ComparatorInt, expect)
|
||||
c, cf := m.Ceiling(21)
|
||||
gtest.Assert(cf, false)
|
||||
gtest.Assert(c, nil)
|
||||
t.Assert(cf, false)
|
||||
t.Assert(c, nil)
|
||||
f, ff := m.Floor(-1)
|
||||
gtest.Assert(ff, false)
|
||||
gtest.Assert(f, nil)
|
||||
t.Assert(ff, false)
|
||||
t.Assert(f, nil)
|
||||
})
|
||||
}
|
||||
|
||||
@ -278,11 +284,11 @@ func Test_RedBlackTree_Remove(t *testing.T) {
|
||||
m.Set(i, fmt.Sprintf("val%d", i))
|
||||
}
|
||||
expect := m.Map()
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
for k, v := range expect {
|
||||
m1 := m.Clone()
|
||||
gtest.Assert(m1.Remove(k), v)
|
||||
gtest.Assert(m1.Remove(k), nil)
|
||||
t.Assert(m1.Remove(k), v)
|
||||
t.Assert(m1.Remove(k), nil)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@ -157,6 +157,12 @@ func BenchmarkBool_Val(b *testing.B) {
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkBool_Cas(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
bl.Cas(false, true)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkString_Set(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
str.Set(strconv.Itoa(i))
|
||||
|
||||
@ -16,110 +16,110 @@ import (
|
||||
)
|
||||
|
||||
func Test_Bool(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
i := gtype.NewBool(true)
|
||||
iClone := i.Clone()
|
||||
gtest.AssertEQ(iClone.Set(false), true)
|
||||
gtest.AssertEQ(iClone.Val(), false)
|
||||
t.AssertEQ(iClone.Set(false), true)
|
||||
t.AssertEQ(iClone.Val(), false)
|
||||
|
||||
i1 := gtype.NewBool(false)
|
||||
iClone1 := i1.Clone()
|
||||
gtest.AssertEQ(iClone1.Set(true), false)
|
||||
gtest.AssertEQ(iClone1.Val(), true)
|
||||
t.AssertEQ(iClone1.Set(true), false)
|
||||
t.AssertEQ(iClone1.Val(), true)
|
||||
|
||||
//空参测试
|
||||
i2 := gtype.NewBool()
|
||||
gtest.AssertEQ(i2.Val(), false)
|
||||
t.AssertEQ(i2.Val(), false)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Bool_JSON(t *testing.T) {
|
||||
// Marshal
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
i := gtype.NewBool(true)
|
||||
b1, err1 := json.Marshal(i)
|
||||
b2, err2 := json.Marshal(i.Val())
|
||||
gtest.Assert(err1, nil)
|
||||
gtest.Assert(err2, nil)
|
||||
gtest.Assert(b1, b2)
|
||||
t.Assert(err1, nil)
|
||||
t.Assert(err2, nil)
|
||||
t.Assert(b1, b2)
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
i := gtype.NewBool(false)
|
||||
b1, err1 := json.Marshal(i)
|
||||
b2, err2 := json.Marshal(i.Val())
|
||||
gtest.Assert(err1, nil)
|
||||
gtest.Assert(err2, nil)
|
||||
gtest.Assert(b1, b2)
|
||||
t.Assert(err1, nil)
|
||||
t.Assert(err2, nil)
|
||||
t.Assert(b1, b2)
|
||||
})
|
||||
// Unmarshal
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var err error
|
||||
i := gtype.NewBool()
|
||||
err = json.Unmarshal([]byte("true"), &i)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(i.Val(), true)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(i.Val(), true)
|
||||
err = json.Unmarshal([]byte("false"), &i)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(i.Val(), false)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(i.Val(), false)
|
||||
err = json.Unmarshal([]byte("1"), &i)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(i.Val(), true)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(i.Val(), true)
|
||||
err = json.Unmarshal([]byte("0"), &i)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(i.Val(), false)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(i.Val(), false)
|
||||
})
|
||||
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
i := gtype.NewBool(true)
|
||||
b1, err1 := json.Marshal(i)
|
||||
b2, err2 := json.Marshal(i.Val())
|
||||
gtest.Assert(err1, nil)
|
||||
gtest.Assert(err2, nil)
|
||||
gtest.Assert(b1, b2)
|
||||
t.Assert(err1, nil)
|
||||
t.Assert(err2, nil)
|
||||
t.Assert(b1, b2)
|
||||
|
||||
i2 := gtype.NewBool()
|
||||
err := json.Unmarshal(b2, &i2)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(i2.Val(), i.Val())
|
||||
t.Assert(err, nil)
|
||||
t.Assert(i2.Val(), i.Val())
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
i := gtype.NewBool(false)
|
||||
b1, err1 := json.Marshal(i)
|
||||
b2, err2 := json.Marshal(i.Val())
|
||||
gtest.Assert(err1, nil)
|
||||
gtest.Assert(err2, nil)
|
||||
gtest.Assert(b1, b2)
|
||||
t.Assert(err1, nil)
|
||||
t.Assert(err2, nil)
|
||||
t.Assert(b1, b2)
|
||||
|
||||
i2 := gtype.NewBool()
|
||||
err := json.Unmarshal(b2, &i2)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(i2.Val(), i.Val())
|
||||
t.Assert(err, nil)
|
||||
t.Assert(i2.Val(), i.Val())
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Bool_UnmarshalValue(t *testing.T) {
|
||||
type T struct {
|
||||
type V struct {
|
||||
Name string
|
||||
Var *gtype.Bool
|
||||
}
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var v *V
|
||||
err := gconv.Struct(map[string]interface{}{
|
||||
"name": "john",
|
||||
"var": "true",
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Var.Val(), true)
|
||||
}, &v)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(v.Name, "john")
|
||||
t.Assert(v.Var.Val(), true)
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var v *V
|
||||
err := gconv.Struct(map[string]interface{}{
|
||||
"name": "john",
|
||||
"var": "false",
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Var.Val(), false)
|
||||
}, &v)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(v.Name, "john")
|
||||
t.Assert(v.Var.Val(), false)
|
||||
})
|
||||
}
|
||||
|
||||
@ -17,13 +17,13 @@ import (
|
||||
)
|
||||
|
||||
func Test_Byte(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var wg sync.WaitGroup
|
||||
addTimes := 127
|
||||
i := gtype.NewByte(byte(0))
|
||||
iClone := i.Clone()
|
||||
gtest.AssertEQ(iClone.Set(byte(1)), byte(0))
|
||||
gtest.AssertEQ(iClone.Val(), byte(1))
|
||||
t.AssertEQ(iClone.Set(byte(1)), byte(0))
|
||||
t.AssertEQ(iClone.Val(), byte(1))
|
||||
for index := 0; index < addTimes; index++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
@ -32,46 +32,46 @@ func Test_Byte(t *testing.T) {
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
gtest.AssertEQ(byte(addTimes), i.Val())
|
||||
t.AssertEQ(byte(addTimes), i.Val())
|
||||
|
||||
//空参测试
|
||||
i1 := gtype.NewByte()
|
||||
gtest.AssertEQ(i1.Val(), byte(0))
|
||||
t.AssertEQ(i1.Val(), byte(0))
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Byte_JSON(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
i := gtype.NewByte(49)
|
||||
b1, err1 := json.Marshal(i)
|
||||
b2, err2 := json.Marshal(i.Val())
|
||||
gtest.Assert(err1, nil)
|
||||
gtest.Assert(err2, nil)
|
||||
gtest.Assert(b1, b2)
|
||||
t.Assert(err1, nil)
|
||||
t.Assert(err2, nil)
|
||||
t.Assert(b1, b2)
|
||||
})
|
||||
// Unmarshal
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var err error
|
||||
i := gtype.NewByte()
|
||||
err = json.Unmarshal([]byte("49"), &i)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(i.Val(), "49")
|
||||
t.Assert(err, nil)
|
||||
t.Assert(i.Val(), "49")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Byte_UnmarshalValue(t *testing.T) {
|
||||
type T struct {
|
||||
type V struct {
|
||||
Name string
|
||||
Var *gtype.Byte
|
||||
}
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var v *V
|
||||
err := gconv.Struct(map[string]interface{}{
|
||||
"name": "john",
|
||||
"var": "2",
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Var.Val(), "2")
|
||||
}, &v)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(v.Name, "john")
|
||||
t.Assert(v.Var.Val(), "2")
|
||||
})
|
||||
}
|
||||
|
||||
@ -16,48 +16,48 @@ import (
|
||||
)
|
||||
|
||||
func Test_Bytes(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
i := gtype.NewBytes([]byte("abc"))
|
||||
iClone := i.Clone()
|
||||
gtest.AssertEQ(iClone.Set([]byte("123")), []byte("abc"))
|
||||
gtest.AssertEQ(iClone.Val(), []byte("123"))
|
||||
t.AssertEQ(iClone.Set([]byte("123")), []byte("abc"))
|
||||
t.AssertEQ(iClone.Val(), []byte("123"))
|
||||
|
||||
//空参测试
|
||||
i1 := gtype.NewBytes()
|
||||
gtest.AssertEQ(i1.Val(), nil)
|
||||
t.AssertEQ(i1.Val(), nil)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Bytes_JSON(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
b := []byte("i love gf")
|
||||
i := gtype.NewBytes(b)
|
||||
b1, err1 := json.Marshal(i)
|
||||
b2, err2 := json.Marshal(i.Val())
|
||||
gtest.Assert(err1, nil)
|
||||
gtest.Assert(err2, nil)
|
||||
gtest.Assert(b1, b2)
|
||||
t.Assert(err1, nil)
|
||||
t.Assert(err2, nil)
|
||||
t.Assert(b1, b2)
|
||||
|
||||
i2 := gtype.NewBytes()
|
||||
err := json.Unmarshal(b2, &i2)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(i2.Val(), b)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(i2.Val(), b)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Bytes_UnmarshalValue(t *testing.T) {
|
||||
type T struct {
|
||||
type V struct {
|
||||
Name string
|
||||
Var *gtype.Bytes
|
||||
}
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var v *V
|
||||
err := gconv.Struct(map[string]interface{}{
|
||||
"name": "john",
|
||||
"var": "123",
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Var.Val(), "123")
|
||||
}, &v)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(v.Name, "john")
|
||||
t.Assert(v.Var.Val(), "123")
|
||||
})
|
||||
}
|
||||
|
||||
@ -16,49 +16,49 @@ import (
|
||||
)
|
||||
|
||||
func Test_Float32(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
i := gtype.NewFloat32(0)
|
||||
iClone := i.Clone()
|
||||
gtest.AssertEQ(iClone.Set(0.1), float32(0))
|
||||
gtest.AssertEQ(iClone.Val(), float32(0.1))
|
||||
t.AssertEQ(iClone.Set(0.1), float32(0))
|
||||
t.AssertEQ(iClone.Val(), float32(0.1))
|
||||
|
||||
//空参测试
|
||||
i1 := gtype.NewFloat32()
|
||||
gtest.AssertEQ(i1.Val(), float32(0))
|
||||
t.AssertEQ(i1.Val(), float32(0))
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Float32_JSON(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
v := float32(math.MaxFloat32)
|
||||
i := gtype.NewFloat32(v)
|
||||
b1, err1 := json.Marshal(i)
|
||||
b2, err2 := json.Marshal(i.Val())
|
||||
|
||||
gtest.Assert(err1, nil)
|
||||
gtest.Assert(err2, nil)
|
||||
gtest.Assert(b1, b2)
|
||||
t.Assert(err1, nil)
|
||||
t.Assert(err2, nil)
|
||||
t.Assert(b1, b2)
|
||||
|
||||
i2 := gtype.NewFloat32()
|
||||
err := json.Unmarshal(b2, &i2)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(i2.Val(), v)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(i2.Val(), v)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Float32_UnmarshalValue(t *testing.T) {
|
||||
type T struct {
|
||||
type V struct {
|
||||
Name string
|
||||
Var *gtype.Float32
|
||||
}
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var v *V
|
||||
err := gconv.Struct(map[string]interface{}{
|
||||
"name": "john",
|
||||
"var": "123.456",
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Var.Val(), "123.456")
|
||||
}, &v)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(v.Name, "john")
|
||||
t.Assert(v.Var.Val(), "123.456")
|
||||
})
|
||||
}
|
||||
|
||||
@ -16,47 +16,47 @@ import (
|
||||
)
|
||||
|
||||
func Test_Float64(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
i := gtype.NewFloat64(0)
|
||||
iClone := i.Clone()
|
||||
gtest.AssertEQ(iClone.Set(0.1), float64(0))
|
||||
gtest.AssertEQ(iClone.Val(), float64(0.1))
|
||||
t.AssertEQ(iClone.Set(0.1), float64(0))
|
||||
t.AssertEQ(iClone.Val(), float64(0.1))
|
||||
//空参测试
|
||||
i1 := gtype.NewFloat64()
|
||||
gtest.AssertEQ(i1.Val(), float64(0))
|
||||
t.AssertEQ(i1.Val(), float64(0))
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Float64_JSON(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
v := math.MaxFloat64
|
||||
i := gtype.NewFloat64(v)
|
||||
b1, err1 := json.Marshal(i)
|
||||
b2, err2 := json.Marshal(i.Val())
|
||||
gtest.Assert(err1, nil)
|
||||
gtest.Assert(err2, nil)
|
||||
gtest.Assert(b1, b2)
|
||||
t.Assert(err1, nil)
|
||||
t.Assert(err2, nil)
|
||||
t.Assert(b1, b2)
|
||||
|
||||
i2 := gtype.NewFloat64()
|
||||
err := json.Unmarshal(b2, &i2)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(i2.Val(), v)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(i2.Val(), v)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Float64_UnmarshalValue(t *testing.T) {
|
||||
type T struct {
|
||||
type V struct {
|
||||
Name string
|
||||
Var *gtype.Float64
|
||||
}
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var v *V
|
||||
err := gconv.Struct(map[string]interface{}{
|
||||
"name": "john",
|
||||
"var": "123.456",
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Var.Val(), "123.456")
|
||||
}, &v)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(v.Name, "john")
|
||||
t.Assert(v.Var.Val(), "123.456")
|
||||
})
|
||||
}
|
||||
|
||||
@ -17,13 +17,13 @@ import (
|
||||
)
|
||||
|
||||
func Test_Int32(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var wg sync.WaitGroup
|
||||
addTimes := 1000
|
||||
i := gtype.NewInt32(0)
|
||||
iClone := i.Clone()
|
||||
gtest.AssertEQ(iClone.Set(1), int32(0))
|
||||
gtest.AssertEQ(iClone.Val(), int32(1))
|
||||
t.AssertEQ(iClone.Set(1), int32(0))
|
||||
t.AssertEQ(iClone.Val(), int32(1))
|
||||
for index := 0; index < addTimes; index++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
@ -32,44 +32,44 @@ func Test_Int32(t *testing.T) {
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
gtest.AssertEQ(int32(addTimes), i.Val())
|
||||
t.AssertEQ(int32(addTimes), i.Val())
|
||||
|
||||
//空参测试
|
||||
i1 := gtype.NewInt32()
|
||||
gtest.AssertEQ(i1.Val(), int32(0))
|
||||
t.AssertEQ(i1.Val(), int32(0))
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Int32_JSON(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
v := int32(math.MaxInt32)
|
||||
i := gtype.NewInt32(v)
|
||||
b1, err1 := json.Marshal(i)
|
||||
b2, err2 := json.Marshal(i.Val())
|
||||
gtest.Assert(err1, nil)
|
||||
gtest.Assert(err2, nil)
|
||||
gtest.Assert(b1, b2)
|
||||
t.Assert(err1, nil)
|
||||
t.Assert(err2, nil)
|
||||
t.Assert(b1, b2)
|
||||
|
||||
i2 := gtype.NewInt32()
|
||||
err := json.Unmarshal(b2, &i2)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(i2.Val(), v)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(i2.Val(), v)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Int32_UnmarshalValue(t *testing.T) {
|
||||
type T struct {
|
||||
type V struct {
|
||||
Name string
|
||||
Var *gtype.Int32
|
||||
}
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var v *V
|
||||
err := gconv.Struct(map[string]interface{}{
|
||||
"name": "john",
|
||||
"var": "123",
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Var.Val(), "123")
|
||||
}, &v)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(v.Name, "john")
|
||||
t.Assert(v.Var.Val(), "123")
|
||||
})
|
||||
}
|
||||
|
||||
@ -17,13 +17,13 @@ import (
|
||||
)
|
||||
|
||||
func Test_Int64(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var wg sync.WaitGroup
|
||||
addTimes := 1000
|
||||
i := gtype.NewInt64(0)
|
||||
iClone := i.Clone()
|
||||
gtest.AssertEQ(iClone.Set(1), int64(0))
|
||||
gtest.AssertEQ(iClone.Val(), int64(1))
|
||||
t.AssertEQ(iClone.Set(1), int64(0))
|
||||
t.AssertEQ(iClone.Val(), int64(1))
|
||||
for index := 0; index < addTimes; index++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
@ -32,43 +32,43 @@ func Test_Int64(t *testing.T) {
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
gtest.AssertEQ(int64(addTimes), i.Val())
|
||||
t.AssertEQ(int64(addTimes), i.Val())
|
||||
|
||||
//空参测试
|
||||
i1 := gtype.NewInt64()
|
||||
gtest.AssertEQ(i1.Val(), int64(0))
|
||||
t.AssertEQ(i1.Val(), int64(0))
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Int64_JSON(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
i := gtype.NewInt64(math.MaxInt64)
|
||||
b1, err1 := json.Marshal(i)
|
||||
b2, err2 := json.Marshal(i.Val())
|
||||
gtest.Assert(err1, nil)
|
||||
gtest.Assert(err2, nil)
|
||||
gtest.Assert(b1, b2)
|
||||
t.Assert(err1, nil)
|
||||
t.Assert(err2, nil)
|
||||
t.Assert(b1, b2)
|
||||
|
||||
i2 := gtype.NewInt64()
|
||||
err := json.Unmarshal(b2, &i2)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(i2.Val(), i)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(i2.Val(), i)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Int64_UnmarshalValue(t *testing.T) {
|
||||
type T struct {
|
||||
type V struct {
|
||||
Name string
|
||||
Var *gtype.Int64
|
||||
}
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var v *V
|
||||
err := gconv.Struct(map[string]interface{}{
|
||||
"name": "john",
|
||||
"var": "123",
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Var.Val(), "123")
|
||||
}, &v)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(v.Name, "john")
|
||||
t.Assert(v.Var.Val(), "123")
|
||||
})
|
||||
}
|
||||
|
||||
@ -16,13 +16,13 @@ import (
|
||||
)
|
||||
|
||||
func Test_Int(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var wg sync.WaitGroup
|
||||
addTimes := 1000
|
||||
i := gtype.NewInt(0)
|
||||
iClone := i.Clone()
|
||||
gtest.AssertEQ(iClone.Set(1), 0)
|
||||
gtest.AssertEQ(iClone.Val(), 1)
|
||||
t.AssertEQ(iClone.Set(1), 0)
|
||||
t.AssertEQ(iClone.Val(), 1)
|
||||
for index := 0; index < addTimes; index++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
@ -31,44 +31,44 @@ func Test_Int(t *testing.T) {
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
gtest.AssertEQ(addTimes, i.Val())
|
||||
t.AssertEQ(addTimes, i.Val())
|
||||
|
||||
//空参测试
|
||||
i1 := gtype.NewInt()
|
||||
gtest.AssertEQ(i1.Val(), 0)
|
||||
t.AssertEQ(i1.Val(), 0)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Int_JSON(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
v := 666
|
||||
i := gtype.NewInt(v)
|
||||
b1, err1 := json.Marshal(i)
|
||||
b2, err2 := json.Marshal(i.Val())
|
||||
gtest.Assert(err1, nil)
|
||||
gtest.Assert(err2, nil)
|
||||
gtest.Assert(b1, b2)
|
||||
t.Assert(err1, nil)
|
||||
t.Assert(err2, nil)
|
||||
t.Assert(b1, b2)
|
||||
|
||||
i2 := gtype.NewInt()
|
||||
err := json.Unmarshal(b2, &i2)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(i2.Val(), v)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(i2.Val(), v)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Int_UnmarshalValue(t *testing.T) {
|
||||
type T struct {
|
||||
type V struct {
|
||||
Name string
|
||||
Var *gtype.Int
|
||||
}
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var v *V
|
||||
err := gconv.Struct(map[string]interface{}{
|
||||
"name": "john",
|
||||
"var": "123",
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Var.Val(), "123")
|
||||
}, &v)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(v.Name, "john")
|
||||
t.Assert(v.Var.Val(), "123")
|
||||
})
|
||||
}
|
||||
|
||||
@ -15,50 +15,50 @@ import (
|
||||
)
|
||||
|
||||
func Test_Interface(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
t := Temp{Name: "gf", Age: 18}
|
||||
t1 := Temp{Name: "gf", Age: 19}
|
||||
i := gtype.New(t)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
t1 := Temp{Name: "gf", Age: 18}
|
||||
t2 := Temp{Name: "gf", Age: 19}
|
||||
i := gtype.New(t1)
|
||||
iClone := i.Clone()
|
||||
gtest.AssertEQ(iClone.Set(t1), t)
|
||||
gtest.AssertEQ(iClone.Val().(Temp), t1)
|
||||
t.AssertEQ(iClone.Set(t2), t1)
|
||||
t.AssertEQ(iClone.Val().(Temp), t2)
|
||||
|
||||
//空参测试
|
||||
i1 := gtype.New()
|
||||
gtest.AssertEQ(i1.Val(), nil)
|
||||
t.AssertEQ(i1.Val(), nil)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Interface_JSON(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s := "i love gf"
|
||||
i := gtype.New(s)
|
||||
b1, err1 := json.Marshal(i)
|
||||
b2, err2 := json.Marshal(i.Val())
|
||||
gtest.Assert(err1, nil)
|
||||
gtest.Assert(err2, nil)
|
||||
gtest.Assert(b1, b2)
|
||||
t.Assert(err1, nil)
|
||||
t.Assert(err2, nil)
|
||||
t.Assert(b1, b2)
|
||||
|
||||
i2 := gtype.New()
|
||||
err := json.Unmarshal(b2, &i2)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(i2.Val(), s)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(i2.Val(), s)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Interface_UnmarshalValue(t *testing.T) {
|
||||
type T struct {
|
||||
type V struct {
|
||||
Name string
|
||||
Var *gtype.Interface
|
||||
}
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var v *V
|
||||
err := gconv.Struct(map[string]interface{}{
|
||||
"name": "john",
|
||||
"var": "123",
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Var.Val(), "123")
|
||||
}, &v)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(v.Name, "john")
|
||||
t.Assert(v.Var.Val(), "123")
|
||||
})
|
||||
}
|
||||
|
||||
@ -15,48 +15,48 @@ import (
|
||||
)
|
||||
|
||||
func Test_String(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
i := gtype.NewString("abc")
|
||||
iClone := i.Clone()
|
||||
gtest.AssertEQ(iClone.Set("123"), "abc")
|
||||
gtest.AssertEQ(iClone.Val(), "123")
|
||||
t.AssertEQ(iClone.Set("123"), "abc")
|
||||
t.AssertEQ(iClone.Val(), "123")
|
||||
|
||||
//空参测试
|
||||
i1 := gtype.NewString()
|
||||
gtest.AssertEQ(i1.Val(), "")
|
||||
t.AssertEQ(i1.Val(), "")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_String_JSON(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s := "i love gf"
|
||||
i1 := gtype.NewString(s)
|
||||
b1, err1 := json.Marshal(i1)
|
||||
b2, err2 := json.Marshal(i1.Val())
|
||||
gtest.Assert(err1, nil)
|
||||
gtest.Assert(err2, nil)
|
||||
gtest.Assert(b1, b2)
|
||||
t.Assert(err1, nil)
|
||||
t.Assert(err2, nil)
|
||||
t.Assert(b1, b2)
|
||||
|
||||
i2 := gtype.NewString()
|
||||
err := json.Unmarshal(b2, &i2)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(i2.Val(), s)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(i2.Val(), s)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_String_UnmarshalValue(t *testing.T) {
|
||||
type T struct {
|
||||
type V struct {
|
||||
Name string
|
||||
Var *gtype.String
|
||||
}
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var v *V
|
||||
err := gconv.Struct(map[string]interface{}{
|
||||
"name": "john",
|
||||
"var": "123",
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Var.Val(), "123")
|
||||
}, &v)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(v.Name, "john")
|
||||
t.Assert(v.Var.Val(), "123")
|
||||
})
|
||||
}
|
||||
|
||||
@ -17,13 +17,13 @@ import (
|
||||
)
|
||||
|
||||
func Test_Uint32(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var wg sync.WaitGroup
|
||||
addTimes := 1000
|
||||
i := gtype.NewUint32(0)
|
||||
iClone := i.Clone()
|
||||
gtest.AssertEQ(iClone.Set(1), uint32(0))
|
||||
gtest.AssertEQ(iClone.Val(), uint32(1))
|
||||
t.AssertEQ(iClone.Set(1), uint32(0))
|
||||
t.AssertEQ(iClone.Val(), uint32(1))
|
||||
for index := 0; index < addTimes; index++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
@ -32,43 +32,43 @@ func Test_Uint32(t *testing.T) {
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
gtest.AssertEQ(uint32(addTimes), i.Val())
|
||||
t.AssertEQ(uint32(addTimes), i.Val())
|
||||
|
||||
//空参测试
|
||||
i1 := gtype.NewUint32()
|
||||
gtest.AssertEQ(i1.Val(), uint32(0))
|
||||
t.AssertEQ(i1.Val(), uint32(0))
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Uint32_JSON(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
i := gtype.NewUint32(math.MaxUint32)
|
||||
b1, err1 := json.Marshal(i)
|
||||
b2, err2 := json.Marshal(i.Val())
|
||||
gtest.Assert(err1, nil)
|
||||
gtest.Assert(err2, nil)
|
||||
gtest.Assert(b1, b2)
|
||||
t.Assert(err1, nil)
|
||||
t.Assert(err2, nil)
|
||||
t.Assert(b1, b2)
|
||||
|
||||
i2 := gtype.NewUint32()
|
||||
err := json.Unmarshal(b2, &i2)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(i2.Val(), i)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(i2.Val(), i)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Uint32_UnmarshalValue(t *testing.T) {
|
||||
type T struct {
|
||||
type V struct {
|
||||
Name string
|
||||
Var *gtype.Uint32
|
||||
}
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var v *V
|
||||
err := gconv.Struct(map[string]interface{}{
|
||||
"name": "john",
|
||||
"var": "123",
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Var.Val(), "123")
|
||||
}, &v)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(v.Name, "john")
|
||||
t.Assert(v.Var.Val(), "123")
|
||||
})
|
||||
}
|
||||
|
||||
@ -23,13 +23,13 @@ type Temp struct {
|
||||
}
|
||||
|
||||
func Test_Uint64(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var wg sync.WaitGroup
|
||||
addTimes := 1000
|
||||
i := gtype.NewUint64(0)
|
||||
iClone := i.Clone()
|
||||
gtest.AssertEQ(iClone.Set(1), uint64(0))
|
||||
gtest.AssertEQ(iClone.Val(), uint64(1))
|
||||
t.AssertEQ(iClone.Set(1), uint64(0))
|
||||
t.AssertEQ(iClone.Val(), uint64(1))
|
||||
for index := 0; index < addTimes; index++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
@ -38,42 +38,42 @@ func Test_Uint64(t *testing.T) {
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
gtest.AssertEQ(uint64(addTimes), i.Val())
|
||||
t.AssertEQ(uint64(addTimes), i.Val())
|
||||
|
||||
//空参测试
|
||||
i1 := gtype.NewUint64()
|
||||
gtest.AssertEQ(i1.Val(), uint64(0))
|
||||
t.AssertEQ(i1.Val(), uint64(0))
|
||||
})
|
||||
}
|
||||
func Test_Uint64_JSON(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
i := gtype.NewUint64(math.MaxUint64)
|
||||
b1, err1 := json.Marshal(i)
|
||||
b2, err2 := json.Marshal(i.Val())
|
||||
gtest.Assert(err1, nil)
|
||||
gtest.Assert(err2, nil)
|
||||
gtest.Assert(b1, b2)
|
||||
t.Assert(err1, nil)
|
||||
t.Assert(err2, nil)
|
||||
t.Assert(b1, b2)
|
||||
|
||||
i2 := gtype.NewUint64()
|
||||
err := json.Unmarshal(b2, &i2)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(i2.Val(), i)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(i2.Val(), i)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Uint64_UnmarshalValue(t *testing.T) {
|
||||
type T struct {
|
||||
type V struct {
|
||||
Name string
|
||||
Var *gtype.Uint64
|
||||
}
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var v *V
|
||||
err := gconv.Struct(map[string]interface{}{
|
||||
"name": "john",
|
||||
"var": "123",
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Var.Val(), "123")
|
||||
}, &v)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(v.Name, "john")
|
||||
t.Assert(v.Var.Val(), "123")
|
||||
})
|
||||
}
|
||||
|
||||
@ -16,13 +16,13 @@ import (
|
||||
)
|
||||
|
||||
func Test_Uint(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var wg sync.WaitGroup
|
||||
addTimes := 1000
|
||||
i := gtype.NewUint(0)
|
||||
iClone := i.Clone()
|
||||
gtest.AssertEQ(iClone.Set(1), uint(0))
|
||||
gtest.AssertEQ(iClone.Val(), uint(1))
|
||||
t.AssertEQ(iClone.Set(1), uint(0))
|
||||
t.AssertEQ(iClone.Val(), uint(1))
|
||||
for index := 0; index < addTimes; index++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
@ -31,43 +31,43 @@ func Test_Uint(t *testing.T) {
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
gtest.AssertEQ(uint(addTimes), i.Val())
|
||||
t.AssertEQ(uint(addTimes), i.Val())
|
||||
|
||||
//空参测试
|
||||
i1 := gtype.NewUint()
|
||||
gtest.AssertEQ(i1.Val(), uint(0))
|
||||
t.AssertEQ(i1.Val(), uint(0))
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Uint_JSON(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
i := gtype.NewUint(666)
|
||||
b1, err1 := json.Marshal(i)
|
||||
b2, err2 := json.Marshal(i.Val())
|
||||
gtest.Assert(err1, nil)
|
||||
gtest.Assert(err2, nil)
|
||||
gtest.Assert(b1, b2)
|
||||
t.Assert(err1, nil)
|
||||
t.Assert(err2, nil)
|
||||
t.Assert(b1, b2)
|
||||
|
||||
i2 := gtype.NewUint()
|
||||
err := json.Unmarshal(b2, &i2)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(i2.Val(), i)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(i2.Val(), i)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Uint_UnmarshalValue(t *testing.T) {
|
||||
type T struct {
|
||||
type V struct {
|
||||
Name string
|
||||
Var *gtype.Uint
|
||||
}
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var v *V
|
||||
err := gconv.Struct(map[string]interface{}{
|
||||
"name": "john",
|
||||
"var": "123",
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Var.Val(), "123")
|
||||
}, &v)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(v.Name, "john")
|
||||
t.Assert(v.Var.Val(), "123")
|
||||
})
|
||||
}
|
||||
|
||||
@ -46,6 +46,11 @@ func Create(value interface{}, safe ...bool) Var {
|
||||
return v
|
||||
}
|
||||
|
||||
// Clone does a shallow copy of current Var and returns a pointer to this Var.
|
||||
func (v *Var) Clone() *Var {
|
||||
return New(v.Val(), v.safe)
|
||||
}
|
||||
|
||||
// Set sets <value> to <v>, and returns the old value.
|
||||
func (v *Var) Set(value interface{}) (old interface{}) {
|
||||
if v.safe {
|
||||
|
||||
@ -22,66 +22,66 @@ import (
|
||||
)
|
||||
|
||||
func Test_Set(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var v gvar.Var
|
||||
v.Set(123.456)
|
||||
gtest.Assert(v.Val(), 123.456)
|
||||
t.Assert(v.Val(), 123.456)
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var v gvar.Var
|
||||
v.Set(123.456)
|
||||
gtest.Assert(v.Val(), 123.456)
|
||||
t.Assert(v.Val(), 123.456)
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
v := gvar.Create(123.456)
|
||||
gtest.Assert(v.Val(), 123.456)
|
||||
t.Assert(v.Val(), 123.456)
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
objOne := gvar.New("old", true)
|
||||
objOneOld, _ := objOne.Set("new").(string)
|
||||
gtest.Assert(objOneOld, "old")
|
||||
t.Assert(objOneOld, "old")
|
||||
|
||||
objTwo := gvar.New("old", false)
|
||||
objTwoOld, _ := objTwo.Set("new").(string)
|
||||
gtest.Assert(objTwoOld, "old")
|
||||
t.Assert(objTwoOld, "old")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Val(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
objOne := gvar.New(1, true)
|
||||
objOneOld, _ := objOne.Val().(int)
|
||||
gtest.Assert(objOneOld, 1)
|
||||
t.Assert(objOneOld, 1)
|
||||
|
||||
objTwo := gvar.New(1, false)
|
||||
objTwoOld, _ := objTwo.Val().(int)
|
||||
gtest.Assert(objTwoOld, 1)
|
||||
t.Assert(objTwoOld, 1)
|
||||
})
|
||||
}
|
||||
func Test_Interface(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
objOne := gvar.New(1, true)
|
||||
objOneOld, _ := objOne.Interface().(int)
|
||||
gtest.Assert(objOneOld, 1)
|
||||
t.Assert(objOneOld, 1)
|
||||
|
||||
objTwo := gvar.New(1, false)
|
||||
objTwoOld, _ := objTwo.Interface().(int)
|
||||
gtest.Assert(objTwoOld, 1)
|
||||
t.Assert(objTwoOld, 1)
|
||||
})
|
||||
}
|
||||
func Test_IsNil(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
objOne := gvar.New(nil, true)
|
||||
gtest.Assert(objOne.IsNil(), true)
|
||||
t.Assert(objOne.IsNil(), true)
|
||||
|
||||
objTwo := gvar.New("noNil", false)
|
||||
gtest.Assert(objTwo.IsNil(), false)
|
||||
t.Assert(objTwo.IsNil(), false)
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Bytes(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
x := int32(1)
|
||||
bytesBuffer := bytes.NewBuffer([]byte{})
|
||||
binary.Write(bytesBuffer, binary.BigEndian, x)
|
||||
@ -92,233 +92,233 @@ func Test_Bytes(t *testing.T) {
|
||||
var y int32
|
||||
binary.Read(bBuf, binary.BigEndian, &y)
|
||||
|
||||
gtest.Assert(x, y)
|
||||
t.Assert(x, y)
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
func Test_String(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var str string = "hello"
|
||||
objOne := gvar.New(str, true)
|
||||
gtest.Assert(objOne.String(), str)
|
||||
t.Assert(objOne.String(), str)
|
||||
|
||||
})
|
||||
}
|
||||
func Test_Bool(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var ok bool = true
|
||||
objOne := gvar.New(ok, true)
|
||||
gtest.Assert(objOne.Bool(), ok)
|
||||
t.Assert(objOne.Bool(), ok)
|
||||
|
||||
ok = false
|
||||
objTwo := gvar.New(ok, true)
|
||||
gtest.Assert(objTwo.Bool(), ok)
|
||||
t.Assert(objTwo.Bool(), ok)
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Int(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var num int = 1
|
||||
objOne := gvar.New(num, true)
|
||||
gtest.Assert(objOne.Int(), num)
|
||||
t.Assert(objOne.Int(), num)
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Int8(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var num int8 = 1
|
||||
objOne := gvar.New(num, true)
|
||||
gtest.Assert(objOne.Int8(), num)
|
||||
t.Assert(objOne.Int8(), num)
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Int16(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var num int16 = 1
|
||||
objOne := gvar.New(num, true)
|
||||
gtest.Assert(objOne.Int16(), num)
|
||||
t.Assert(objOne.Int16(), num)
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Int32(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var num int32 = 1
|
||||
objOne := gvar.New(num, true)
|
||||
gtest.Assert(objOne.Int32(), num)
|
||||
t.Assert(objOne.Int32(), num)
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Int64(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var num int64 = 1
|
||||
objOne := gvar.New(num, true)
|
||||
gtest.Assert(objOne.Int64(), num)
|
||||
t.Assert(objOne.Int64(), num)
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Uint(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var num uint = 1
|
||||
objOne := gvar.New(num, true)
|
||||
gtest.Assert(objOne.Uint(), num)
|
||||
t.Assert(objOne.Uint(), num)
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Uint8(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var num uint8 = 1
|
||||
objOne := gvar.New(num, true)
|
||||
gtest.Assert(objOne.Uint8(), num)
|
||||
t.Assert(objOne.Uint8(), num)
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Uint16(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var num uint16 = 1
|
||||
objOne := gvar.New(num, true)
|
||||
gtest.Assert(objOne.Uint16(), num)
|
||||
t.Assert(objOne.Uint16(), num)
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Uint32(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var num uint32 = 1
|
||||
objOne := gvar.New(num, true)
|
||||
gtest.Assert(objOne.Uint32(), num)
|
||||
t.Assert(objOne.Uint32(), num)
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Uint64(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var num uint64 = 1
|
||||
objOne := gvar.New(num, true)
|
||||
gtest.Assert(objOne.Uint64(), num)
|
||||
t.Assert(objOne.Uint64(), num)
|
||||
|
||||
})
|
||||
}
|
||||
func Test_Float32(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var num float32 = 1.1
|
||||
objOne := gvar.New(num, true)
|
||||
gtest.Assert(objOne.Float32(), num)
|
||||
t.Assert(objOne.Float32(), num)
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Float64(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var num float64 = 1.1
|
||||
objOne := gvar.New(num, true)
|
||||
gtest.Assert(objOne.Float64(), num)
|
||||
t.Assert(objOne.Float64(), num)
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Ints(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var arr = []int{1, 2, 3, 4, 5}
|
||||
objOne := gvar.New(arr, true)
|
||||
gtest.Assert(objOne.Ints()[0], arr[0])
|
||||
t.Assert(objOne.Ints()[0], arr[0])
|
||||
})
|
||||
}
|
||||
func Test_Floats(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var arr = []float64{1, 2, 3, 4, 5}
|
||||
objOne := gvar.New(arr, true)
|
||||
gtest.Assert(objOne.Floats()[0], arr[0])
|
||||
t.Assert(objOne.Floats()[0], arr[0])
|
||||
})
|
||||
}
|
||||
func Test_Strings(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var arr = []string{"hello", "world"}
|
||||
objOne := gvar.New(arr, true)
|
||||
gtest.Assert(objOne.Strings()[0], arr[0])
|
||||
t.Assert(objOne.Strings()[0], arr[0])
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Interfaces(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var arr = []int{1, 2, 3, 4, 5}
|
||||
objOne := gvar.New(arr, true)
|
||||
gtest.Assert(objOne.Interfaces(), arr)
|
||||
t.Assert(objOne.Interfaces(), arr)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Slice(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var arr = []int{1, 2, 3, 4, 5}
|
||||
objOne := gvar.New(arr, true)
|
||||
gtest.Assert(objOne.Slice(), arr)
|
||||
t.Assert(objOne.Slice(), arr)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Array(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var arr = []int{1, 2, 3, 4, 5}
|
||||
objOne := gvar.New(arr, false)
|
||||
gtest.Assert(objOne.Array(), arr)
|
||||
t.Assert(objOne.Array(), arr)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Vars(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var arr = []int{1, 2, 3, 4, 5}
|
||||
objOne := gvar.New(arr, false)
|
||||
gtest.Assert(len(objOne.Vars()), 5)
|
||||
gtest.Assert(objOne.Vars()[0].Int(), 1)
|
||||
gtest.Assert(objOne.Vars()[4].Int(), 5)
|
||||
t.Assert(len(objOne.Vars()), 5)
|
||||
t.Assert(objOne.Vars()[0].Int(), 1)
|
||||
t.Assert(objOne.Vars()[4].Int(), 5)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Time(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var timeUnix int64 = 1556242660
|
||||
objOne := gvar.New(timeUnix, true)
|
||||
gtest.Assert(objOne.Time().Unix(), timeUnix)
|
||||
t.Assert(objOne.Time().Unix(), timeUnix)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_GTime(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var timeUnix int64 = 1556242660
|
||||
objOne := gvar.New(timeUnix, true)
|
||||
gtest.Assert(objOne.GTime().Unix(), timeUnix)
|
||||
t.Assert(objOne.GTime().Unix(), timeUnix)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Duration(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var timeUnix int64 = 1556242660
|
||||
objOne := gvar.New(timeUnix, true)
|
||||
gtest.Assert(objOne.Duration(), time.Duration(timeUnix))
|
||||
t.Assert(objOne.Duration(), time.Duration(timeUnix))
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Map(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := g.Map{
|
||||
"k1": "v1",
|
||||
"k2": "v2",
|
||||
}
|
||||
objOne := gvar.New(m, true)
|
||||
gtest.Assert(objOne.Map()["k1"], m["k1"])
|
||||
gtest.Assert(objOne.Map()["k2"], m["k2"])
|
||||
t.Assert(objOne.Map()["k1"], m["k1"])
|
||||
t.Assert(objOne.Map()["k2"], m["k2"])
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Struct(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type StTest struct {
|
||||
Test int
|
||||
}
|
||||
@ -332,76 +332,76 @@ func Test_Struct(t *testing.T) {
|
||||
|
||||
objOne.Struct(testObj)
|
||||
|
||||
gtest.Assert(testObj.Test, Kv["Test"])
|
||||
t.Assert(testObj.Test, Kv["Test"])
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type StTest struct {
|
||||
Test int8
|
||||
}
|
||||
o := &StTest{}
|
||||
v := gvar.New(g.Slice{"Test", "-25"})
|
||||
v.Struct(o)
|
||||
gtest.Assert(o.Test, -25)
|
||||
t.Assert(o.Test, -25)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Json(t *testing.T) {
|
||||
// Marshal
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s := "i love gf"
|
||||
v := gvar.New(s)
|
||||
b1, err1 := json.Marshal(v)
|
||||
b2, err2 := json.Marshal(s)
|
||||
gtest.Assert(err1, err2)
|
||||
gtest.Assert(b1, b2)
|
||||
t.Assert(err1, err2)
|
||||
t.Assert(b1, b2)
|
||||
})
|
||||
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s := int64(math.MaxInt64)
|
||||
v := gvar.New(s)
|
||||
b1, err1 := json.Marshal(v)
|
||||
b2, err2 := json.Marshal(s)
|
||||
gtest.Assert(err1, err2)
|
||||
gtest.Assert(b1, b2)
|
||||
t.Assert(err1, err2)
|
||||
t.Assert(b1, b2)
|
||||
})
|
||||
|
||||
// Unmarshal
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s := "i love gf"
|
||||
v := gvar.New(nil)
|
||||
b, err := json.Marshal(s)
|
||||
gtest.Assert(err, nil)
|
||||
t.Assert(err, nil)
|
||||
|
||||
err = json.Unmarshal(b, v)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(v.String(), s)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(v.String(), s)
|
||||
})
|
||||
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var v gvar.Var
|
||||
s := "i love gf"
|
||||
b, err := json.Marshal(s)
|
||||
gtest.Assert(err, nil)
|
||||
t.Assert(err, nil)
|
||||
|
||||
err = json.Unmarshal(b, &v)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(v.String(), s)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(v.String(), s)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_UnmarshalValue(t *testing.T) {
|
||||
type T struct {
|
||||
type V struct {
|
||||
Name string
|
||||
Var *gvar.Var
|
||||
}
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var v *V
|
||||
err := gconv.Struct(map[string]interface{}{
|
||||
"name": "john",
|
||||
"var": "v",
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Var.String(), "v")
|
||||
}, &v)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(v.Name, "john")
|
||||
t.Assert(v.Var.String(), "v")
|
||||
})
|
||||
}
|
||||
|
||||
@ -40,111 +40,111 @@ var (
|
||||
)
|
||||
|
||||
func TestEncrypt(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
data, err := gaes.Encrypt(content, key_16)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(data, []byte(content_16))
|
||||
t.Assert(err, nil)
|
||||
t.Assert(data, []byte(content_16))
|
||||
data, err = gaes.Encrypt(content, key_24)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(data, []byte(content_24))
|
||||
t.Assert(err, nil)
|
||||
t.Assert(data, []byte(content_24))
|
||||
data, err = gaes.Encrypt(content, key_32)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(data, []byte(content_32))
|
||||
t.Assert(err, nil)
|
||||
t.Assert(data, []byte(content_32))
|
||||
data, err = gaes.Encrypt(content, key_16, iv)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(data, []byte(content_16_iv))
|
||||
t.Assert(err, nil)
|
||||
t.Assert(data, []byte(content_16_iv))
|
||||
data, err = gaes.Encrypt(content, key_32, iv)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(data, []byte(content_32_iv))
|
||||
t.Assert(err, nil)
|
||||
t.Assert(data, []byte(content_32_iv))
|
||||
})
|
||||
}
|
||||
|
||||
func TestDecrypt(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
decrypt, err := gaes.Decrypt([]byte(content_16), key_16)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(decrypt, content)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(decrypt, content)
|
||||
|
||||
decrypt, err = gaes.Decrypt([]byte(content_24), key_24)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(decrypt, content)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(decrypt, content)
|
||||
|
||||
decrypt, err = gaes.Decrypt([]byte(content_32), key_32)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(decrypt, content)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(decrypt, content)
|
||||
|
||||
decrypt, err = gaes.Decrypt([]byte(content_16_iv), key_16, iv)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(decrypt, content)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(decrypt, content)
|
||||
|
||||
decrypt, err = gaes.Decrypt([]byte(content_32_iv), key_32, iv)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(decrypt, content)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(decrypt, content)
|
||||
|
||||
decrypt, err = gaes.Decrypt([]byte(content_32_iv), keys, iv)
|
||||
gtest.Assert(err, "invalid padding")
|
||||
t.Assert(err, "invalid padding")
|
||||
})
|
||||
}
|
||||
|
||||
func TestEncryptErr(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// encrypt key error
|
||||
_, err := gaes.Encrypt(content, key_err)
|
||||
gtest.AssertNE(err, nil)
|
||||
t.AssertNE(err, nil)
|
||||
})
|
||||
}
|
||||
|
||||
func TestDecryptErr(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// decrypt key error
|
||||
encrypt, err := gaes.Encrypt(content, key_16)
|
||||
_, err = gaes.Decrypt(encrypt, key_err)
|
||||
gtest.AssertNE(err, nil)
|
||||
t.AssertNE(err, nil)
|
||||
|
||||
// decrypt content too short error
|
||||
_, err = gaes.Decrypt([]byte("test"), key_16)
|
||||
gtest.AssertNE(err, nil)
|
||||
t.AssertNE(err, nil)
|
||||
|
||||
// decrypt content size error
|
||||
_, err = gaes.Decrypt(key_17, key_16)
|
||||
gtest.AssertNE(err, nil)
|
||||
t.AssertNE(err, nil)
|
||||
})
|
||||
}
|
||||
|
||||
func TestPKCS5UnPaddingErr(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// PKCS5UnPadding blockSize zero
|
||||
_, err := gaes.PKCS5UnPadding(content, 0)
|
||||
gtest.AssertNE(err, nil)
|
||||
t.AssertNE(err, nil)
|
||||
|
||||
// PKCS5UnPadding src len zero
|
||||
_, err = gaes.PKCS5UnPadding([]byte(""), 16)
|
||||
gtest.AssertNE(err, nil)
|
||||
t.AssertNE(err, nil)
|
||||
|
||||
// PKCS5UnPadding src len > blockSize
|
||||
_, err = gaes.PKCS5UnPadding(key_17, 16)
|
||||
gtest.AssertNE(err, nil)
|
||||
t.AssertNE(err, nil)
|
||||
|
||||
// PKCS5UnPadding src len > blockSize
|
||||
_, err = gaes.PKCS5UnPadding(key_32_err, 32)
|
||||
gtest.AssertNE(err, nil)
|
||||
t.AssertNE(err, nil)
|
||||
})
|
||||
}
|
||||
|
||||
func TestEncryptCFB(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var padding int = 0
|
||||
data, err := gaes.EncryptCFB(content, key_16, &padding, iv)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(padding, padding_size)
|
||||
gtest.Assert(data, []byte(content_16_cfb))
|
||||
t.Assert(err, nil)
|
||||
t.Assert(padding, padding_size)
|
||||
t.Assert(data, []byte(content_16_cfb))
|
||||
})
|
||||
}
|
||||
|
||||
func TestDecryptCFB(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
decrypt, err := gaes.DecryptCFB([]byte(content_16_cfb), key_16, padding_size, iv)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(decrypt, content)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(decrypt, content)
|
||||
})
|
||||
}
|
||||
|
||||
@ -17,17 +17,17 @@ import (
|
||||
)
|
||||
|
||||
func TestEncrypt(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s := "pibigstar"
|
||||
result := 693191136
|
||||
encrypt1 := gcrc32.Encrypt(s)
|
||||
encrypt2 := gcrc32.Encrypt([]byte(s))
|
||||
gtest.AssertEQ(int(encrypt1), result)
|
||||
gtest.AssertEQ(int(encrypt2), result)
|
||||
t.AssertEQ(int(encrypt1), result)
|
||||
t.AssertEQ(int(encrypt2), result)
|
||||
|
||||
strmd5, _ := gmd5.Encrypt(s)
|
||||
test1 := gcrc32.Encrypt(strmd5)
|
||||
test2 := gcrc32.Encrypt([]byte(strmd5))
|
||||
gtest.AssertEQ(test2, test1)
|
||||
t.AssertEQ(test2, test1)
|
||||
})
|
||||
}
|
||||
|
||||
@ -21,81 +21,81 @@ var (
|
||||
)
|
||||
|
||||
func TestDesECB(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
key := []byte("11111111")
|
||||
text := []byte("12345678")
|
||||
padding := gdes.NOPADDING
|
||||
result := "858b176da8b12503"
|
||||
// encrypt test
|
||||
cipherText, err := gdes.EncryptECB(text, key, padding)
|
||||
gtest.AssertEQ(err, nil)
|
||||
gtest.AssertEQ(hex.EncodeToString(cipherText), result)
|
||||
t.AssertEQ(err, nil)
|
||||
t.AssertEQ(hex.EncodeToString(cipherText), result)
|
||||
// decrypt test
|
||||
clearText, err := gdes.DecryptECB(cipherText, key, padding)
|
||||
gtest.AssertEQ(err, nil)
|
||||
gtest.AssertEQ(string(clearText), "12345678")
|
||||
t.AssertEQ(err, nil)
|
||||
t.AssertEQ(string(clearText), "12345678")
|
||||
|
||||
// encrypt err test. when throw exception,the err is not equal nil and the string is nil
|
||||
errEncrypt, err := gdes.EncryptECB(text, key, errPadding)
|
||||
gtest.AssertNE(err, nil)
|
||||
gtest.AssertEQ(errEncrypt, nil)
|
||||
t.AssertNE(err, nil)
|
||||
t.AssertEQ(errEncrypt, nil)
|
||||
errEncrypt, err = gdes.EncryptECB(text, errKey, padding)
|
||||
gtest.AssertNE(err, nil)
|
||||
gtest.AssertEQ(errEncrypt, nil)
|
||||
t.AssertNE(err, nil)
|
||||
t.AssertEQ(errEncrypt, nil)
|
||||
// err decrypt test.
|
||||
errDecrypt, err := gdes.DecryptECB(cipherText, errKey, padding)
|
||||
gtest.AssertNE(err, nil)
|
||||
gtest.AssertEQ(errDecrypt, nil)
|
||||
t.AssertNE(err, nil)
|
||||
t.AssertEQ(errDecrypt, nil)
|
||||
errDecrypt, err = gdes.DecryptECB(cipherText, key, errPadding)
|
||||
gtest.AssertNE(err, nil)
|
||||
gtest.AssertEQ(errDecrypt, nil)
|
||||
t.AssertNE(err, nil)
|
||||
t.AssertEQ(errDecrypt, nil)
|
||||
})
|
||||
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
key := []byte("11111111")
|
||||
text := []byte("12345678")
|
||||
padding := gdes.PKCS5PADDING
|
||||
errPadding := 5
|
||||
result := "858b176da8b12503ad6a88b4fa37833d"
|
||||
cipherText, err := gdes.EncryptECB(text, key, padding)
|
||||
gtest.AssertEQ(err, nil)
|
||||
gtest.AssertEQ(hex.EncodeToString(cipherText), result)
|
||||
t.AssertEQ(err, nil)
|
||||
t.AssertEQ(hex.EncodeToString(cipherText), result)
|
||||
// decrypt test
|
||||
clearText, err := gdes.DecryptECB(cipherText, key, padding)
|
||||
gtest.AssertEQ(err, nil)
|
||||
gtest.AssertEQ(string(clearText), "12345678")
|
||||
t.AssertEQ(err, nil)
|
||||
t.AssertEQ(string(clearText), "12345678")
|
||||
|
||||
// err test
|
||||
errEncrypt, err := gdes.EncryptECB(text, key, errPadding)
|
||||
gtest.AssertNE(err, nil)
|
||||
gtest.AssertEQ(errEncrypt, nil)
|
||||
t.AssertNE(err, nil)
|
||||
t.AssertEQ(errEncrypt, nil)
|
||||
errDecrypt, err := gdes.DecryptECB(cipherText, errKey, padding)
|
||||
gtest.AssertNE(err, nil)
|
||||
gtest.AssertEQ(errDecrypt, nil)
|
||||
t.AssertNE(err, nil)
|
||||
t.AssertEQ(errDecrypt, nil)
|
||||
})
|
||||
}
|
||||
|
||||
func Test3DesECB(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
key := []byte("1111111111111234")
|
||||
text := []byte("1234567812345678")
|
||||
padding := gdes.NOPADDING
|
||||
result := "a23ee24b98c26263a23ee24b98c26263"
|
||||
// encrypt test
|
||||
cipherText, err := gdes.EncryptECBTriple(text, key, padding)
|
||||
gtest.AssertEQ(err, nil)
|
||||
gtest.AssertEQ(hex.EncodeToString(cipherText), result)
|
||||
t.AssertEQ(err, nil)
|
||||
t.AssertEQ(hex.EncodeToString(cipherText), result)
|
||||
// decrypt test
|
||||
clearText, err := gdes.DecryptECBTriple(cipherText, key, padding)
|
||||
gtest.AssertEQ(err, nil)
|
||||
gtest.AssertEQ(string(clearText), "1234567812345678")
|
||||
t.AssertEQ(err, nil)
|
||||
t.AssertEQ(string(clearText), "1234567812345678")
|
||||
// err test
|
||||
errEncrypt, err := gdes.EncryptECB(text, key, errPadding)
|
||||
gtest.AssertNE(err, nil)
|
||||
gtest.AssertEQ(errEncrypt, nil)
|
||||
t.AssertNE(err, nil)
|
||||
t.AssertEQ(errEncrypt, nil)
|
||||
})
|
||||
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
key := []byte("111111111111123412345678")
|
||||
text := []byte("123456789")
|
||||
padding := gdes.PKCS5PADDING
|
||||
@ -103,29 +103,29 @@ func Test3DesECB(t *testing.T) {
|
||||
result := "37989b1effc07a6d00ff89a7d052e79f"
|
||||
// encrypt test
|
||||
cipherText, err := gdes.EncryptECBTriple(text, key, padding)
|
||||
gtest.AssertEQ(err, nil)
|
||||
gtest.AssertEQ(hex.EncodeToString(cipherText), result)
|
||||
t.AssertEQ(err, nil)
|
||||
t.AssertEQ(hex.EncodeToString(cipherText), result)
|
||||
// decrypt test
|
||||
clearText, err := gdes.DecryptECBTriple(cipherText, key, padding)
|
||||
gtest.AssertEQ(err, nil)
|
||||
gtest.AssertEQ(string(clearText), "123456789")
|
||||
t.AssertEQ(err, nil)
|
||||
t.AssertEQ(string(clearText), "123456789")
|
||||
// err test, when key is err, but text and padding is right
|
||||
errEncrypt, err := gdes.EncryptECBTriple(text, errKey, padding)
|
||||
gtest.AssertNE(err, nil)
|
||||
gtest.AssertEQ(errEncrypt, nil)
|
||||
t.AssertNE(err, nil)
|
||||
t.AssertEQ(errEncrypt, nil)
|
||||
// when padding is err,but key and text is right
|
||||
errEncrypt, err = gdes.EncryptECBTriple(text, key, errPadding)
|
||||
gtest.AssertNE(err, nil)
|
||||
gtest.AssertEQ(errEncrypt, nil)
|
||||
t.AssertNE(err, nil)
|
||||
t.AssertEQ(errEncrypt, nil)
|
||||
// decrypt err test,when key is err
|
||||
errEncrypt, err = gdes.DecryptECBTriple(text, errKey, padding)
|
||||
gtest.AssertNE(err, nil)
|
||||
gtest.AssertEQ(errEncrypt, nil)
|
||||
t.AssertNE(err, nil)
|
||||
t.AssertEQ(errEncrypt, nil)
|
||||
})
|
||||
}
|
||||
|
||||
func TestDesCBC(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
key := []byte("11111111")
|
||||
text := []byte("1234567812345678")
|
||||
padding := gdes.NOPADDING
|
||||
@ -133,39 +133,39 @@ func TestDesCBC(t *testing.T) {
|
||||
result := "40826a5800608c87585ca7c9efabee47"
|
||||
// encrypt test
|
||||
cipherText, err := gdes.EncryptCBC(text, key, iv, padding)
|
||||
gtest.AssertEQ(err, nil)
|
||||
gtest.AssertEQ(hex.EncodeToString(cipherText), result)
|
||||
t.AssertEQ(err, nil)
|
||||
t.AssertEQ(hex.EncodeToString(cipherText), result)
|
||||
// decrypt test
|
||||
clearText, err := gdes.DecryptCBC(cipherText, key, iv, padding)
|
||||
gtest.AssertEQ(err, nil)
|
||||
gtest.AssertEQ(string(clearText), "1234567812345678")
|
||||
t.AssertEQ(err, nil)
|
||||
t.AssertEQ(string(clearText), "1234567812345678")
|
||||
// encrypt err test.
|
||||
errEncrypt, err := gdes.EncryptCBC(text, errKey, iv, padding)
|
||||
gtest.AssertNE(err, nil)
|
||||
gtest.AssertEQ(errEncrypt, nil)
|
||||
t.AssertNE(err, nil)
|
||||
t.AssertEQ(errEncrypt, nil)
|
||||
// the iv is err
|
||||
errEncrypt, err = gdes.EncryptCBC(text, key, errIv, padding)
|
||||
gtest.AssertNE(err, nil)
|
||||
gtest.AssertEQ(errEncrypt, nil)
|
||||
t.AssertNE(err, nil)
|
||||
t.AssertEQ(errEncrypt, nil)
|
||||
// the padding is err
|
||||
errEncrypt, err = gdes.EncryptCBC(text, key, iv, errPadding)
|
||||
gtest.AssertNE(err, nil)
|
||||
gtest.AssertEQ(errEncrypt, nil)
|
||||
t.AssertNE(err, nil)
|
||||
t.AssertEQ(errEncrypt, nil)
|
||||
// decrypt err test. the key is err
|
||||
errDecrypt, err := gdes.DecryptCBC(cipherText, errKey, iv, padding)
|
||||
gtest.AssertNE(err, nil)
|
||||
gtest.AssertEQ(errDecrypt, nil)
|
||||
t.AssertNE(err, nil)
|
||||
t.AssertEQ(errDecrypt, nil)
|
||||
// the iv is err
|
||||
errDecrypt, err = gdes.DecryptCBC(cipherText, key, errIv, padding)
|
||||
gtest.AssertNE(err, nil)
|
||||
gtest.AssertEQ(errDecrypt, nil)
|
||||
t.AssertNE(err, nil)
|
||||
t.AssertEQ(errDecrypt, nil)
|
||||
// the padding is err
|
||||
errDecrypt, err = gdes.DecryptCBC(cipherText, key, iv, errPadding)
|
||||
gtest.AssertNE(err, nil)
|
||||
gtest.AssertEQ(errDecrypt, nil)
|
||||
t.AssertNE(err, nil)
|
||||
t.AssertEQ(errDecrypt, nil)
|
||||
})
|
||||
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
key := []byte("11111111")
|
||||
text := []byte("12345678")
|
||||
padding := gdes.PKCS5PADDING
|
||||
@ -173,21 +173,21 @@ func TestDesCBC(t *testing.T) {
|
||||
result := "40826a5800608c87100a25d86ac7c52c"
|
||||
// encrypt test
|
||||
cipherText, err := gdes.EncryptCBC(text, key, iv, padding)
|
||||
gtest.AssertEQ(err, nil)
|
||||
gtest.AssertEQ(hex.EncodeToString(cipherText), result)
|
||||
t.AssertEQ(err, nil)
|
||||
t.AssertEQ(hex.EncodeToString(cipherText), result)
|
||||
// decrypt test
|
||||
clearText, err := gdes.DecryptCBC(cipherText, key, iv, padding)
|
||||
gtest.AssertEQ(err, nil)
|
||||
gtest.AssertEQ(string(clearText), "12345678")
|
||||
t.AssertEQ(err, nil)
|
||||
t.AssertEQ(string(clearText), "12345678")
|
||||
// err test
|
||||
errEncrypt, err := gdes.EncryptCBC(text, key, errIv, padding)
|
||||
gtest.AssertNE(err, nil)
|
||||
gtest.AssertEQ(errEncrypt, nil)
|
||||
t.AssertNE(err, nil)
|
||||
t.AssertEQ(errEncrypt, nil)
|
||||
})
|
||||
}
|
||||
|
||||
func Test3DesCBC(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
key := []byte("1111111112345678")
|
||||
text := []byte("1234567812345678")
|
||||
padding := gdes.NOPADDING
|
||||
@ -195,38 +195,38 @@ func Test3DesCBC(t *testing.T) {
|
||||
result := "bfde1394e265d5f738d5cab170c77c88"
|
||||
// encrypt test
|
||||
cipherText, err := gdes.EncryptCBCTriple(text, key, iv, padding)
|
||||
gtest.AssertEQ(err, nil)
|
||||
gtest.AssertEQ(hex.EncodeToString(cipherText), result)
|
||||
t.AssertEQ(err, nil)
|
||||
t.AssertEQ(hex.EncodeToString(cipherText), result)
|
||||
// decrypt test
|
||||
clearText, err := gdes.DecryptCBCTriple(cipherText, key, iv, padding)
|
||||
gtest.AssertEQ(err, nil)
|
||||
gtest.AssertEQ(string(clearText), "1234567812345678")
|
||||
t.AssertEQ(err, nil)
|
||||
t.AssertEQ(string(clearText), "1234567812345678")
|
||||
// encrypt err test
|
||||
errEncrypt, err := gdes.EncryptCBCTriple(text, errKey, iv, padding)
|
||||
gtest.AssertNE(err, nil)
|
||||
gtest.AssertEQ(errEncrypt, nil)
|
||||
t.AssertNE(err, nil)
|
||||
t.AssertEQ(errEncrypt, nil)
|
||||
// the iv is err
|
||||
errEncrypt, err = gdes.EncryptCBCTriple(text, key, errIv, padding)
|
||||
gtest.AssertNE(err, nil)
|
||||
gtest.AssertEQ(errEncrypt, nil)
|
||||
t.AssertNE(err, nil)
|
||||
t.AssertEQ(errEncrypt, nil)
|
||||
// the padding is err
|
||||
errEncrypt, err = gdes.EncryptCBCTriple(text, key, iv, errPadding)
|
||||
gtest.AssertNE(err, nil)
|
||||
gtest.AssertEQ(errEncrypt, nil)
|
||||
t.AssertNE(err, nil)
|
||||
t.AssertEQ(errEncrypt, nil)
|
||||
// decrypt err test
|
||||
errDecrypt, err := gdes.DecryptCBCTriple(cipherText, errKey, iv, padding)
|
||||
gtest.AssertNE(err, nil)
|
||||
gtest.AssertEQ(errDecrypt, nil)
|
||||
t.AssertNE(err, nil)
|
||||
t.AssertEQ(errDecrypt, nil)
|
||||
// the iv is err
|
||||
errDecrypt, err = gdes.DecryptCBCTriple(cipherText, key, errIv, padding)
|
||||
gtest.AssertNE(err, nil)
|
||||
gtest.AssertEQ(errDecrypt, nil)
|
||||
t.AssertNE(err, nil)
|
||||
t.AssertEQ(errDecrypt, nil)
|
||||
// the padding is err
|
||||
errDecrypt, err = gdes.DecryptCBCTriple(cipherText, key, iv, errPadding)
|
||||
gtest.AssertNE(err, nil)
|
||||
gtest.AssertEQ(errDecrypt, nil)
|
||||
t.AssertNE(err, nil)
|
||||
t.AssertEQ(errDecrypt, nil)
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
key := []byte("111111111234567812345678")
|
||||
text := []byte("12345678")
|
||||
padding := gdes.PKCS5PADDING
|
||||
@ -234,12 +234,12 @@ func Test3DesCBC(t *testing.T) {
|
||||
result := "40826a5800608c87100a25d86ac7c52c"
|
||||
// encrypt test
|
||||
cipherText, err := gdes.EncryptCBCTriple(text, key, iv, padding)
|
||||
gtest.AssertEQ(err, nil)
|
||||
gtest.AssertEQ(hex.EncodeToString(cipherText), result)
|
||||
t.AssertEQ(err, nil)
|
||||
t.AssertEQ(hex.EncodeToString(cipherText), result)
|
||||
// decrypt test
|
||||
clearText, err := gdes.DecryptCBCTriple(cipherText, key, iv, padding)
|
||||
gtest.AssertEQ(err, nil)
|
||||
gtest.AssertEQ(string(clearText), "12345678")
|
||||
t.AssertEQ(err, nil)
|
||||
t.AssertEQ(string(clearText), "12345678")
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
@ -29,16 +29,16 @@ type user struct {
|
||||
}
|
||||
|
||||
func TestEncrypt(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
encryptString, _ := gmd5.Encrypt(s)
|
||||
gtest.Assert(encryptString, result)
|
||||
t.Assert(encryptString, result)
|
||||
|
||||
result := "1427562bb29f88a1161590b76398ab72"
|
||||
encrypt, _ := gmd5.Encrypt(123456)
|
||||
gtest.AssertEQ(encrypt, result)
|
||||
t.AssertEQ(encrypt, result)
|
||||
})
|
||||
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
user := &user{
|
||||
name: "派大星",
|
||||
password: "123456",
|
||||
@ -46,14 +46,14 @@ func TestEncrypt(t *testing.T) {
|
||||
}
|
||||
result := "70917ebce8bd2f78c736cda63870fb39"
|
||||
encrypt, _ := gmd5.Encrypt(user)
|
||||
gtest.AssertEQ(encrypt, result)
|
||||
t.AssertEQ(encrypt, result)
|
||||
})
|
||||
}
|
||||
|
||||
func TestEncryptString(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
encryptString, _ := gmd5.EncryptString(s)
|
||||
gtest.Assert(encryptString, result)
|
||||
t.Assert(encryptString, result)
|
||||
})
|
||||
}
|
||||
|
||||
@ -61,17 +61,17 @@ func TestEncryptFile(t *testing.T) {
|
||||
path := "test.text"
|
||||
errorPath := "err.txt"
|
||||
result := "e6e6e1cd41895beebff16d5452dfce12"
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
file, err := os.Create(path)
|
||||
defer os.Remove(path)
|
||||
defer file.Close()
|
||||
gtest.Assert(err, nil)
|
||||
t.Assert(err, nil)
|
||||
_, _ = file.Write([]byte("Hello Go Frame"))
|
||||
encryptFile, _ := gmd5.EncryptFile(path)
|
||||
gtest.AssertEQ(encryptFile, result)
|
||||
t.AssertEQ(encryptFile, result)
|
||||
// when the file is not exist,encrypt will return empty string
|
||||
errEncrypt, _ := gmd5.EncryptFile(errorPath)
|
||||
gtest.AssertEQ(errEncrypt, "")
|
||||
t.AssertEQ(errEncrypt, "")
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
@ -23,7 +23,7 @@ type user struct {
|
||||
}
|
||||
|
||||
func TestEncrypt(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
user := &user{
|
||||
name: "派大星",
|
||||
password: "123456",
|
||||
@ -31,29 +31,29 @@ func TestEncrypt(t *testing.T) {
|
||||
}
|
||||
result := "97386736e3ee4adee5ca595c78c12129f6032cad"
|
||||
encrypt := gsha1.Encrypt(user)
|
||||
gtest.AssertEQ(encrypt, result)
|
||||
t.AssertEQ(encrypt, result)
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
result := "5b4c1c2a08ca85ddd031ef8627414f4cb2620b41"
|
||||
s := gsha1.Encrypt("pibigstar")
|
||||
gtest.AssertEQ(s, result)
|
||||
t.AssertEQ(s, result)
|
||||
})
|
||||
}
|
||||
|
||||
func TestEncryptFile(t *testing.T) {
|
||||
path := "test.text"
|
||||
errPath := "err.text"
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
result := "8b05d3ba24b8d2374b8f5149d9f3fbada14ea984"
|
||||
file, err := os.Create(path)
|
||||
defer os.Remove(path)
|
||||
defer file.Close()
|
||||
gtest.Assert(err, nil)
|
||||
t.Assert(err, nil)
|
||||
_, _ = file.Write([]byte("Hello Go Frame"))
|
||||
encryptFile, _ := gsha1.EncryptFile(path)
|
||||
gtest.AssertEQ(encryptFile, result)
|
||||
t.AssertEQ(encryptFile, result)
|
||||
// when the file is not exist,encrypt will return empty string
|
||||
errEncrypt, _ := gsha1.EncryptFile(errPath)
|
||||
gtest.AssertEQ(errEncrypt, "")
|
||||
t.AssertEQ(errEncrypt, "")
|
||||
})
|
||||
}
|
||||
|
||||
@ -11,6 +11,7 @@ import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/internal/intlog"
|
||||
"time"
|
||||
|
||||
"github.com/gogf/gf/os/glog"
|
||||
@ -29,29 +30,29 @@ type DB interface {
|
||||
Open(config *ConfigNode) (*sql.DB, error)
|
||||
|
||||
// Query APIs.
|
||||
Query(query string, args ...interface{}) (*sql.Rows, error)
|
||||
Query(sql string, args ...interface{}) (*sql.Rows, error)
|
||||
Exec(sql string, args ...interface{}) (sql.Result, error)
|
||||
Prepare(sql string, execOnMaster ...bool) (*sql.Stmt, error)
|
||||
|
||||
// Internal APIs for CURD, which can be overwrote for custom CURD implements.
|
||||
DoQuery(link Link, query string, args ...interface{}) (rows *sql.Rows, err error)
|
||||
DoGetAll(link Link, query string, args ...interface{}) (result Result, err error)
|
||||
DoExec(link Link, query string, args ...interface{}) (result sql.Result, err error)
|
||||
DoPrepare(link Link, query string) (*sql.Stmt, error)
|
||||
DoQuery(link Link, sql string, args ...interface{}) (rows *sql.Rows, err error)
|
||||
DoGetAll(link Link, sql string, args ...interface{}) (result Result, err error)
|
||||
DoExec(link Link, sql string, args ...interface{}) (result sql.Result, err error)
|
||||
DoPrepare(link Link, sql string) (*sql.Stmt, error)
|
||||
DoInsert(link Link, table string, data interface{}, option int, batch ...int) (result sql.Result, err error)
|
||||
DoBatchInsert(link Link, table string, list interface{}, option int, batch ...int) (result sql.Result, err error)
|
||||
DoUpdate(link Link, table string, data interface{}, condition string, args ...interface{}) (result sql.Result, err error)
|
||||
DoDelete(link Link, table string, condition string, args ...interface{}) (result sql.Result, err error)
|
||||
|
||||
// Query APIs for convenience purpose.
|
||||
GetAll(query string, args ...interface{}) (Result, error)
|
||||
GetOne(query string, args ...interface{}) (Record, error)
|
||||
GetValue(query string, args ...interface{}) (Value, error)
|
||||
GetArray(query string, args ...interface{}) ([]Value, error)
|
||||
GetCount(query string, args ...interface{}) (int, error)
|
||||
GetStruct(objPointer interface{}, query string, args ...interface{}) error
|
||||
GetStructs(objPointerSlice interface{}, query string, args ...interface{}) error
|
||||
GetScan(objPointer interface{}, query string, args ...interface{}) error
|
||||
GetAll(sql string, args ...interface{}) (Result, error)
|
||||
GetOne(sql string, args ...interface{}) (Record, error)
|
||||
GetValue(sql string, args ...interface{}) (Value, error)
|
||||
GetArray(sql string, args ...interface{}) ([]Value, error)
|
||||
GetCount(sql string, args ...interface{}) (int, error)
|
||||
GetStruct(objPointer interface{}, sql string, args ...interface{}) error
|
||||
GetStructs(objPointerSlice interface{}, sql string, args ...interface{}) error
|
||||
GetScan(objPointer interface{}, sql string, args ...interface{}) error
|
||||
|
||||
// Master/Slave specification support.
|
||||
Master() (*sql.DB, error)
|
||||
@ -106,9 +107,9 @@ type DB interface {
|
||||
|
||||
// HandleSqlBeforeCommit is a hook function, which deals with the sql string before
|
||||
// it's committed to underlying driver. The parameter <link> specifies the current
|
||||
// database connection operation object. You can modify the sql string <query> and its
|
||||
// database connection operation object. You can modify the sql string <sql> and its
|
||||
// arguments <args> as you wish before they're committed to driver.
|
||||
HandleSqlBeforeCommit(link Link, query string, args []interface{}) (string, []interface{})
|
||||
HandleSqlBeforeCommit(link Link, sql string, args []interface{}) (string, []interface{})
|
||||
|
||||
// Internal methods.
|
||||
filterFields(schema, table string, data map[string]interface{}) map[string]interface{}
|
||||
@ -160,7 +161,7 @@ type TableField struct {
|
||||
|
||||
// Link is a common database function wrapper interface.
|
||||
type Link interface {
|
||||
Query(query string, args ...interface{}) (*sql.Rows, error)
|
||||
Query(sql string, args ...interface{}) (*sql.Rows, error)
|
||||
Exec(sql string, args ...interface{}) (sql.Result, error)
|
||||
Prepare(sql string) (*sql.Stmt, error)
|
||||
}
|
||||
@ -182,15 +183,18 @@ type Map = map[string]interface{}
|
||||
type List = []Map
|
||||
|
||||
const (
|
||||
gINSERT_OPTION_DEFAULT = 0
|
||||
gINSERT_OPTION_REPLACE = 1
|
||||
gINSERT_OPTION_SAVE = 2
|
||||
gINSERT_OPTION_IGNORE = 3
|
||||
gDEFAULT_BATCH_NUM = 10 // Per count for batch insert/replace/save
|
||||
gDEFAULT_CONN_MAX_LIFE_TIME = 30 // Max life time for per connection in pool in seconds.
|
||||
gINSERT_OPTION_DEFAULT = 0
|
||||
gINSERT_OPTION_REPLACE = 1
|
||||
gINSERT_OPTION_SAVE = 2
|
||||
gINSERT_OPTION_IGNORE = 3
|
||||
gDEFAULT_BATCH_NUM = 10 // Per count for batch insert/replace/save
|
||||
gDEFAULT_CONN_MAX_IDLE_COUNT = 10 // Max idle connection count in pool.
|
||||
gDEFAULT_CONN_MAX_LIFE_TIME = 30 // Max life time for per connection in pool in seconds.
|
||||
)
|
||||
|
||||
var (
|
||||
// ErrNoRows is alias of sql.ErrNoRows.
|
||||
ErrNoRows = sql.ErrNoRows
|
||||
// instances is the management map for instances.
|
||||
instances = gmap.NewStrAnyMap(true)
|
||||
// driverMap manages all custom registered driver.
|
||||
@ -226,13 +230,14 @@ func New(name ...string) (db DB, err error) {
|
||||
if _, ok := configs.config[group]; ok {
|
||||
if node, err := getConfigNodeByGroup(group, true); err == nil {
|
||||
c := &Core{
|
||||
group: group,
|
||||
debug: gtype.NewBool(),
|
||||
cache: gcache.New(),
|
||||
schema: gtype.NewString(),
|
||||
logger: glog.New(),
|
||||
prefix: node.Prefix,
|
||||
maxConnLifetime: gDEFAULT_CONN_MAX_LIFE_TIME, // Default max connection life time if user does not configure.
|
||||
group: group,
|
||||
debug: gtype.NewBool(),
|
||||
cache: gcache.New(),
|
||||
schema: gtype.NewString(),
|
||||
logger: glog.New(),
|
||||
prefix: node.Prefix,
|
||||
maxIdleConnCount: gDEFAULT_CONN_MAX_IDLE_COUNT,
|
||||
maxConnLifetime: gDEFAULT_CONN_MAX_LIFE_TIME, // Default max connection life time if user does not configure.
|
||||
}
|
||||
if v, ok := driverMap[node.Type]; ok {
|
||||
c.DB, err = v.New(c, node)
|
||||
@ -364,10 +369,11 @@ func (c *Core) getSqlDb(master bool, schema ...string) (sqlDb *sql.DB, err error
|
||||
n.Name = nodeSchema
|
||||
node = &n
|
||||
}
|
||||
// Cache the underlying connection object by node.
|
||||
// Cache the underlying connection pool object by node.
|
||||
v := c.cache.GetOrSetFuncLock(node.String(), func() interface{} {
|
||||
sqlDb, err = c.DB.Open(node)
|
||||
if err != nil {
|
||||
intlog.Printf("DB open failed: %v, %+v", err, node)
|
||||
return nil
|
||||
}
|
||||
if c.maxIdleConnCount > 0 {
|
||||
|
||||
@ -1,25 +0,0 @@
|
||||
// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
// You can obtain one at https://github.com/gogf/gf.
|
||||
|
||||
package gdb
|
||||
|
||||
import "database/sql"
|
||||
|
||||
// batchSqlResult is execution result for batch operations.
|
||||
type batchSqlResult struct {
|
||||
rowsAffected int64
|
||||
lastResult sql.Result
|
||||
}
|
||||
|
||||
// see sql.Result.RowsAffected
|
||||
func (r *batchSqlResult) RowsAffected() (int64, error) {
|
||||
return r.rowsAffected, nil
|
||||
}
|
||||
|
||||
// see sql.Result.LastInsertId
|
||||
func (r *batchSqlResult) LastInsertId() (int64, error) {
|
||||
return r.lastResult.LastInsertId()
|
||||
}
|
||||
@ -45,75 +45,75 @@ func (c *Core) Slave() (*sql.DB, error) {
|
||||
|
||||
// Query commits one query SQL to underlying driver and returns the execution result.
|
||||
// It is most commonly used for data querying.
|
||||
func (c *Core) Query(query string, args ...interface{}) (rows *sql.Rows, err error) {
|
||||
func (c *Core) Query(sql string, args ...interface{}) (rows *sql.Rows, err error) {
|
||||
link, err := c.DB.Slave()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return c.DB.DoQuery(link, query, args...)
|
||||
return c.DB.DoQuery(link, sql, args...)
|
||||
}
|
||||
|
||||
// doQuery commits the query string and its arguments to underlying driver
|
||||
// DoQuery commits the sql string and its arguments to underlying driver
|
||||
// through given link object and returns the execution result.
|
||||
func (c *Core) DoQuery(link Link, query string, args ...interface{}) (rows *sql.Rows, err error) {
|
||||
query, args = formatQuery(query, args)
|
||||
query, args = c.DB.HandleSqlBeforeCommit(link, query, args)
|
||||
func (c *Core) DoQuery(link Link, sql string, args ...interface{}) (rows *sql.Rows, err error) {
|
||||
sql, args = formatSql(sql, args)
|
||||
sql, args = c.DB.HandleSqlBeforeCommit(link, sql, args)
|
||||
if c.DB.GetDebug() {
|
||||
mTime1 := gtime.TimestampMilli()
|
||||
rows, err = link.Query(query, args...)
|
||||
rows, err = link.Query(sql, args...)
|
||||
mTime2 := gtime.TimestampMilli()
|
||||
s := &Sql{
|
||||
Sql: query,
|
||||
Sql: sql,
|
||||
Args: args,
|
||||
Format: bindArgsToQuery(query, args),
|
||||
Format: FormatSqlWithArgs(sql, args),
|
||||
Error: err,
|
||||
Start: mTime1,
|
||||
End: mTime2,
|
||||
}
|
||||
c.writeSqlToLogger(s)
|
||||
} else {
|
||||
rows, err = link.Query(query, args...)
|
||||
rows, err = link.Query(sql, args...)
|
||||
}
|
||||
if err == nil {
|
||||
return rows, nil
|
||||
} else {
|
||||
err = formatError(err, query, args...)
|
||||
err = formatError(err, sql, args...)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Exec commits one query SQL to underlying driver and returns the execution result.
|
||||
// It is most commonly used for data inserting and updating.
|
||||
func (c *Core) Exec(query string, args ...interface{}) (result sql.Result, err error) {
|
||||
func (c *Core) Exec(sql string, args ...interface{}) (result sql.Result, err error) {
|
||||
link, err := c.DB.Master()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return c.DB.DoExec(link, query, args...)
|
||||
return c.DB.DoExec(link, sql, args...)
|
||||
}
|
||||
|
||||
// doExec commits the query string and its arguments to underlying driver
|
||||
// DoExec commits the sql string and its arguments to underlying driver
|
||||
// through given link object and returns the execution result.
|
||||
func (c *Core) DoExec(link Link, query string, args ...interface{}) (result sql.Result, err error) {
|
||||
query, args = formatQuery(query, args)
|
||||
query, args = c.DB.HandleSqlBeforeCommit(link, query, args)
|
||||
func (c *Core) DoExec(link Link, sql string, args ...interface{}) (result sql.Result, err error) {
|
||||
sql, args = formatSql(sql, args)
|
||||
sql, args = c.DB.HandleSqlBeforeCommit(link, sql, args)
|
||||
if c.DB.GetDebug() {
|
||||
mTime1 := gtime.TimestampMilli()
|
||||
result, err = link.Exec(query, args...)
|
||||
result, err = link.Exec(sql, args...)
|
||||
mTime2 := gtime.TimestampMilli()
|
||||
s := &Sql{
|
||||
Sql: query,
|
||||
Sql: sql,
|
||||
Args: args,
|
||||
Format: bindArgsToQuery(query, args),
|
||||
Format: FormatSqlWithArgs(sql, args),
|
||||
Error: err,
|
||||
Start: mTime1,
|
||||
End: mTime2,
|
||||
}
|
||||
c.writeSqlToLogger(s)
|
||||
} else {
|
||||
result, err = link.Exec(query, args...)
|
||||
result, err = link.Exec(sql, args...)
|
||||
}
|
||||
return result, formatError(err, query, args...)
|
||||
return result, formatError(err, sql, args...)
|
||||
}
|
||||
|
||||
// Prepare creates a prepared statement for later queries or executions.
|
||||
@ -124,7 +124,7 @@ func (c *Core) DoExec(link Link, query string, args ...interface{}) (result sql.
|
||||
//
|
||||
// The parameter <execOnMaster> specifies whether executing the sql on master node,
|
||||
// or else it executes the sql on slave node if master-slave configured.
|
||||
func (c *Core) Prepare(query string, execOnMaster ...bool) (*sql.Stmt, error) {
|
||||
func (c *Core) Prepare(sql string, execOnMaster ...bool) (*sql.Stmt, error) {
|
||||
err := (error)(nil)
|
||||
link := (Link)(nil)
|
||||
if len(execOnMaster) > 0 && execOnMaster[0] {
|
||||
@ -136,28 +136,28 @@ func (c *Core) Prepare(query string, execOnMaster ...bool) (*sql.Stmt, error) {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return c.DB.DoPrepare(link, query)
|
||||
return c.DB.DoPrepare(link, sql)
|
||||
}
|
||||
|
||||
// doPrepare calls prepare function on given link object and returns the statement object.
|
||||
func (c *Core) DoPrepare(link Link, query string) (*sql.Stmt, error) {
|
||||
return link.Prepare(query)
|
||||
func (c *Core) DoPrepare(link Link, sql string) (*sql.Stmt, error) {
|
||||
return link.Prepare(sql)
|
||||
}
|
||||
|
||||
// GetAll queries and returns data records from database.
|
||||
func (c *Core) GetAll(query string, args ...interface{}) (Result, error) {
|
||||
return c.DB.DoGetAll(nil, query, args...)
|
||||
func (c *Core) GetAll(sql string, args ...interface{}) (Result, error) {
|
||||
return c.DB.DoGetAll(nil, sql, args...)
|
||||
}
|
||||
|
||||
// doGetAll queries and returns data records from database.
|
||||
func (c *Core) DoGetAll(link Link, query string, args ...interface{}) (result Result, err error) {
|
||||
func (c *Core) DoGetAll(link Link, sql string, args ...interface{}) (result Result, err error) {
|
||||
if link == nil {
|
||||
link, err = c.DB.Slave()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
rows, err := c.DB.DoQuery(link, query, args...)
|
||||
rows, err := c.DB.DoQuery(link, sql, args...)
|
||||
if err != nil || rows == nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -166,8 +166,8 @@ func (c *Core) DoGetAll(link Link, query string, args ...interface{}) (result Re
|
||||
}
|
||||
|
||||
// GetOne queries and returns one record from database.
|
||||
func (c *Core) GetOne(query string, args ...interface{}) (Record, error) {
|
||||
list, err := c.DB.GetAll(query, args...)
|
||||
func (c *Core) GetOne(sql string, args ...interface{}) (Record, error) {
|
||||
list, err := c.DB.GetAll(sql, args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -179,8 +179,8 @@ func (c *Core) GetOne(query string, args ...interface{}) (Record, error) {
|
||||
|
||||
// GetArray queries and returns data values as slice from database.
|
||||
// Note that if there're multiple columns in the result, it returns just one column values randomly.
|
||||
func (c *Core) GetArray(query string, args ...interface{}) ([]Value, error) {
|
||||
all, err := c.DB.DoGetAll(nil, query, args...)
|
||||
func (c *Core) GetArray(sql string, args ...interface{}) ([]Value, error) {
|
||||
all, err := c.DB.DoGetAll(nil, sql, args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -189,26 +189,26 @@ func (c *Core) GetArray(query string, args ...interface{}) ([]Value, error) {
|
||||
|
||||
// GetStruct queries one record from database and converts it to given struct.
|
||||
// The parameter <pointer> should be a pointer to struct.
|
||||
func (c *Core) GetStruct(pointer interface{}, query string, args ...interface{}) error {
|
||||
one, err := c.DB.GetOne(query, args...)
|
||||
func (c *Core) GetStruct(pointer interface{}, sql string, args ...interface{}) error {
|
||||
one, err := c.DB.GetOne(sql, args...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(one) == 0 {
|
||||
return sql.ErrNoRows
|
||||
return ErrNoRows
|
||||
}
|
||||
return one.Struct(pointer)
|
||||
}
|
||||
|
||||
// GetStructs queries records from database and converts them to given struct.
|
||||
// The parameter <pointer> should be type of struct slice: []struct/[]*struct.
|
||||
func (c *Core) GetStructs(pointer interface{}, query string, args ...interface{}) error {
|
||||
all, err := c.DB.GetAll(query, args...)
|
||||
func (c *Core) GetStructs(pointer interface{}, sql string, args ...interface{}) error {
|
||||
all, err := c.DB.GetAll(sql, args...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(all) == 0 {
|
||||
return sql.ErrNoRows
|
||||
return ErrNoRows
|
||||
}
|
||||
return all.Structs(pointer)
|
||||
}
|
||||
@ -219,7 +219,7 @@ func (c *Core) GetStructs(pointer interface{}, query string, args ...interface{}
|
||||
// If parameter <pointer> is type of struct pointer, it calls GetStruct internally for
|
||||
// the conversion. If parameter <pointer> is type of slice, it calls GetStructs internally
|
||||
// for conversion.
|
||||
func (c *Core) GetScan(pointer interface{}, query string, args ...interface{}) error {
|
||||
func (c *Core) GetScan(pointer interface{}, sql string, args ...interface{}) error {
|
||||
t := reflect.TypeOf(pointer)
|
||||
k := t.Kind()
|
||||
if k != reflect.Ptr {
|
||||
@ -228,9 +228,9 @@ func (c *Core) GetScan(pointer interface{}, query string, args ...interface{}) e
|
||||
k = t.Elem().Kind()
|
||||
switch k {
|
||||
case reflect.Array, reflect.Slice:
|
||||
return c.DB.GetStructs(pointer, query, args...)
|
||||
return c.DB.GetStructs(pointer, sql, args...)
|
||||
case reflect.Struct:
|
||||
return c.DB.GetStruct(pointer, query, args...)
|
||||
return c.DB.GetStruct(pointer, sql, args...)
|
||||
}
|
||||
return fmt.Errorf("element type should be type of struct/slice, unsupported: %v", k)
|
||||
}
|
||||
@ -238,8 +238,8 @@ func (c *Core) GetScan(pointer interface{}, query string, args ...interface{}) e
|
||||
// GetValue queries and returns the field value from database.
|
||||
// The sql should queries only one field from database, or else it returns only one
|
||||
// field of the result.
|
||||
func (c *Core) GetValue(query string, args ...interface{}) (Value, error) {
|
||||
one, err := c.DB.GetOne(query, args...)
|
||||
func (c *Core) GetValue(sql string, args ...interface{}) (Value, error) {
|
||||
one, err := c.DB.GetOne(sql, args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -250,13 +250,13 @@ func (c *Core) GetValue(query string, args ...interface{}) (Value, error) {
|
||||
}
|
||||
|
||||
// GetCount queries and returns the count from database.
|
||||
func (c *Core) GetCount(query string, args ...interface{}) (int, error) {
|
||||
func (c *Core) GetCount(sql string, args ...interface{}) (int, error) {
|
||||
// If the query fields do not contains function "COUNT",
|
||||
// it replaces the query string and adds the "COUNT" function to the fields.
|
||||
if !gregex.IsMatchString(`(?i)SELECT\s+COUNT\(.+\)\s+FROM`, query) {
|
||||
query, _ = gregex.ReplaceString(`(?i)(SELECT)\s+(.+)\s+(FROM)`, `$1 COUNT($2) $3`, query)
|
||||
// it replaces the sql string and adds the "COUNT" function to the fields.
|
||||
if !gregex.IsMatchString(`(?i)SELECT\s+COUNT\(.+\)\s+FROM`, sql) {
|
||||
sql, _ = gregex.ReplaceString(`(?i)(SELECT)\s+(.+)\s+(FROM)`, `$1 COUNT($2) $3`, sql)
|
||||
}
|
||||
value, err := c.DB.GetValue(query, args...)
|
||||
value, err := c.DB.GetValue(sql, args...)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
@ -504,7 +504,7 @@ func (c *Core) DoBatchInsert(link Link, table string, list interface{}, option i
|
||||
holders = append(holders, "?")
|
||||
}
|
||||
// Prepare the batch result pointer.
|
||||
batchResult := new(batchSqlResult)
|
||||
batchResult := new(SqlResult)
|
||||
charL, charR := c.DB.GetChars()
|
||||
keysStr := charL + strings.Join(keys, charR+","+charL) + charR
|
||||
valueHolderStr := "(" + strings.Join(holders, ",") + ")"
|
||||
@ -555,8 +555,8 @@ func (c *Core) DoBatchInsert(link Link, table string, list interface{}, option i
|
||||
if n, err := r.RowsAffected(); err != nil {
|
||||
return r, err
|
||||
} else {
|
||||
batchResult.lastResult = r
|
||||
batchResult.rowsAffected += n
|
||||
batchResult.result = r
|
||||
batchResult.affected += n
|
||||
}
|
||||
params = params[:0]
|
||||
values = values[:0]
|
||||
@ -721,7 +721,7 @@ func (c *Core) MarshalJSON() ([]byte, error) {
|
||||
// writeSqlToLogger outputs the sql object to logger.
|
||||
// It is enabled when configuration "debug" is true.
|
||||
func (c *Core) writeSqlToLogger(v *Sql) {
|
||||
s := fmt.Sprintf("[%d ms] %s", v.End-v.Start, v.Format)
|
||||
s := fmt.Sprintf("[%3d ms] %s", v.End-v.Start, v.Format)
|
||||
if v.Error != nil {
|
||||
s += "\nError: " + v.Error.Error()
|
||||
c.logger.StackWithFilter(gPATH_FILTER_KEY).Error(s)
|
||||
|
||||
@ -60,10 +60,10 @@ func (d *DriverMssql) GetChars() (charLeft string, charRight string) {
|
||||
}
|
||||
|
||||
// HandleSqlBeforeCommit deals with the sql string before commits it to underlying sql driver.
|
||||
func (d *DriverMssql) HandleSqlBeforeCommit(link Link, query string, args []interface{}) (string, []interface{}) {
|
||||
func (d *DriverMssql) HandleSqlBeforeCommit(link Link, sql string, args []interface{}) (string, []interface{}) {
|
||||
var index int
|
||||
// Convert place holder char '?' to string "@px".
|
||||
str, _ := gregex.ReplaceStringFunc("\\?", query, func(s string) string {
|
||||
str, _ := gregex.ReplaceStringFunc("\\?", sql, func(s string) string {
|
||||
index++
|
||||
return fmt.Sprintf("@p%d", index)
|
||||
})
|
||||
@ -71,6 +71,8 @@ func (d *DriverMssql) HandleSqlBeforeCommit(link Link, query string, args []inte
|
||||
return d.parseSql(str), args
|
||||
}
|
||||
|
||||
// parseSql does some replacement of the sql before commits it to underlying driver,
|
||||
// for support of microsoft sql server.
|
||||
func (d *DriverMssql) parseSql(sql string) string {
|
||||
// SELECT * FROM USER WHERE ID=1 LIMIT 1
|
||||
if m, _ := gregex.MatchString(`^SELECT(.+)LIMIT 1$`, sql); len(m) > 1 {
|
||||
@ -91,22 +93,20 @@ func (d *DriverMssql) parseSql(sql string) string {
|
||||
index++
|
||||
switch keyword {
|
||||
case "SELECT":
|
||||
// 不含LIMIT关键字则不处理
|
||||
// LIMIT statement checks.
|
||||
if len(res) < 2 ||
|
||||
(strings.HasPrefix(res[index][0], "LIMIT") == false &&
|
||||
strings.HasPrefix(res[index][0], "limit") == false) {
|
||||
break
|
||||
}
|
||||
// 不含LIMIT则不处理
|
||||
if gregex.IsMatchString("((?i)SELECT)(.+)((?i)LIMIT)", sql) == false {
|
||||
break
|
||||
}
|
||||
// 判断SQL中是否含有order by
|
||||
// ORDER BY statement checks.
|
||||
selectStr := ""
|
||||
orderStr := ""
|
||||
haveOrder := gregex.IsMatchString("((?i)SELECT)(.+)((?i)ORDER BY)", sql)
|
||||
if haveOrder {
|
||||
// 取order by 前面的字符串
|
||||
queryExpr, _ := gregex.MatchString("((?i)SELECT)(.+)((?i)ORDER BY)", sql)
|
||||
if len(queryExpr) != 4 ||
|
||||
strings.EqualFold(queryExpr[1], "SELECT") == false ||
|
||||
@ -114,8 +114,6 @@ func (d *DriverMssql) parseSql(sql string) string {
|
||||
break
|
||||
}
|
||||
selectStr = queryExpr[2]
|
||||
|
||||
// 取order by表达式的值
|
||||
orderExpr, _ := gregex.MatchString("((?i)ORDER BY)(.+)((?i)LIMIT)", sql)
|
||||
if len(orderExpr) != 4 ||
|
||||
strings.EqualFold(orderExpr[1], "ORDER BY") == false ||
|
||||
@ -132,8 +130,6 @@ func (d *DriverMssql) parseSql(sql string) string {
|
||||
}
|
||||
selectStr = queryExpr[2]
|
||||
}
|
||||
|
||||
// 取limit后面的取值范围
|
||||
first, limit := 0, 0
|
||||
for i := 1; i < len(res[index]); i++ {
|
||||
if len(strings.TrimSpace(res[index][i])) == 0 {
|
||||
@ -147,23 +143,20 @@ func (d *DriverMssql) parseSql(sql string) string {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if haveOrder {
|
||||
sql = fmt.Sprintf(
|
||||
"SELECT * FROM "+
|
||||
"(SELECT ROW_NUMBER() OVER (ORDER BY %s) as ROWNUMBER_, %s ) as TMP_ "+
|
||||
"WHERE TMP_.ROWNUMBER_ > %d AND TMP_.ROWNUMBER_ <= %d",
|
||||
orderStr, selectStr, first, limit,
|
||||
orderStr, selectStr, first, first+limit,
|
||||
)
|
||||
} else {
|
||||
if first == 0 {
|
||||
first = limit
|
||||
} else {
|
||||
first = limit - first
|
||||
}
|
||||
sql = fmt.Sprintf(
|
||||
"SELECT * FROM (SELECT TOP %d * FROM (SELECT TOP %d %s) as TMP1_ ) as TMP2_ ",
|
||||
first, limit, selectStr,
|
||||
limit, first+limit, selectStr,
|
||||
)
|
||||
}
|
||||
default:
|
||||
|
||||
@ -64,10 +64,10 @@ func (d *DriverOracle) GetChars() (charLeft string, charRight string) {
|
||||
}
|
||||
|
||||
// HandleSqlBeforeCommit deals with the sql string before commits it to underlying sql driver.
|
||||
func (d *DriverOracle) HandleSqlBeforeCommit(link Link, query string, args []interface{}) (string, []interface{}) {
|
||||
func (d *DriverOracle) HandleSqlBeforeCommit(link Link, sql string, args []interface{}) (string, []interface{}) {
|
||||
var index int
|
||||
// Convert place holder char '?' to string ":x".
|
||||
str, _ := gregex.ReplaceStringFunc("\\?", query, func(s string) string {
|
||||
str, _ := gregex.ReplaceStringFunc("\\?", sql, func(s string) string {
|
||||
index++
|
||||
return fmt.Sprintf(":%d", index)
|
||||
})
|
||||
@ -75,6 +75,8 @@ func (d *DriverOracle) HandleSqlBeforeCommit(link Link, query string, args []int
|
||||
return d.parseSql(str), args
|
||||
}
|
||||
|
||||
// parseSql does some replacement of the sql before commits it to underlying driver,
|
||||
// for support of oracle server.
|
||||
func (d *DriverOracle) parseSql(sql string) string {
|
||||
patten := `^\s*(?i)(SELECT)|(LIMIT\s*(\d+)\s*,\s*(\d+))`
|
||||
if gregex.IsMatchString(patten, sql) == false {
|
||||
@ -93,22 +95,18 @@ func (d *DriverOracle) parseSql(sql string) string {
|
||||
index++
|
||||
switch keyword {
|
||||
case "SELECT":
|
||||
// 不含LIMIT关键字则不处理
|
||||
if len(res) < 2 || (strings.HasPrefix(res[index][0], "LIMIT") == false && strings.HasPrefix(res[index][0], "limit") == false) {
|
||||
if len(res) < 2 || (strings.HasPrefix(res[index][0], "LIMIT") == false &&
|
||||
strings.HasPrefix(res[index][0], "limit") == false) {
|
||||
break
|
||||
}
|
||||
|
||||
// 取limit前面的字符串
|
||||
if gregex.IsMatchString("((?i)SELECT)(.+)((?i)LIMIT)", sql) == false {
|
||||
break
|
||||
}
|
||||
|
||||
queryExpr, _ := gregex.MatchString("((?i)SELECT)(.+)((?i)LIMIT)", sql)
|
||||
if len(queryExpr) != 4 || strings.EqualFold(queryExpr[1], "SELECT") == false || strings.EqualFold(queryExpr[3], "LIMIT") == false {
|
||||
if len(queryExpr) != 4 || strings.EqualFold(queryExpr[1], "SELECT") == false ||
|
||||
strings.EqualFold(queryExpr[3], "LIMIT") == false {
|
||||
break
|
||||
}
|
||||
|
||||
// 取limit后面的取值范围
|
||||
first, limit := 0, 0
|
||||
for i := 1; i < len(res[index]); i++ {
|
||||
if len(strings.TrimSpace(res[index][i])) == 0 {
|
||||
@ -121,10 +119,10 @@ func (d *DriverOracle) parseSql(sql string) string {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// 也可以使用between,据说这种写法的性能会比between好点,里层SQL中的ROWNUM_ >= limit可以缩小查询后的数据集规模
|
||||
sql = fmt.Sprintf(
|
||||
"SELECT * FROM (SELECT GFORM.*, ROWNUM ROWNUM_ FROM (%s %s) GFORM WHERE ROWNUM <= %d) WHERE ROWNUM_ >= %d",
|
||||
"SELECT * FROM "+
|
||||
"(SELECT GFORM.*, ROWNUM ROWNUM_ FROM (%s %s) GFORM WHERE ROWNUM <= %d)"+
|
||||
" WHERE ROWNUM_ >= %d",
|
||||
queryExpr[1], queryExpr[2], limit, first,
|
||||
)
|
||||
}
|
||||
@ -377,7 +375,7 @@ func (d *DriverOracle) DoBatchInsert(link Link, table string, list interface{},
|
||||
keys = append(keys, k)
|
||||
holders = append(holders, "?")
|
||||
}
|
||||
batchResult := new(batchSqlResult)
|
||||
batchResult := new(SqlResult)
|
||||
charL, charR := d.DB.GetChars()
|
||||
keyStr := charL + strings.Join(keys, charL+","+charR) + charR
|
||||
valueHolderStr := strings.Join(holders, ",")
|
||||
@ -393,8 +391,8 @@ func (d *DriverOracle) DoBatchInsert(link Link, table string, list interface{},
|
||||
if n, err := r.RowsAffected(); err != nil {
|
||||
return r, err
|
||||
} else {
|
||||
batchResult.lastResult = r
|
||||
batchResult.rowsAffected += n
|
||||
batchResult.result = r
|
||||
batchResult.affected += n
|
||||
}
|
||||
}
|
||||
return batchResult, nil
|
||||
@ -421,8 +419,8 @@ func (d *DriverOracle) DoBatchInsert(link Link, table string, list interface{},
|
||||
if n, err := r.RowsAffected(); err != nil {
|
||||
return r, err
|
||||
} else {
|
||||
batchResult.lastResult = r
|
||||
batchResult.rowsAffected += n
|
||||
batchResult.result = r
|
||||
batchResult.affected += n
|
||||
}
|
||||
params = params[:0]
|
||||
intoStr = intoStr[:0]
|
||||
@ -437,8 +435,8 @@ func (d *DriverOracle) DoBatchInsert(link Link, table string, list interface{},
|
||||
if n, err := r.RowsAffected(); err != nil {
|
||||
return r, err
|
||||
} else {
|
||||
batchResult.lastResult = r
|
||||
batchResult.rowsAffected += n
|
||||
batchResult.result = r
|
||||
batchResult.affected += n
|
||||
}
|
||||
}
|
||||
return batchResult, nil
|
||||
|
||||
@ -66,7 +66,7 @@ func (d *DriverPgsql) HandleSqlBeforeCommit(link Link, sql string, args []interf
|
||||
index++
|
||||
return fmt.Sprintf("$%d", index)
|
||||
})
|
||||
sql, _ = gregex.ReplaceString(` LIMIT (\d+),\s*(\d+)`, ` LIMIT $1 OFFSET $2`, sql)
|
||||
sql, _ = gregex.ReplaceString(` LIMIT (\d+),\s*(\d+)`, ` LIMIT $2 OFFSET $1`, sql)
|
||||
return sql, args
|
||||
}
|
||||
|
||||
|
||||
@ -14,6 +14,7 @@ import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/internal/intlog"
|
||||
"github.com/gogf/gf/os/gfile"
|
||||
"github.com/gogf/gf/text/gstr"
|
||||
"strings"
|
||||
)
|
||||
@ -34,11 +35,16 @@ func (d *DriverSqlite) New(core *Core, node *ConfigNode) (DB, error) {
|
||||
// Open creates and returns a underlying sql.DB object for sqlite.
|
||||
func (d *DriverSqlite) Open(config *ConfigNode) (*sql.DB, error) {
|
||||
var source string
|
||||
var err error
|
||||
if config.LinkInfo != "" {
|
||||
source = config.LinkInfo
|
||||
} else {
|
||||
source = config.Name
|
||||
}
|
||||
source, err = gfile.Search(source)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
intlog.Printf("Open: %s", source)
|
||||
if db, err := sql.Open("sqlite3", source); err == nil {
|
||||
return db, nil
|
||||
@ -53,8 +59,8 @@ func (d *DriverSqlite) GetChars() (charLeft string, charRight string) {
|
||||
}
|
||||
|
||||
// HandleSqlBeforeCommit deals with the sql string before commits it to underlying sql driver.
|
||||
// @todo 需要增加对Save方法的支持,可使用正则来实现替换,
|
||||
// @todo 将ON DUPLICATE KEY UPDATE触发器修改为两条SQL语句(INSERT OR IGNORE & UPDATE)
|
||||
// TODO 需要增加对Save方法的支持,可使用正则来实现替换,
|
||||
// TODO 将ON DUPLICATE KEY UPDATE触发器修改为两条SQL语句(INSERT OR IGNORE & UPDATE)
|
||||
func (d *DriverSqlite) HandleSqlBeforeCommit(link Link, sql string, args []interface{}) (string, []interface{}) {
|
||||
return sql, args
|
||||
}
|
||||
|
||||
@ -8,7 +8,6 @@ package gdb
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/internal/empty"
|
||||
@ -41,6 +40,11 @@ type apiInterfaces interface {
|
||||
Interfaces() []interface{}
|
||||
}
|
||||
|
||||
// apiMapStrAny is the interface support for converting struct parameter to map.
|
||||
type apiMapStrAny interface {
|
||||
MapStrAny() map[string]interface{}
|
||||
}
|
||||
|
||||
const (
|
||||
ORM_TAG_FOR_STRUCT = "orm"
|
||||
ORM_TAG_FOR_UNIQUE = "unique"
|
||||
@ -99,21 +103,27 @@ func DataToMapDeep(obj interface{}) map[string]interface{} {
|
||||
return data
|
||||
}
|
||||
|
||||
// QuotePrefixTableName adds prefix string and quote chars for the table. It handles table string like:
|
||||
// "user", "user u", "user,user_detail", "user u, user_detail ut", "user as u, user_detail as ut", "user.user u".
|
||||
// doHandleTableName adds prefix string and quote chars for the table. It handles table string like:
|
||||
// "user", "user u", "user,user_detail", "user u, user_detail ut", "user as u, user_detail as ut",
|
||||
// "user.user u", "`user`.`user` u".
|
||||
//
|
||||
// Note that, this will automatically checks the table prefix whether already added, if true it does
|
||||
// nothing to the table name, or else adds the prefix to the table name.
|
||||
func doHandleTableName(table, prefix, charLeft, charRight string) string {
|
||||
index := 0
|
||||
array1 := gstr.SplitAndTrim(table, ",")
|
||||
var (
|
||||
index = 0
|
||||
chars = charLeft + charRight
|
||||
array1 = gstr.SplitAndTrim(table, ",")
|
||||
)
|
||||
for k1, v1 := range array1 {
|
||||
array2 := gstr.SplitAndTrim(v1, " ")
|
||||
// Trim the security chars.
|
||||
array2[0] = gstr.TrimLeftStr(array2[0], charLeft)
|
||||
array2[0] = gstr.TrimRightStr(array2[0], charRight)
|
||||
array2[0] = gstr.Trim(array2[0], chars)
|
||||
// Check whether it has database name.
|
||||
array3 := gstr.Split(gstr.Trim(array2[0]), ".")
|
||||
for k, v := range array3 {
|
||||
array3[k] = gstr.Trim(v, chars)
|
||||
}
|
||||
index = len(array3) - 1
|
||||
// If the table name already has the prefix, skips the prefix adding.
|
||||
if len(array3[index]) <= len(prefix) || array3[index][:len(prefix)] != prefix {
|
||||
@ -222,11 +232,11 @@ func GetPrimaryKeyCondition(primary string, where ...interface{}) (newWhereCondi
|
||||
return where
|
||||
}
|
||||
|
||||
// formatQuery formats the query string and its arguments before executing.
|
||||
// formatSql formats the sql string and its arguments before executing.
|
||||
// The internal handleArguments function might be called twice during the SQL procedure,
|
||||
// but do not worry about it, it's safe and efficient.
|
||||
func formatQuery(query string, args []interface{}) (newQuery string, newArgs []interface{}) {
|
||||
return handleArguments(query, args)
|
||||
func formatSql(sql string, args []interface{}) (newQuery string, newArgs []interface{}) {
|
||||
return handleArguments(sql, args)
|
||||
}
|
||||
|
||||
// formatWhere formats where statement and its arguments.
|
||||
@ -288,12 +298,19 @@ func formatWhere(db DB, where interface{}, args []interface{}, omitEmpty bool) (
|
||||
// Eg: Where/And/Or("uid>=", 1)
|
||||
newWhere += "?"
|
||||
} else if gregex.IsMatchString(`^[\w\.\-]+$`, newWhere) {
|
||||
if len(newArgs) == 1 && utils.IsArray(newArgs[0]) {
|
||||
// Eg: Where("id", []int{1,2,3})
|
||||
newWhere += " IN (?)"
|
||||
} else {
|
||||
// Eg: Where/And/Or("uid", 1)
|
||||
newWhere += "=?"
|
||||
newWhere = db.QuoteString(newWhere)
|
||||
if len(newArgs) > 0 {
|
||||
if utils.IsArray(newArgs[0]) {
|
||||
// Eg: Where("id", []int{1,2,3})
|
||||
newWhere += " IN (?)"
|
||||
} else if empty.IsNil(newArgs[0]) {
|
||||
// Eg: Where("id", nil)
|
||||
newWhere += " IS NULL"
|
||||
newArgs = nil
|
||||
} else {
|
||||
// Eg: Where/And/Or("uid", 1)
|
||||
newWhere += "=?"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -323,7 +340,7 @@ func formatWhereInterfaces(db DB, where []interface{}, buffer *bytes.Buffer, new
|
||||
|
||||
// formatWhereKeyValue handles each key-value pair of the parameter map.
|
||||
func formatWhereKeyValue(db DB, buffer *bytes.Buffer, newArgs []interface{}, key string, value interface{}) []interface{} {
|
||||
key = db.QuoteWord(key)
|
||||
quotedKey := db.QuoteWord(key)
|
||||
if buffer.Len() > 0 {
|
||||
buffer.WriteString(" AND ")
|
||||
}
|
||||
@ -331,36 +348,43 @@ func formatWhereKeyValue(db DB, buffer *bytes.Buffer, newArgs []interface{}, key
|
||||
// the key string, it automatically adds '?' holder chars according to its arguments count
|
||||
// and converts it to "IN" statement.
|
||||
rv := reflect.ValueOf(value)
|
||||
switch rv.Kind() {
|
||||
kind := rv.Kind()
|
||||
switch kind {
|
||||
case reflect.Slice, reflect.Array:
|
||||
count := gstr.Count(key, "?")
|
||||
count := gstr.Count(quotedKey, "?")
|
||||
if count == 0 {
|
||||
buffer.WriteString(key + " IN(?)")
|
||||
buffer.WriteString(quotedKey + " IN(?)")
|
||||
newArgs = append(newArgs, value)
|
||||
} else if count != rv.Len() {
|
||||
buffer.WriteString(key)
|
||||
buffer.WriteString(quotedKey)
|
||||
newArgs = append(newArgs, value)
|
||||
} else {
|
||||
buffer.WriteString(key)
|
||||
buffer.WriteString(quotedKey)
|
||||
newArgs = append(newArgs, gconv.Interfaces(value)...)
|
||||
}
|
||||
default:
|
||||
if value == nil {
|
||||
buffer.WriteString(key)
|
||||
if value == nil || empty.IsNil(rv) {
|
||||
if gregex.IsMatchString(`^[\w\.\-]+$`, key) {
|
||||
// The key is a single field name.
|
||||
buffer.WriteString(quotedKey + " IS NULL")
|
||||
} else {
|
||||
// The key may have operation chars.
|
||||
buffer.WriteString(quotedKey)
|
||||
}
|
||||
} else {
|
||||
// It also supports "LIKE" statement, which we considers it an operator.
|
||||
key = gstr.Trim(key)
|
||||
if gstr.Pos(key, "?") == -1 {
|
||||
quotedKey = gstr.Trim(quotedKey)
|
||||
if gstr.Pos(quotedKey, "?") == -1 {
|
||||
like := " like"
|
||||
if len(key) > len(like) && gstr.Equal(key[len(key)-len(like):], like) {
|
||||
buffer.WriteString(key + " ?")
|
||||
} else if lastOperatorReg.MatchString(key) {
|
||||
buffer.WriteString(key + " ?")
|
||||
if len(quotedKey) > len(like) && gstr.Equal(quotedKey[len(quotedKey)-len(like):], like) {
|
||||
buffer.WriteString(quotedKey + " ?")
|
||||
} else if lastOperatorReg.MatchString(quotedKey) {
|
||||
buffer.WriteString(quotedKey + " ?")
|
||||
} else {
|
||||
buffer.WriteString(key + "=?")
|
||||
buffer.WriteString(quotedKey + "=?")
|
||||
}
|
||||
} else {
|
||||
buffer.WriteString(key)
|
||||
buffer.WriteString(quotedKey)
|
||||
}
|
||||
newArgs = append(newArgs, value)
|
||||
}
|
||||
@ -370,8 +394,8 @@ func formatWhereKeyValue(db DB, buffer *bytes.Buffer, newArgs []interface{}, key
|
||||
|
||||
// handleArguments is a nice function which handles the query and its arguments before committing to
|
||||
// underlying driver.
|
||||
func handleArguments(query string, args []interface{}) (newQuery string, newArgs []interface{}) {
|
||||
newQuery = query
|
||||
func handleArguments(sql string, args []interface{}) (newSql string, newArgs []interface{}) {
|
||||
newSql = sql
|
||||
// Handles the slice arguments.
|
||||
if len(args) > 0 {
|
||||
for index, arg := range args {
|
||||
@ -395,12 +419,12 @@ func handleArguments(query string, args []interface{}) (newQuery string, newArgs
|
||||
// It the '?' holder count equals the length of the slice,
|
||||
// it does not implement the arguments splitting logic.
|
||||
// Eg: db.Query("SELECT ?+?", g.Slice{1, 2})
|
||||
if len(args) == 1 && gstr.Count(newQuery, "?") == rv.Len() {
|
||||
if len(args) == 1 && gstr.Count(newSql, "?") == rv.Len() {
|
||||
break
|
||||
}
|
||||
// counter is used to finding the inserting position for the '?' holder.
|
||||
counter := 0
|
||||
newQuery, _ = gregex.ReplaceStringFunc(`\?`, newQuery, func(s string) string {
|
||||
newSql, _ = gregex.ReplaceStringFunc(`\?`, newSql, func(s string) string {
|
||||
counter++
|
||||
if counter == index+1 {
|
||||
return "?" + strings.Repeat(",?", rv.Len()-1)
|
||||
@ -436,19 +460,19 @@ func handleArguments(query string, args []interface{}) (newQuery string, newArgs
|
||||
}
|
||||
|
||||
// formatError customizes and returns the SQL error.
|
||||
func formatError(err error, query string, args ...interface{}) error {
|
||||
if err != nil && err != sql.ErrNoRows {
|
||||
return errors.New(fmt.Sprintf("%s, %s\n", err.Error(), bindArgsToQuery(query, args)))
|
||||
func formatError(err error, sql string, args ...interface{}) error {
|
||||
if err != nil && err != ErrNoRows {
|
||||
return errors.New(fmt.Sprintf("%s, %s\n", err.Error(), FormatSqlWithArgs(sql, args)))
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// bindArgsToQuery binds the arguments to the query string and returns a complete
|
||||
// FormatSqlWithArgs binds the arguments to the sql string and returns a complete
|
||||
// sql string, just for debugging.
|
||||
func bindArgsToQuery(query string, args []interface{}) string {
|
||||
func FormatSqlWithArgs(sql string, args []interface{}) string {
|
||||
index := -1
|
||||
newQuery, _ := gregex.ReplaceStringFunc(
|
||||
`(\?|:\d+|\$\d+|@p\d+)`, query, func(s string) string {
|
||||
`(\?|:\d+|\$\d+|@p\d+)`, sql, func(s string) string {
|
||||
index++
|
||||
if len(args) > index {
|
||||
if args[index] == nil {
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
36
database/gdb/gdb_model_cache.go
Normal file
36
database/gdb/gdb_model_cache.go
Normal file
@ -0,0 +1,36 @@
|
||||
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
// You can obtain one at https://github.com/gogf/gf.
|
||||
|
||||
package gdb
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// Cache sets the cache feature for the model. It caches the result of the sql, which means
|
||||
// if there's another same sql request, it just reads and returns the result from cache, it
|
||||
// but not committed and executed into the database.
|
||||
//
|
||||
// If the parameter <duration> < 0, which means it clear the cache with given <name>.
|
||||
// If the parameter <duration> = 0, which means it never expires.
|
||||
// If the parameter <duration> > 0, which means it expires after <duration>.
|
||||
//
|
||||
// The optional parameter <name> is used to bind a name to the cache, which means you can later
|
||||
// control the cache like changing the <duration> or clearing the cache with specified <name>.
|
||||
//
|
||||
// Note that, the cache feature is disabled if the model is operating on a transaction.
|
||||
func (m *Model) Cache(duration time.Duration, name ...string) *Model {
|
||||
model := m.getModel()
|
||||
model.cacheDuration = duration
|
||||
if len(name) > 0 {
|
||||
model.cacheName = name[0]
|
||||
}
|
||||
// It does not support cache on transaction.
|
||||
if model.tx == nil {
|
||||
model.cacheEnabled = true
|
||||
}
|
||||
return model
|
||||
}
|
||||
170
database/gdb/gdb_model_condition.go
Normal file
170
database/gdb/gdb_model_condition.go
Normal file
@ -0,0 +1,170 @@
|
||||
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
// You can obtain one at https://github.com/gogf/gf.
|
||||
|
||||
package gdb
|
||||
|
||||
import "github.com/gogf/gf/util/gconv"
|
||||
|
||||
// Where sets the condition statement for the model. The parameter <where> can be type of
|
||||
// string/map/gmap/slice/struct/*struct, etc. Note that, if it's called more than one times,
|
||||
// multiple conditions will be joined into where statement using "AND".
|
||||
// Eg:
|
||||
// Where("uid=10000")
|
||||
// Where("uid", 10000)
|
||||
// Where("money>? AND name like ?", 99999, "vip_%")
|
||||
// Where("uid", 1).Where("name", "john")
|
||||
// Where("status IN (?)", g.Slice{1,2,3})
|
||||
// Where("age IN(?,?)", 18, 50)
|
||||
// Where(User{ Id : 1, UserName : "john"})
|
||||
func (m *Model) Where(where interface{}, args ...interface{}) *Model {
|
||||
model := m.getModel()
|
||||
if model.whereHolder == nil {
|
||||
model.whereHolder = make([]*whereHolder, 0)
|
||||
}
|
||||
model.whereHolder = append(model.whereHolder, &whereHolder{
|
||||
operator: gWHERE_HOLDER_WHERE,
|
||||
where: where,
|
||||
args: args,
|
||||
})
|
||||
return model
|
||||
}
|
||||
|
||||
// WherePri does the same logic as Model.Where except that if the parameter <where>
|
||||
// is a single condition like int/string/float/slice, it treats the condition as the primary
|
||||
// key value. That is, if primary key is "id" and given <where> parameter as "123", the
|
||||
// WherePri function treats it as "id=123", but Model.Where treats it as string "123".
|
||||
func (m *Model) WherePri(where interface{}, args ...interface{}) *Model {
|
||||
if len(args) > 0 {
|
||||
return m.Where(where, args...)
|
||||
}
|
||||
newWhere := GetPrimaryKeyCondition(m.getPrimaryKey(), where)
|
||||
return m.Where(newWhere[0], newWhere[1:]...)
|
||||
}
|
||||
|
||||
// And adds "AND" condition to the where statement.
|
||||
func (m *Model) And(where interface{}, args ...interface{}) *Model {
|
||||
model := m.getModel()
|
||||
if model.whereHolder == nil {
|
||||
model.whereHolder = make([]*whereHolder, 0)
|
||||
}
|
||||
model.whereHolder = append(model.whereHolder, &whereHolder{
|
||||
operator: gWHERE_HOLDER_AND,
|
||||
where: where,
|
||||
args: args,
|
||||
})
|
||||
return model
|
||||
}
|
||||
|
||||
// Or adds "OR" condition to the where statement.
|
||||
func (m *Model) Or(where interface{}, args ...interface{}) *Model {
|
||||
model := m.getModel()
|
||||
if model.whereHolder == nil {
|
||||
model.whereHolder = make([]*whereHolder, 0)
|
||||
}
|
||||
model.whereHolder = append(model.whereHolder, &whereHolder{
|
||||
operator: gWHERE_HOLDER_OR,
|
||||
where: where,
|
||||
args: args,
|
||||
})
|
||||
return model
|
||||
}
|
||||
|
||||
// Group sets the "GROUP BY" statement for the model.
|
||||
func (m *Model) Group(groupBy string) *Model {
|
||||
model := m.getModel()
|
||||
model.groupBy = m.db.QuoteString(groupBy)
|
||||
return model
|
||||
}
|
||||
|
||||
// GroupBy is alias of Model.Group.
|
||||
// See Model.Group.
|
||||
// Deprecated.
|
||||
func (m *Model) GroupBy(groupBy string) *Model {
|
||||
return m.Group(groupBy)
|
||||
}
|
||||
|
||||
// Order sets the "ORDER BY" statement for the model.
|
||||
func (m *Model) Order(orderBy string) *Model {
|
||||
model := m.getModel()
|
||||
model.orderBy = m.db.QuoteString(orderBy)
|
||||
return model
|
||||
}
|
||||
|
||||
// OrderBy is alias of Model.Order.
|
||||
// See Model.Order.
|
||||
// Deprecated.
|
||||
func (m *Model) OrderBy(orderBy string) *Model {
|
||||
return m.Order(orderBy)
|
||||
}
|
||||
|
||||
// Limit sets the "LIMIT" statement for the model.
|
||||
// The parameter <limit> can be either one or two number, if passed two number is passed,
|
||||
// it then sets "LIMIT limit[0],limit[1]" statement for the model, or else it sets "LIMIT limit[0]"
|
||||
// statement.
|
||||
func (m *Model) Limit(limit ...int) *Model {
|
||||
model := m.getModel()
|
||||
switch len(limit) {
|
||||
case 1:
|
||||
model.limit = limit[0]
|
||||
case 2:
|
||||
model.start = limit[0]
|
||||
model.limit = limit[1]
|
||||
}
|
||||
return model
|
||||
}
|
||||
|
||||
// Offset sets the "OFFSET" statement for the model.
|
||||
// It only makes sense for some databases like SQLServer, PostgreSQL, etc.
|
||||
func (m *Model) Offset(offset int) *Model {
|
||||
model := m.getModel()
|
||||
model.offset = offset
|
||||
return model
|
||||
}
|
||||
|
||||
// Page sets the paging number for the model.
|
||||
// The parameter <page> is started from 1 for paging.
|
||||
// Note that, it differs that the Limit function start from 0 for "LIMIT" statement.
|
||||
func (m *Model) Page(page, limit int) *Model {
|
||||
model := m.getModel()
|
||||
if page <= 0 {
|
||||
page = 1
|
||||
}
|
||||
model.start = (page - 1) * limit
|
||||
model.limit = limit
|
||||
return model
|
||||
}
|
||||
|
||||
// ForPage is alias of Model.Page.
|
||||
// See Model.Page.
|
||||
// Deprecated.
|
||||
func (m *Model) ForPage(page, limit int) *Model {
|
||||
return m.Page(page, limit)
|
||||
}
|
||||
|
||||
// getAll does the query from database.
|
||||
func (m *Model) getAll(sql string, args ...interface{}) (result Result, err error) {
|
||||
cacheKey := ""
|
||||
// Retrieve from cache.
|
||||
if m.cacheEnabled {
|
||||
cacheKey = m.cacheName
|
||||
if len(cacheKey) == 0 {
|
||||
cacheKey = sql + "/" + gconv.String(args)
|
||||
}
|
||||
if v := m.db.GetCache().Get(cacheKey); v != nil {
|
||||
return v.(Result), nil
|
||||
}
|
||||
}
|
||||
result, err = m.db.DoGetAll(m.getLink(false), sql, m.mergeArguments(args)...)
|
||||
// Cache the result.
|
||||
if len(cacheKey) > 0 && err == nil {
|
||||
if m.cacheDuration < 0 {
|
||||
m.db.GetCache().Remove(cacheKey)
|
||||
} else {
|
||||
m.db.GetCache().Set(cacheKey, result, m.cacheDuration)
|
||||
}
|
||||
}
|
||||
return result, err
|
||||
}
|
||||
27
database/gdb/gdb_model_delete.go
Normal file
27
database/gdb/gdb_model_delete.go
Normal file
@ -0,0 +1,27 @@
|
||||
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
// You can obtain one at https://github.com/gogf/gf.
|
||||
|
||||
package gdb
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
)
|
||||
|
||||
// Delete does "DELETE FROM ... " statement for the model.
|
||||
// The optional parameter <where> is the same as the parameter of Model.Where function,
|
||||
// see Model.Where.
|
||||
func (m *Model) Delete(where ...interface{}) (result sql.Result, err error) {
|
||||
if len(where) > 0 {
|
||||
return m.Where(where[0], where[1:]...).Delete()
|
||||
}
|
||||
defer func() {
|
||||
if err == nil {
|
||||
m.checkAndRemoveCache()
|
||||
}
|
||||
}()
|
||||
condition, conditionArgs := m.formatCondition(false)
|
||||
return m.db.DoDelete(m.getLink(true), m.tables, condition, conditionArgs...)
|
||||
}
|
||||
94
database/gdb/gdb_model_fields.go
Normal file
94
database/gdb/gdb_model_fields.go
Normal file
@ -0,0 +1,94 @@
|
||||
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
// You can obtain one at https://github.com/gogf/gf.
|
||||
|
||||
package gdb
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/container/garray"
|
||||
"github.com/gogf/gf/container/gset"
|
||||
"github.com/gogf/gf/text/gstr"
|
||||
)
|
||||
|
||||
// Filter marks filtering the fields which does not exist in the fields of the operated table.
|
||||
func (m *Model) Filter() *Model {
|
||||
if gstr.Contains(m.tables, " ") {
|
||||
panic("function Filter supports only single table operations")
|
||||
}
|
||||
model := m.getModel()
|
||||
model.filter = true
|
||||
return model
|
||||
}
|
||||
|
||||
// Fields sets the operation fields of the model, multiple fields joined using char ','.
|
||||
func (m *Model) Fields(fields string) *Model {
|
||||
model := m.getModel()
|
||||
model.fields = fields
|
||||
return model
|
||||
}
|
||||
|
||||
// FieldsEx sets the excluded operation fields of the model, multiple fields joined using char ','.
|
||||
func (m *Model) FieldsEx(fields string) *Model {
|
||||
if gstr.Contains(m.tables, " ") {
|
||||
panic("function FieldsEx supports only single table operations")
|
||||
}
|
||||
model := m.getModel()
|
||||
model.fieldsEx = fields
|
||||
fieldsExSet := gset.NewStrSetFrom(gstr.SplitAndTrim(fields, ","))
|
||||
if m, err := m.db.TableFields(m.tables); err == nil {
|
||||
model.fields = ""
|
||||
for k, _ := range m {
|
||||
if fieldsExSet.Contains(k) {
|
||||
continue
|
||||
}
|
||||
if len(model.fields) > 0 {
|
||||
model.fields += ","
|
||||
}
|
||||
model.fields += k
|
||||
}
|
||||
}
|
||||
return model
|
||||
}
|
||||
|
||||
// FieldsStr retrieves and returns all fields from the table, joined with char ','.
|
||||
// The optional parameter <prefix> specifies the prefix for each field, eg: FieldsStr("u.").
|
||||
func (m *Model) FieldsStr(prefix ...string) string {
|
||||
prefixStr := ""
|
||||
if len(prefix) > 0 {
|
||||
prefixStr = prefix[0]
|
||||
}
|
||||
if m, err := m.db.TableFields(m.tables); err == nil {
|
||||
fieldsArray := garray.NewStrArraySize(len(m), len(m))
|
||||
for _, field := range m {
|
||||
fieldsArray.Set(field.Index, prefixStr+field.Name)
|
||||
}
|
||||
return fieldsArray.Join(",")
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// FieldsExStr retrieves and returns fields which are not in parameter <fields> from the table,
|
||||
// joined with char ','.
|
||||
// The parameter <fields> specifies the fields that are excluded.
|
||||
// The optional parameter <prefix> specifies the prefix for each field, eg: FieldsExStr("id", "u.").
|
||||
func (m *Model) FieldsExStr(fields string, prefix ...string) string {
|
||||
prefixStr := ""
|
||||
if len(prefix) > 0 {
|
||||
prefixStr = prefix[0]
|
||||
}
|
||||
if m, err := m.db.TableFields(m.tables); err == nil {
|
||||
fieldsArray := garray.NewStrArraySize(len(m), len(m))
|
||||
fieldsExSet := gset.NewStrSetFrom(gstr.SplitAndTrim(fields, ","))
|
||||
for _, field := range m {
|
||||
if fieldsExSet.Contains(field.Name) {
|
||||
continue
|
||||
}
|
||||
fieldsArray.Set(field.Index, prefixStr+field.Name)
|
||||
}
|
||||
fieldsArray.FilterEmpty()
|
||||
return fieldsArray.Join(",")
|
||||
}
|
||||
return ""
|
||||
}
|
||||
226
database/gdb/gdb_model_insert.go
Normal file
226
database/gdb/gdb_model_insert.go
Normal file
@ -0,0 +1,226 @@
|
||||
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
// You can obtain one at https://github.com/gogf/gf.
|
||||
|
||||
package gdb
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"github.com/gogf/gf/text/gstr"
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
// Batch sets the batch operation number for the model.
|
||||
func (m *Model) Batch(batch int) *Model {
|
||||
model := m.getModel()
|
||||
model.batch = batch
|
||||
return model
|
||||
}
|
||||
|
||||
// Data sets the operation data for the model.
|
||||
// The parameter <data> can be type of string/map/gmap/slice/struct/*struct, etc.
|
||||
// Eg:
|
||||
// Data("uid=10000")
|
||||
// Data("uid", 10000)
|
||||
// Data("uid=? AND name=?", 10000, "john")
|
||||
// Data(g.Map{"uid": 10000, "name":"john"})
|
||||
// Data(g.Slice{g.Map{"uid": 10000, "name":"john"}, g.Map{"uid": 20000, "name":"smith"})
|
||||
func (m *Model) Data(data ...interface{}) *Model {
|
||||
model := m.getModel()
|
||||
if len(data) > 1 {
|
||||
s := gconv.String(data[0])
|
||||
if gstr.Contains(s, "?") {
|
||||
model.data = s
|
||||
model.extraArgs = data[1:]
|
||||
} else {
|
||||
m := make(map[string]interface{})
|
||||
for i := 0; i < len(data); i += 2 {
|
||||
m[gconv.String(data[i])] = data[i+1]
|
||||
}
|
||||
model.data = m
|
||||
}
|
||||
} else {
|
||||
switch params := data[0].(type) {
|
||||
case Result:
|
||||
model.data = params.List()
|
||||
case Record:
|
||||
model.data = params.Map()
|
||||
case List:
|
||||
model.data = params
|
||||
case Map:
|
||||
model.data = params
|
||||
default:
|
||||
rv := reflect.ValueOf(params)
|
||||
kind := rv.Kind()
|
||||
if kind == reflect.Ptr {
|
||||
rv = rv.Elem()
|
||||
kind = rv.Kind()
|
||||
}
|
||||
switch kind {
|
||||
case reflect.Slice, reflect.Array:
|
||||
list := make(List, rv.Len())
|
||||
for i := 0; i < rv.Len(); i++ {
|
||||
list[i] = DataToMapDeep(rv.Index(i).Interface())
|
||||
}
|
||||
model.data = list
|
||||
case reflect.Map:
|
||||
model.data = DataToMapDeep(data[0])
|
||||
case reflect.Struct:
|
||||
if v, ok := data[0].(apiMapStrAny); ok {
|
||||
model.data = v.MapStrAny()
|
||||
} else if v, ok := data[0].(apiInterfaces); ok {
|
||||
array := v.Interfaces()
|
||||
list := make(List, len(array))
|
||||
for i := 0; i < len(array); i++ {
|
||||
list[i] = DataToMapDeep(array[i])
|
||||
}
|
||||
model.data = list
|
||||
} else {
|
||||
model.data = DataToMapDeep(data[0])
|
||||
}
|
||||
default:
|
||||
model.data = data[0]
|
||||
}
|
||||
}
|
||||
}
|
||||
return model
|
||||
}
|
||||
|
||||
// Insert does "INSERT INTO ..." statement for the model.
|
||||
// The optional parameter <data> is the same as the parameter of Model.Data function,
|
||||
// see Model.Data.
|
||||
func (m *Model) Insert(data ...interface{}) (result sql.Result, err error) {
|
||||
return m.doInsertWithOption(gINSERT_OPTION_DEFAULT, data...)
|
||||
}
|
||||
|
||||
// InsertIgnore does "INSERT IGNORE INTO ..." statement for the model.
|
||||
// The optional parameter <data> is the same as the parameter of Model.Data function,
|
||||
// see Model.Data.
|
||||
func (m *Model) InsertIgnore(data ...interface{}) (result sql.Result, err error) {
|
||||
return m.doInsertWithOption(gINSERT_OPTION_IGNORE, data...)
|
||||
}
|
||||
|
||||
// doInsertWithOption inserts data with option parameter.
|
||||
func (m *Model) doInsertWithOption(option int, data ...interface{}) (result sql.Result, err error) {
|
||||
if len(data) > 0 {
|
||||
return m.Data(data...).Insert()
|
||||
}
|
||||
defer func() {
|
||||
if err == nil {
|
||||
m.checkAndRemoveCache()
|
||||
}
|
||||
}()
|
||||
if m.data == nil {
|
||||
return nil, errors.New("inserting into table with empty data")
|
||||
}
|
||||
if list, ok := m.data.(List); ok {
|
||||
// Batch insert.
|
||||
batch := 10
|
||||
if m.batch > 0 {
|
||||
batch = m.batch
|
||||
}
|
||||
return m.db.DoBatchInsert(
|
||||
m.getLink(true),
|
||||
m.tables,
|
||||
m.filterDataForInsertOrUpdate(list),
|
||||
option,
|
||||
batch,
|
||||
)
|
||||
} else if data, ok := m.data.(Map); ok {
|
||||
// Single insert.
|
||||
return m.db.DoInsert(
|
||||
m.getLink(true),
|
||||
m.tables,
|
||||
m.filterDataForInsertOrUpdate(data),
|
||||
option,
|
||||
)
|
||||
}
|
||||
return nil, errors.New("inserting into table with invalid data type")
|
||||
}
|
||||
|
||||
// Replace does "REPLACE INTO ..." statement for the model.
|
||||
// The optional parameter <data> is the same as the parameter of Model.Data function,
|
||||
// see Model.Data.
|
||||
func (m *Model) Replace(data ...interface{}) (result sql.Result, err error) {
|
||||
if len(data) > 0 {
|
||||
return m.Data(data...).Replace()
|
||||
}
|
||||
defer func() {
|
||||
if err == nil {
|
||||
m.checkAndRemoveCache()
|
||||
}
|
||||
}()
|
||||
if m.data == nil {
|
||||
return nil, errors.New("replacing into table with empty data")
|
||||
}
|
||||
if list, ok := m.data.(List); ok {
|
||||
// Batch replace.
|
||||
batch := 10
|
||||
if m.batch > 0 {
|
||||
batch = m.batch
|
||||
}
|
||||
return m.db.DoBatchInsert(
|
||||
m.getLink(true),
|
||||
m.tables,
|
||||
m.filterDataForInsertOrUpdate(list),
|
||||
gINSERT_OPTION_REPLACE,
|
||||
batch,
|
||||
)
|
||||
} else if data, ok := m.data.(Map); ok {
|
||||
// Single insert.
|
||||
return m.db.DoInsert(
|
||||
m.getLink(true),
|
||||
m.tables,
|
||||
m.filterDataForInsertOrUpdate(data),
|
||||
gINSERT_OPTION_REPLACE,
|
||||
)
|
||||
}
|
||||
return nil, errors.New("replacing into table with invalid data type")
|
||||
}
|
||||
|
||||
// Save does "INSERT INTO ... ON DUPLICATE KEY UPDATE..." statement for the model.
|
||||
// The optional parameter <data> is the same as the parameter of Model.Data function,
|
||||
// see Model.Data.
|
||||
//
|
||||
// It updates the record if there's primary or unique index in the saving data,
|
||||
// or else it inserts a new record into the table.
|
||||
func (m *Model) Save(data ...interface{}) (result sql.Result, err error) {
|
||||
if len(data) > 0 {
|
||||
return m.Data(data...).Save()
|
||||
}
|
||||
defer func() {
|
||||
if err == nil {
|
||||
m.checkAndRemoveCache()
|
||||
}
|
||||
}()
|
||||
if m.data == nil {
|
||||
return nil, errors.New("saving into table with empty data")
|
||||
}
|
||||
if list, ok := m.data.(List); ok {
|
||||
// Batch save.
|
||||
batch := gDEFAULT_BATCH_NUM
|
||||
if m.batch > 0 {
|
||||
batch = m.batch
|
||||
}
|
||||
return m.db.DoBatchInsert(
|
||||
m.getLink(true),
|
||||
m.tables,
|
||||
m.filterDataForInsertOrUpdate(list),
|
||||
gINSERT_OPTION_SAVE,
|
||||
batch,
|
||||
)
|
||||
} else if data, ok := m.data.(Map); ok {
|
||||
// Single save.
|
||||
return m.db.DoInsert(
|
||||
m.getLink(true),
|
||||
m.tables,
|
||||
m.filterDataForInsertOrUpdate(data),
|
||||
gINSERT_OPTION_SAVE,
|
||||
)
|
||||
}
|
||||
return nil, errors.New("saving into table with invalid data type")
|
||||
}
|
||||
30
database/gdb/gdb_model_join.go
Normal file
30
database/gdb/gdb_model_join.go
Normal file
@ -0,0 +1,30 @@
|
||||
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
// You can obtain one at https://github.com/gogf/gf.
|
||||
|
||||
package gdb
|
||||
|
||||
import "fmt"
|
||||
|
||||
// LeftJoin does "LEFT JOIN ... ON ..." statement on the model.
|
||||
func (m *Model) LeftJoin(table string, on string) *Model {
|
||||
model := m.getModel()
|
||||
model.tables += fmt.Sprintf(" LEFT JOIN %s ON (%s)", m.db.QuotePrefixTableName(table), on)
|
||||
return model
|
||||
}
|
||||
|
||||
// RightJoin does "RIGHT JOIN ... ON ..." statement on the model.
|
||||
func (m *Model) RightJoin(table string, on string) *Model {
|
||||
model := m.getModel()
|
||||
model.tables += fmt.Sprintf(" RIGHT JOIN %s ON (%s)", m.db.QuotePrefixTableName(table), on)
|
||||
return model
|
||||
}
|
||||
|
||||
// InnerJoin does "INNER JOIN ... ON ..." statement on the model.
|
||||
func (m *Model) InnerJoin(table string, on string) *Model {
|
||||
model := m.getModel()
|
||||
model.tables += fmt.Sprintf(" INNER JOIN %s ON (%s)", m.db.QuotePrefixTableName(table), on)
|
||||
return model
|
||||
}
|
||||
21
database/gdb/gdb_model_lock.go
Normal file
21
database/gdb/gdb_model_lock.go
Normal file
@ -0,0 +1,21 @@
|
||||
// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
// You can obtain one at https://github.com/gogf/gf.
|
||||
|
||||
package gdb
|
||||
|
||||
// LockUpdate sets the lock for update for current operation.
|
||||
func (m *Model) LockUpdate() *Model {
|
||||
model := m.getModel()
|
||||
model.lockInfo = "FOR UPDATE"
|
||||
return model
|
||||
}
|
||||
|
||||
// LockShared sets the lock in share mode for current operation.
|
||||
func (m *Model) LockShared() *Model {
|
||||
model := m.getModel()
|
||||
model.lockInfo = "LOCK IN SHARE MODE"
|
||||
return model
|
||||
}
|
||||
27
database/gdb/gdb_model_option.go
Normal file
27
database/gdb/gdb_model_option.go
Normal file
@ -0,0 +1,27 @@
|
||||
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
// You can obtain one at https://github.com/gogf/gf.
|
||||
|
||||
package gdb
|
||||
|
||||
// Option adds extra operation option for the model.
|
||||
func (m *Model) Option(option int) *Model {
|
||||
model := m.getModel()
|
||||
model.option = model.option | option
|
||||
return model
|
||||
}
|
||||
|
||||
// OptionOmitEmpty sets OPTION_OMITEMPTY option for the model, which automatically filers
|
||||
// the data and where attributes for empty values.
|
||||
// Deprecated, use OmitEmpty instead.
|
||||
func (m *Model) OptionOmitEmpty() *Model {
|
||||
return m.Option(OPTION_OMITEMPTY)
|
||||
}
|
||||
|
||||
// OmitEmpty sets OPTION_OMITEMPTY option for the model, which automatically filers
|
||||
// the data and where attributes for empty values.
|
||||
func (m *Model) OmitEmpty() *Model {
|
||||
return m.Option(OPTION_OMITEMPTY)
|
||||
}
|
||||
315
database/gdb/gdb_model_select.go
Normal file
315
database/gdb/gdb_model_select.go
Normal file
@ -0,0 +1,315 @@
|
||||
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
// You can obtain one at https://github.com/gogf/gf.
|
||||
|
||||
package gdb
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
// Select is alias of Model.All.
|
||||
// See Model.All.
|
||||
// Deprecated.
|
||||
func (m *Model) Select(where ...interface{}) (Result, error) {
|
||||
return m.All(where...)
|
||||
}
|
||||
|
||||
// All does "SELECT FROM ..." statement for the model.
|
||||
// It retrieves the records from table and returns the result as slice type.
|
||||
// It returns nil if there's no record retrieved with the given conditions from table.
|
||||
//
|
||||
// The optional parameter <where> is the same as the parameter of Model.Where function,
|
||||
// see Model.Where.
|
||||
func (m *Model) All(where ...interface{}) (Result, error) {
|
||||
if len(where) > 0 {
|
||||
return m.Where(where[0], where[1:]...).All()
|
||||
}
|
||||
condition, conditionArgs := m.formatCondition(false)
|
||||
return m.getAll(
|
||||
fmt.Sprintf("SELECT %s FROM %s%s", m.fields, m.tables, condition),
|
||||
conditionArgs...,
|
||||
)
|
||||
}
|
||||
|
||||
// Chunk iterates the query result with given size and callback function.
|
||||
func (m *Model) Chunk(limit int, callback func(result Result, err error) bool) {
|
||||
page := m.start
|
||||
if page == 0 {
|
||||
page = 1
|
||||
}
|
||||
model := m
|
||||
for {
|
||||
model = model.Page(page, limit)
|
||||
data, err := model.All()
|
||||
if err != nil {
|
||||
callback(nil, err)
|
||||
break
|
||||
}
|
||||
if len(data) == 0 {
|
||||
break
|
||||
}
|
||||
if callback(data, err) == false {
|
||||
break
|
||||
}
|
||||
if len(data) < limit {
|
||||
break
|
||||
}
|
||||
page++
|
||||
}
|
||||
}
|
||||
|
||||
// One retrieves one record from table and returns the result as map type.
|
||||
// It returns nil if there's no record retrieved with the given conditions from table.
|
||||
//
|
||||
// The optional parameter <where> is the same as the parameter of Model.Where function,
|
||||
// see Model.Where.
|
||||
func (m *Model) One(where ...interface{}) (Record, error) {
|
||||
if len(where) > 0 {
|
||||
return m.Where(where[0], where[1:]...).One()
|
||||
}
|
||||
condition, conditionArgs := m.formatCondition(true)
|
||||
all, err := m.getAll(fmt.Sprintf("SELECT %s FROM %s%s", m.fields, m.tables, condition), conditionArgs...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(all) > 0 {
|
||||
return all[0], nil
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// Value retrieves a specified record value from table and returns the result as interface type.
|
||||
// It returns nil if there's no record found with the given conditions from table.
|
||||
//
|
||||
// If the optional parameter <fieldsAndWhere> is given, the fieldsAndWhere[0] is the selected fields
|
||||
// and fieldsAndWhere[1:] is treated as where condition fields.
|
||||
// Also see Model.Fields and Model.Where functions.
|
||||
func (m *Model) Value(fieldsAndWhere ...interface{}) (Value, error) {
|
||||
if len(fieldsAndWhere) > 0 {
|
||||
if len(fieldsAndWhere) > 2 {
|
||||
return m.Fields(gconv.String(fieldsAndWhere[0])).Where(fieldsAndWhere[1], fieldsAndWhere[2:]...).Value()
|
||||
} else if len(fieldsAndWhere) == 2 {
|
||||
return m.Fields(gconv.String(fieldsAndWhere[0])).Where(fieldsAndWhere[1]).Value()
|
||||
} else {
|
||||
return m.Fields(gconv.String(fieldsAndWhere[0])).Value()
|
||||
}
|
||||
}
|
||||
one, err := m.One()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, v := range one {
|
||||
return v, nil
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// Array queries and returns data values as slice from database.
|
||||
// Note that if there're multiple columns in the result, it returns just one column values randomly.
|
||||
//
|
||||
// If the optional parameter <fieldsAndWhere> is given, the fieldsAndWhere[0] is the selected fields
|
||||
// and fieldsAndWhere[1:] is treated as where condition fields.
|
||||
// Also see Model.Fields and Model.Where functions.
|
||||
func (m *Model) Array(fieldsAndWhere ...interface{}) ([]Value, error) {
|
||||
if len(fieldsAndWhere) > 0 {
|
||||
if len(fieldsAndWhere) > 2 {
|
||||
return m.Fields(gconv.String(fieldsAndWhere[0])).Where(fieldsAndWhere[1], fieldsAndWhere[2:]...).Array()
|
||||
} else if len(fieldsAndWhere) == 2 {
|
||||
return m.Fields(gconv.String(fieldsAndWhere[0])).Where(fieldsAndWhere[1]).Array()
|
||||
} else {
|
||||
return m.Fields(gconv.String(fieldsAndWhere[0])).Array()
|
||||
}
|
||||
}
|
||||
all, err := m.All()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return all.Array(), nil
|
||||
}
|
||||
|
||||
// Struct retrieves one record from table and converts it into given struct.
|
||||
// The parameter <pointer> should be type of *struct/**struct. If type **struct is given,
|
||||
// it can create the struct internally during converting.
|
||||
//
|
||||
// The optional parameter <where> is the same as the parameter of Model.Where function,
|
||||
// see Model.Where.
|
||||
//
|
||||
// Note that it returns sql.ErrNoRows if there's no record retrieved with the given conditions
|
||||
// from table.
|
||||
//
|
||||
// Eg:
|
||||
// user := new(User)
|
||||
// err := db.Table("user").Where("id", 1).Struct(user)
|
||||
//
|
||||
// user := (*User)(nil)
|
||||
// err := db.Table("user").Where("id", 1).Struct(&user)
|
||||
func (m *Model) Struct(pointer interface{}, where ...interface{}) error {
|
||||
one, err := m.One(where...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(one) == 0 {
|
||||
return sql.ErrNoRows
|
||||
}
|
||||
return one.Struct(pointer)
|
||||
}
|
||||
|
||||
// Structs retrieves records from table and converts them into given struct slice.
|
||||
// The parameter <pointer> should be type of *[]struct/*[]*struct. It can create and fill the struct
|
||||
// slice internally during converting.
|
||||
//
|
||||
// The optional parameter <where> is the same as the parameter of Model.Where function,
|
||||
// see Model.Where.
|
||||
//
|
||||
// Note that it returns sql.ErrNoRows if there's no record retrieved with the given conditions
|
||||
// from table.
|
||||
//
|
||||
// Eg:
|
||||
// users := ([]User)(nil)
|
||||
// err := db.Table("user").Structs(&users)
|
||||
//
|
||||
// users := ([]*User)(nil)
|
||||
// err := db.Table("user").Structs(&users)
|
||||
func (m *Model) Structs(pointer interface{}, where ...interface{}) error {
|
||||
all, err := m.All(where...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(all) == 0 {
|
||||
return sql.ErrNoRows
|
||||
}
|
||||
return all.Structs(pointer)
|
||||
}
|
||||
|
||||
// Scan automatically calls Struct or Structs function according to the type of parameter <pointer>.
|
||||
// It calls function Struct if <pointer> is type of *struct/**struct.
|
||||
// It calls function Structs if <pointer> is type of *[]struct/*[]*struct.
|
||||
//
|
||||
// The optional parameter <where> is the same as the parameter of Model.Where function,
|
||||
// see Model.Where.
|
||||
//
|
||||
// Note that it returns sql.ErrNoRows if there's no record retrieved with the given conditions
|
||||
// from table.
|
||||
//
|
||||
// Eg:
|
||||
// user := new(User)
|
||||
// err := db.Table("user").Where("id", 1).Struct(user)
|
||||
//
|
||||
// user := (*User)(nil)
|
||||
// err := db.Table("user").Where("id", 1).Struct(&user)
|
||||
//
|
||||
// users := ([]User)(nil)
|
||||
// err := db.Table("user").Structs(&users)
|
||||
//
|
||||
// users := ([]*User)(nil)
|
||||
// err := db.Table("user").Structs(&users)
|
||||
func (m *Model) Scan(pointer interface{}, where ...interface{}) error {
|
||||
t := reflect.TypeOf(pointer)
|
||||
k := t.Kind()
|
||||
if k != reflect.Ptr {
|
||||
return fmt.Errorf("params should be type of pointer, but got: %v", k)
|
||||
}
|
||||
switch t.Elem().Kind() {
|
||||
case reflect.Array:
|
||||
case reflect.Slice:
|
||||
return m.Structs(pointer, where...)
|
||||
default:
|
||||
return m.Struct(pointer, where...)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Count does "SELECT COUNT(x) FROM ..." statement for the model.
|
||||
// The optional parameter <where> is the same as the parameter of Model.Where function,
|
||||
// see Model.Where.
|
||||
func (m *Model) Count(where ...interface{}) (int, error) {
|
||||
if len(where) > 0 {
|
||||
return m.Where(where[0], where[1:]...).Count()
|
||||
}
|
||||
countFields := "COUNT(1)"
|
||||
if m.fields != "" && m.fields != "*" {
|
||||
countFields = fmt.Sprintf(`COUNT(%s)`, m.fields)
|
||||
}
|
||||
condition, conditionArgs := m.formatCondition(false)
|
||||
s := fmt.Sprintf("SELECT %s FROM %s %s", countFields, m.tables, condition)
|
||||
if len(m.groupBy) > 0 {
|
||||
s = fmt.Sprintf("SELECT COUNT(1) FROM (%s) count_alias", s)
|
||||
}
|
||||
list, err := m.getAll(s, conditionArgs...)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if len(list) > 0 {
|
||||
for _, v := range list[0] {
|
||||
return v.Int(), nil
|
||||
}
|
||||
}
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
// FindOne retrieves and returns a single Record by Model.WherePri and Model.One.
|
||||
// Also see Model.WherePri and Model.One.
|
||||
func (m *Model) FindOne(where ...interface{}) (Record, error) {
|
||||
if len(where) > 0 {
|
||||
return m.WherePri(where[0], where[1:]...).One()
|
||||
}
|
||||
return m.One()
|
||||
}
|
||||
|
||||
// FindAll retrieves and returns Result by by Model.WherePri and Model.All.
|
||||
// Also see Model.WherePri and Model.All.
|
||||
func (m *Model) FindAll(where ...interface{}) (Result, error) {
|
||||
if len(where) > 0 {
|
||||
return m.WherePri(where[0], where[1:]...).All()
|
||||
}
|
||||
return m.All()
|
||||
}
|
||||
|
||||
// FindValue retrieves and returns single field value by Model.WherePri and Model.Value.
|
||||
// Also see Model.WherePri and Model.Value.
|
||||
func (m *Model) FindValue(fieldsAndWhere ...interface{}) (Value, error) {
|
||||
if len(fieldsAndWhere) >= 2 {
|
||||
return m.WherePri(fieldsAndWhere[1], fieldsAndWhere[2:]...).Fields(gconv.String(fieldsAndWhere[0])).Value()
|
||||
}
|
||||
if len(fieldsAndWhere) == 1 {
|
||||
return m.Fields(gconv.String(fieldsAndWhere[0])).Value()
|
||||
}
|
||||
return m.Value()
|
||||
}
|
||||
|
||||
// FindArray queries and returns data values as slice from database.
|
||||
// Note that if there're multiple columns in the result, it returns just one column values randomly.
|
||||
// Also see Model.WherePri and Model.Value.
|
||||
func (m *Model) FindArray(fieldsAndWhere ...interface{}) ([]Value, error) {
|
||||
if len(fieldsAndWhere) >= 2 {
|
||||
return m.WherePri(fieldsAndWhere[1], fieldsAndWhere[2:]...).Fields(gconv.String(fieldsAndWhere[0])).Array()
|
||||
}
|
||||
if len(fieldsAndWhere) == 1 {
|
||||
return m.Fields(gconv.String(fieldsAndWhere[0])).Array()
|
||||
}
|
||||
return m.Array()
|
||||
}
|
||||
|
||||
// FindCount retrieves and returns the record number by Model.WherePri and Model.Count.
|
||||
// Also see Model.WherePri and Model.Count.
|
||||
func (m *Model) FindCount(where ...interface{}) (int, error) {
|
||||
if len(where) > 0 {
|
||||
return m.WherePri(where[0], where[1:]...).Count()
|
||||
}
|
||||
return m.Count()
|
||||
}
|
||||
|
||||
// FindScan retrieves and returns the record/records by Model.WherePri and Model.Scan.
|
||||
// Also see Model.WherePri and Model.Scan.
|
||||
func (m *Model) FindScan(pointer interface{}, where ...interface{}) error {
|
||||
if len(where) > 0 {
|
||||
return m.WherePri(where[0], where[1:]...).Scan(pointer)
|
||||
}
|
||||
return m.Scan(pointer)
|
||||
}
|
||||
45
database/gdb/gdb_model_update.go
Normal file
45
database/gdb/gdb_model_update.go
Normal file
@ -0,0 +1,45 @@
|
||||
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
// You can obtain one at https://github.com/gogf/gf.
|
||||
|
||||
package gdb
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
)
|
||||
|
||||
// Update does "UPDATE ... " statement for the model.
|
||||
//
|
||||
// If the optional parameter <dataAndWhere> is given, the dataAndWhere[0] is the updated data field,
|
||||
// and dataAndWhere[1:] is treated as where condition fields.
|
||||
// Also see Model.Data and Model.Where functions.
|
||||
func (m *Model) Update(dataAndWhere ...interface{}) (result sql.Result, err error) {
|
||||
if len(dataAndWhere) > 0 {
|
||||
if len(dataAndWhere) > 2 {
|
||||
return m.Data(dataAndWhere[0]).Where(dataAndWhere[1], dataAndWhere[2:]...).Update()
|
||||
} else if len(dataAndWhere) == 2 {
|
||||
return m.Data(dataAndWhere[0]).Where(dataAndWhere[1]).Update()
|
||||
} else {
|
||||
return m.Data(dataAndWhere[0]).Update()
|
||||
}
|
||||
}
|
||||
defer func() {
|
||||
if err == nil {
|
||||
m.checkAndRemoveCache()
|
||||
}
|
||||
}()
|
||||
if m.data == nil {
|
||||
return nil, errors.New("updating table with empty data")
|
||||
}
|
||||
condition, conditionArgs := m.formatCondition(false)
|
||||
return m.db.DoUpdate(
|
||||
m.getLink(true),
|
||||
m.tables,
|
||||
m.filterDataForInsertOrUpdate(m.data),
|
||||
condition,
|
||||
m.mergeArguments(conditionArgs)...,
|
||||
)
|
||||
}
|
||||
205
database/gdb/gdb_model_utility.go
Normal file
205
database/gdb/gdb_model_utility.go
Normal file
@ -0,0 +1,205 @@
|
||||
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
// You can obtain one at https://github.com/gogf/gf.
|
||||
|
||||
package gdb
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/container/gmap"
|
||||
"github.com/gogf/gf/container/gset"
|
||||
"github.com/gogf/gf/text/gstr"
|
||||
)
|
||||
|
||||
// getModel creates and returns a cloned model of current model if <safe> is true, or else it returns
|
||||
// the current model.
|
||||
func (m *Model) getModel() *Model {
|
||||
if !m.safe {
|
||||
return m
|
||||
} else {
|
||||
return m.Clone()
|
||||
}
|
||||
}
|
||||
|
||||
// filterDataForInsertOrUpdate does filter feature with data for inserting/updating operations.
|
||||
// Note that, it does not filter list item, which is also type of map, for "omit empty" feature.
|
||||
func (m *Model) filterDataForInsertOrUpdate(data interface{}) interface{} {
|
||||
if list, ok := m.data.(List); ok {
|
||||
for k, item := range list {
|
||||
list[k] = m.doFilterDataMapForInsertOrUpdate(item, false)
|
||||
}
|
||||
return list
|
||||
} else if item, ok := m.data.(Map); ok {
|
||||
return m.doFilterDataMapForInsertOrUpdate(item, true)
|
||||
}
|
||||
return data
|
||||
}
|
||||
|
||||
// doFilterDataMapForInsertOrUpdate does the filter features for map.
|
||||
// Note that, it does not filter list item, which is also type of map, for "omit empty" feature.
|
||||
func (m *Model) doFilterDataMapForInsertOrUpdate(data Map, allowOmitEmpty bool) Map {
|
||||
if m.filter {
|
||||
data = m.db.filterFields(m.schema, m.tables, data)
|
||||
}
|
||||
// Remove key-value pairs of which the value is empty.
|
||||
if allowOmitEmpty && m.option&OPTION_OMITEMPTY > 0 {
|
||||
m := gmap.NewStrAnyMapFrom(data)
|
||||
m.FilterEmpty()
|
||||
data = m.Map()
|
||||
}
|
||||
|
||||
if len(m.fields) > 0 && m.fields != "*" {
|
||||
// Keep specified fields.
|
||||
set := gset.NewStrSetFrom(gstr.SplitAndTrim(m.fields, ","))
|
||||
for k := range data {
|
||||
if !set.Contains(k) {
|
||||
delete(data, k)
|
||||
}
|
||||
}
|
||||
} else if len(m.fieldsEx) > 0 {
|
||||
// Filter specified fields.
|
||||
for _, v := range gstr.SplitAndTrim(m.fieldsEx, ",") {
|
||||
delete(data, v)
|
||||
}
|
||||
}
|
||||
return data
|
||||
}
|
||||
|
||||
// getLink returns the underlying database link object with configured <linkType> attribute.
|
||||
// The parameter <master> specifies whether using the master node if master-slave configured.
|
||||
func (m *Model) getLink(master bool) Link {
|
||||
if m.tx != nil {
|
||||
return m.tx.tx
|
||||
}
|
||||
linkType := m.linkType
|
||||
if linkType == 0 {
|
||||
if master {
|
||||
linkType = gLINK_TYPE_MASTER
|
||||
} else {
|
||||
linkType = gLINK_TYPE_SLAVE
|
||||
}
|
||||
}
|
||||
switch linkType {
|
||||
case gLINK_TYPE_MASTER:
|
||||
link, err := m.db.GetMaster(m.schema)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return link
|
||||
case gLINK_TYPE_SLAVE:
|
||||
link, err := m.db.GetSlave(m.schema)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return link
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// getPrimaryKey retrieves and returns the primary key name of the model table.
|
||||
// It parses m.tables to retrieve the primary table name, supporting m.tables like:
|
||||
// "user", "user u", "user as u, user_detail as ud".
|
||||
func (m *Model) getPrimaryKey() string {
|
||||
table := gstr.SplitAndTrim(m.tables, " ")[0]
|
||||
tableFields, err := m.db.TableFields(table)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
for name, field := range tableFields {
|
||||
if gstr.ContainsI(field.Key, "pri") {
|
||||
return name
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// checkAndRemoveCache checks and remove the cache if necessary.
|
||||
func (m *Model) checkAndRemoveCache() {
|
||||
if m.cacheEnabled && m.cacheDuration < 0 && len(m.cacheName) > 0 {
|
||||
m.db.GetCache().Remove(m.cacheName)
|
||||
}
|
||||
}
|
||||
|
||||
// formatCondition formats where arguments of the model and returns a new condition sql and its arguments.
|
||||
// Note that this function does not change any attribute value of the <m>.
|
||||
//
|
||||
// The parameter <limit> specifies whether limits querying only one record if m.limit is not set.
|
||||
func (m *Model) formatCondition(limit bool) (condition string, conditionArgs []interface{}) {
|
||||
var where string
|
||||
if len(m.whereHolder) > 0 {
|
||||
for _, v := range m.whereHolder {
|
||||
switch v.operator {
|
||||
case gWHERE_HOLDER_WHERE:
|
||||
if where == "" {
|
||||
newWhere, newArgs := formatWhere(m.db, v.where, v.args, m.option&OPTION_OMITEMPTY > 0)
|
||||
if len(newWhere) > 0 {
|
||||
where = newWhere
|
||||
conditionArgs = newArgs
|
||||
}
|
||||
continue
|
||||
}
|
||||
fallthrough
|
||||
|
||||
case gWHERE_HOLDER_AND:
|
||||
newWhere, newArgs := formatWhere(m.db, v.where, v.args, m.option&OPTION_OMITEMPTY > 0)
|
||||
if len(newWhere) > 0 {
|
||||
if where[0] == '(' {
|
||||
where = fmt.Sprintf(`%s AND (%s)`, where, newWhere)
|
||||
} else {
|
||||
where = fmt.Sprintf(`(%s) AND (%s)`, where, newWhere)
|
||||
}
|
||||
conditionArgs = append(conditionArgs, newArgs...)
|
||||
}
|
||||
|
||||
case gWHERE_HOLDER_OR:
|
||||
newWhere, newArgs := formatWhere(m.db, v.where, v.args, m.option&OPTION_OMITEMPTY > 0)
|
||||
if len(newWhere) > 0 {
|
||||
if where[0] == '(' {
|
||||
where = fmt.Sprintf(`%s OR (%s)`, where, newWhere)
|
||||
} else {
|
||||
where = fmt.Sprintf(`(%s) OR (%s)`, where, newWhere)
|
||||
}
|
||||
conditionArgs = append(conditionArgs, newArgs...)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if where != "" {
|
||||
condition += " WHERE " + where
|
||||
}
|
||||
if m.groupBy != "" {
|
||||
condition += " GROUP BY " + m.groupBy
|
||||
}
|
||||
if m.orderBy != "" {
|
||||
condition += " ORDER BY " + m.orderBy
|
||||
}
|
||||
if m.limit != 0 {
|
||||
if m.start >= 0 {
|
||||
condition += fmt.Sprintf(" LIMIT %d,%d", m.start, m.limit)
|
||||
} else {
|
||||
condition += fmt.Sprintf(" LIMIT %d", m.limit)
|
||||
}
|
||||
} else if limit {
|
||||
condition += " LIMIT 1"
|
||||
}
|
||||
if m.offset >= 0 {
|
||||
condition += fmt.Sprintf(" OFFSET %d", m.offset)
|
||||
}
|
||||
if m.lockInfo != "" {
|
||||
condition += " " + m.lockInfo
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// mergeArguments creates and returns new arguments by merging <m.extraArgs> and given <args>.
|
||||
func (m *Model) mergeArguments(args []interface{}) []interface{} {
|
||||
if len(m.extraArgs) > 0 {
|
||||
newArgs := make([]interface{}, len(m.extraArgs)+len(args))
|
||||
copy(newArgs, m.extraArgs)
|
||||
copy(newArgs[len(m.extraArgs):], args)
|
||||
return newArgs
|
||||
}
|
||||
return args
|
||||
}
|
||||
47
database/gdb/gdb_result.go
Normal file
47
database/gdb/gdb_result.go
Normal file
@ -0,0 +1,47 @@
|
||||
// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
// You can obtain one at https://github.com/gogf/gf.
|
||||
|
||||
package gdb
|
||||
|
||||
import "database/sql"
|
||||
|
||||
// SqlResult is execution result for sql operations.
|
||||
// It also supports batch operation result for rowsAffected.
|
||||
type SqlResult struct {
|
||||
result sql.Result
|
||||
affected int64
|
||||
}
|
||||
|
||||
// MustGetAffected returns the affected rows count, if any error occurs, it panics.
|
||||
func (r *SqlResult) MustGetAffected() int64 {
|
||||
rows, err := r.RowsAffected()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return rows
|
||||
}
|
||||
|
||||
// MustGetInsertId returns the last insert id, if any error occurs, it panics.
|
||||
func (r *SqlResult) MustGetInsertId() int64 {
|
||||
id, err := r.LastInsertId()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return id
|
||||
}
|
||||
|
||||
// see sql.Result.RowsAffected
|
||||
func (r *SqlResult) RowsAffected() (int64, error) {
|
||||
if r.affected > 0 {
|
||||
return r.affected, nil
|
||||
}
|
||||
return r.result.RowsAffected()
|
||||
}
|
||||
|
||||
// see sql.Result.LastInsertId
|
||||
func (r *SqlResult) LastInsertId() (int64, error) {
|
||||
return r.result.LastInsertId()
|
||||
}
|
||||
@ -33,14 +33,14 @@ func (tx *TX) Rollback() error {
|
||||
|
||||
// Query does query operation on transaction.
|
||||
// See Core.Query.
|
||||
func (tx *TX) Query(query string, args ...interface{}) (rows *sql.Rows, err error) {
|
||||
return tx.db.DoQuery(tx.tx, query, args...)
|
||||
func (tx *TX) Query(sql string, args ...interface{}) (rows *sql.Rows, err error) {
|
||||
return tx.db.DoQuery(tx.tx, sql, args...)
|
||||
}
|
||||
|
||||
// Exec does none query operation on transaction.
|
||||
// See Core.Exec.
|
||||
func (tx *TX) Exec(query string, args ...interface{}) (sql.Result, error) {
|
||||
return tx.db.DoExec(tx.tx, query, args...)
|
||||
func (tx *TX) Exec(sql string, args ...interface{}) (sql.Result, error) {
|
||||
return tx.db.DoExec(tx.tx, sql, args...)
|
||||
}
|
||||
|
||||
// Prepare creates a prepared statement for later queries or executions.
|
||||
@ -48,13 +48,13 @@ func (tx *TX) Exec(query string, args ...interface{}) (sql.Result, error) {
|
||||
// returned statement.
|
||||
// The caller must call the statement's Close method
|
||||
// when the statement is no longer needed.
|
||||
func (tx *TX) Prepare(query string) (*sql.Stmt, error) {
|
||||
return tx.db.DoPrepare(tx.tx, query)
|
||||
func (tx *TX) Prepare(sql string) (*sql.Stmt, error) {
|
||||
return tx.db.DoPrepare(tx.tx, sql)
|
||||
}
|
||||
|
||||
// GetAll queries and returns data records from database.
|
||||
func (tx *TX) GetAll(query string, args ...interface{}) (Result, error) {
|
||||
rows, err := tx.Query(query, args...)
|
||||
func (tx *TX) GetAll(sql string, args ...interface{}) (Result, error) {
|
||||
rows, err := tx.Query(sql, args...)
|
||||
if err != nil || rows == nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -63,8 +63,8 @@ func (tx *TX) GetAll(query string, args ...interface{}) (Result, error) {
|
||||
}
|
||||
|
||||
// GetOne queries and returns one record from database.
|
||||
func (tx *TX) GetOne(query string, args ...interface{}) (Record, error) {
|
||||
list, err := tx.GetAll(query, args...)
|
||||
func (tx *TX) GetOne(sql string, args ...interface{}) (Record, error) {
|
||||
list, err := tx.GetAll(sql, args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -76,8 +76,8 @@ func (tx *TX) GetOne(query string, args ...interface{}) (Record, error) {
|
||||
|
||||
// GetStruct queries one record from database and converts it to given struct.
|
||||
// The parameter <pointer> should be a pointer to struct.
|
||||
func (tx *TX) GetStruct(obj interface{}, query string, args ...interface{}) error {
|
||||
one, err := tx.GetOne(query, args...)
|
||||
func (tx *TX) GetStruct(obj interface{}, sql string, args ...interface{}) error {
|
||||
one, err := tx.GetOne(sql, args...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -86,8 +86,8 @@ func (tx *TX) GetStruct(obj interface{}, query string, args ...interface{}) erro
|
||||
|
||||
// GetStructs queries records from database and converts them to given struct.
|
||||
// The parameter <pointer> should be type of struct slice: []struct/[]*struct.
|
||||
func (tx *TX) GetStructs(objPointerSlice interface{}, query string, args ...interface{}) error {
|
||||
all, err := tx.GetAll(query, args...)
|
||||
func (tx *TX) GetStructs(objPointerSlice interface{}, sql string, args ...interface{}) error {
|
||||
all, err := tx.GetAll(sql, args...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -100,7 +100,7 @@ func (tx *TX) GetStructs(objPointerSlice interface{}, query string, args ...inte
|
||||
// If parameter <pointer> is type of struct pointer, it calls GetStruct internally for
|
||||
// the conversion. If parameter <pointer> is type of slice, it calls GetStructs internally
|
||||
// for conversion.
|
||||
func (tx *TX) GetScan(objPointer interface{}, query string, args ...interface{}) error {
|
||||
func (tx *TX) GetScan(objPointer interface{}, sql string, args ...interface{}) error {
|
||||
t := reflect.TypeOf(objPointer)
|
||||
k := t.Kind()
|
||||
if k != reflect.Ptr {
|
||||
@ -109,9 +109,9 @@ func (tx *TX) GetScan(objPointer interface{}, query string, args ...interface{})
|
||||
k = t.Elem().Kind()
|
||||
switch k {
|
||||
case reflect.Array, reflect.Slice:
|
||||
return tx.db.GetStructs(objPointer, query, args...)
|
||||
return tx.db.GetStructs(objPointer, sql, args...)
|
||||
case reflect.Struct:
|
||||
return tx.db.GetStruct(objPointer, query, args...)
|
||||
return tx.db.GetStruct(objPointer, sql, args...)
|
||||
default:
|
||||
return fmt.Errorf("element type should be type of struct/slice, unsupported: %v", k)
|
||||
}
|
||||
@ -121,8 +121,8 @@ func (tx *TX) GetScan(objPointer interface{}, query string, args ...interface{})
|
||||
// GetValue queries and returns the field value from database.
|
||||
// The sql should queries only one field from database, or else it returns only one
|
||||
// field of the result.
|
||||
func (tx *TX) GetValue(query string, args ...interface{}) (Value, error) {
|
||||
one, err := tx.GetOne(query, args...)
|
||||
func (tx *TX) GetValue(sql string, args ...interface{}) (Value, error) {
|
||||
one, err := tx.GetOne(sql, args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -133,11 +133,11 @@ func (tx *TX) GetValue(query string, args ...interface{}) (Value, error) {
|
||||
}
|
||||
|
||||
// GetCount queries and returns the count from database.
|
||||
func (tx *TX) GetCount(query string, args ...interface{}) (int, error) {
|
||||
if !gregex.IsMatchString(`(?i)SELECT\s+COUNT\(.+\)\s+FROM`, query) {
|
||||
query, _ = gregex.ReplaceString(`(?i)(SELECT)\s+(.+)\s+(FROM)`, `$1 COUNT($2) $3`, query)
|
||||
func (tx *TX) GetCount(sql string, args ...interface{}) (int, error) {
|
||||
if !gregex.IsMatchString(`(?i)SELECT\s+COUNT\(.+\)\s+FROM`, sql) {
|
||||
sql, _ = gregex.ReplaceString(`(?i)(SELECT)\s+(.+)\s+(FROM)`, `$1 COUNT($2) $3`, sql)
|
||||
}
|
||||
value, err := tx.GetValue(query, args...)
|
||||
value, err := tx.GetValue(sql, args...)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user