Merge pull request #4 from gogf/master

同步主库
This commit is contained in:
HaiLaz 2019-06-17 09:20:58 +08:00 committed by GitHub
commit 5896dadaad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 475 additions and 65 deletions

View File

@ -2,7 +2,7 @@
<div align=center>
<img src="https://goframe.org/logo.png" width="100"/>
[![Go Doc](https://godoc.org/github.com/gogf/gf?status.svg)](https://godoc.org/github.com/gogf/gf)
[![Go Doc](https://godoc.org/github.com/gogf/gf?status.svg)](https://godoc.org/github.com/gogf/gf/g#pkg-subdirectories)
[![Build Status](https://travis-ci.org/gogf/gf.svg?branch=master)](https://travis-ci.org/gogf/gf)
[![Go Report](https://goreportcard.com/badge/github.com/gogf/gf)](https://goreportcard.com/report/github.com/gogf/gf)
[![Code Coverage](https://codecov.io/gh/gogf/gf/branch/master/graph/badge.svg)](https://codecov.io/gh/gogf/gf/branch/master)

View File

@ -2,7 +2,7 @@
<div align=center>
<img src="https://goframe.org/logo.png" width="100"/>
[![Go Doc](https://godoc.org/github.com/gogf/gf?status.svg)](https://godoc.org/github.com/gogf/gf)
[![Go Doc](https://godoc.org/github.com/gogf/gf?status.svg)](https://godoc.org/github.com/gogf/gf/g#pkg-subdirectories)
[![Build Status](https://travis-ci.org/gogf/gf.svg?branch=master)](https://travis-ci.org/gogf/gf)
[![Go Report](https://goreportcard.com/badge/github.com/gogf/gf)](https://goreportcard.com/report/github.com/gogf/gf)
[![Code Coverage](https://codecov.io/gh/gogf/gf/branch/master/graph/badge.svg)](https://codecov.io/gh/gogf/gf/branch/master)

View File

@ -0,0 +1,29 @@
// Copyright 2018 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 gchan_test
import (
"fmt"
"github.com/gogf/gf/g/container/gchan"
)
func Example_basic() {
n := 10
c := gchan.New(n)
for i := 0; i < n; i++ {
c.Push(i)
}
fmt.Println(c.Len(), c.Cap())
for i := 0; i < n; i++ {
fmt.Print(c.Pop())
}
c.Close()
// Output:
//10 10
//0123456789
}

View File

@ -209,7 +209,7 @@ func (l *List) Len() (length int) {
return
}
// Alias of Len.
// Size is alias of Len.
func (l *List) Size() int {
return l.Len()
}

View File

@ -0,0 +1,36 @@
// Copyright 2018 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 glist_test
import (
"fmt"
"github.com/gogf/gf/g/container/glist"
)
func Example_basic() {
n := 10
l := glist.New()
for i := 0; i < n; i++ {
l.PushBack(i)
}
fmt.Println(l.Len())
fmt.Println(l.FrontAll())
fmt.Println(l.BackAll())
for i := 0; i < n; i++ {
fmt.Print(l.PopFront())
}
l.Clear()
fmt.Println()
fmt.Println(l.Len())
// Output:
//10
//[0 1 2 3 4 5 6 7 8 9]
//[9 8 7 6 5 4 3 2 1 0]
//0123456789
//0
}

View File

@ -20,14 +20,15 @@ package gqueue
import (
"github.com/gogf/gf/g/container/glist"
"github.com/gogf/gf/g/container/gtype"
"math"
)
type Queue struct {
limit int // Limit for queue size.
list *glist.List // Underlying list structure for data maintaining.
closed *gtype.Bool // Whether queue is closed.
events chan struct{} // Events for data writing.
closed chan struct{} // Events for queue closing.
C chan interface{} // Underlying channel for data reading.
}
@ -41,7 +42,7 @@ const (
// When <limit> is given, the queue will be static and high performance which is comparable with stdlib channel.
func New(limit...int) *Queue {
q := &Queue {
closed : make(chan struct{}, 0),
closed : gtype.NewBool(),
}
if len(limit) > 0 {
q.limit = limit[0]
@ -58,28 +59,27 @@ func New(limit...int) *Queue {
// startAsyncLoop starts an asynchronous goroutine,
// which handles the data synchronization from list <q.list> to channel <q.C>.
func (q *Queue) startAsyncLoop() {
for {
select {
case <- q.closed:
return
case <- q.events:
for {
if length := q.list.Len(); length > 0 {
array := make([]interface{}, length)
for i := 0; i < length; i++ {
if e := q.list.Front(); e != nil {
array[i] = q.list.Remove(e)
} else {
break
}
}
for _, v := range array {
q.C <- v
}
} else {
break
}
defer func() {
if q.closed.Val() {
_ = recover()
}
}()
for !q.closed.Val() {
<- q.events
if length := q.list.Len(); length > 0 {
array := make([]interface{}, length)
for i := 0; i < length; i++ {
if e := q.list.Front(); e != nil {
array[i] = q.list.Remove(e)
} else {
break
}
}
for _, v := range array {
// When q.C closes, it will panic here, especially q.C is being blocked for writing.
// It will be caught by recover and be ignored, if any error occurs here.
q.C <- v
}
}
}
}
@ -105,14 +105,25 @@ func (q *Queue) Pop() interface{} {
// Notice: It would notify all goroutines return immediately,
// which are being blocked reading using Pop method.
func (q *Queue) Close() {
close(q.C)
close(q.events)
close(q.closed)
q.closed.Set(true)
if q.events != nil {
close(q.events)
}
if q.C != nil {
close(q.C)
}
}
// Size returns the length of the queue.
// Len returns the length of the queue.
func (q *Queue) Len() (length int) {
if q.list != nil {
length += q.list.Len()
}
length += len(q.C)
return
}
// Size is alias of Len.
func (q *Queue) Size() int {
return len(q.C) + q.list.Len()
return q.Len()
}

View File

@ -503,9 +503,9 @@ func (tree *BTree) isLeaf(node *BTreeNode) bool {
return len(node.Children) == 0
}
func (tree *BTree) isFull(node *BTreeNode) bool {
return len(node.Entries) == tree.maxEntries()
}
//func (tree *BTree) isFull(node *BTreeNode) bool {
// return len(node.Entries) == tree.maxEntries()
//}
func (tree *BTree) shouldSplit(node *BTreeNode) bool {
return len(node.Entries) > tree.maxEntries()

View File

@ -0,0 +1,90 @@
// 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 empty_test
import (
"github.com/gogf/gf/g"
"github.com/gogf/gf/g/internal/empty"
"github.com/gogf/gf/g/test/gtest"
"github.com/gogf/gf/g/util/gconv"
"testing"
)
type TestPerson interface {
Say() string
}
type TestWoman struct {
}
func (woman TestWoman) Say() string {
return "nice"
}
func TestIsEmpty(t *testing.T) {
gtest.Case(t, func() {
tmpT1 := "0"
tmpT2 := func() {}
tmpT2 = nil
tmpT3 := make(chan int, 0)
var tmpT4 TestPerson = nil
var tmpT5 *TestPerson = nil
tmpF1 := "1"
tmpF2 := func(a string) string { return "1" }
tmpF3 := make(chan int, 1)
tmpF3 <- 1
var tmpF4 TestPerson = TestWoman{}
tmpF5 := &tmpF4
// true
gtest.Assert(empty.IsEmpty(nil), true)
gtest.Assert(empty.IsEmpty(gconv.Int(tmpT1)), true)
gtest.Assert(empty.IsEmpty(gconv.Int8(tmpT1)), true)
gtest.Assert(empty.IsEmpty(gconv.Int16(tmpT1)), true)
gtest.Assert(empty.IsEmpty(gconv.Int32(tmpT1)), true)
gtest.Assert(empty.IsEmpty(gconv.Int64(tmpT1)), true)
gtest.Assert(empty.IsEmpty(gconv.Uint64(tmpT1)), true)
gtest.Assert(empty.IsEmpty(gconv.Uint(tmpT1)), true)
gtest.Assert(empty.IsEmpty(gconv.Uint16(tmpT1)), true)
gtest.Assert(empty.IsEmpty(gconv.Uint32(tmpT1)), true)
gtest.Assert(empty.IsEmpty(gconv.Uint64(tmpT1)), true)
gtest.Assert(empty.IsEmpty(gconv.Float32(tmpT1)), true)
gtest.Assert(empty.IsEmpty(gconv.Float64(tmpT1)), true)
gtest.Assert(empty.IsEmpty(false), true)
gtest.Assert(empty.IsEmpty([]byte("")), true)
gtest.Assert(empty.IsEmpty(""), true)
gtest.Assert(empty.IsEmpty(g.Map{}), true)
gtest.Assert(empty.IsEmpty(g.Slice{}), true)
gtest.Assert(empty.IsEmpty(g.Array{}), true)
gtest.Assert(empty.IsEmpty(tmpT2), true)
gtest.Assert(empty.IsEmpty(tmpT3), true)
gtest.Assert(empty.IsEmpty(tmpT3), true)
gtest.Assert(empty.IsEmpty(tmpT4), true)
gtest.Assert(empty.IsEmpty(tmpT5), true)
// false
gtest.Assert(empty.IsEmpty(gconv.Int(tmpF1)), false)
gtest.Assert(empty.IsEmpty(gconv.Int8(tmpF1)), false)
gtest.Assert(empty.IsEmpty(gconv.Int16(tmpF1)), false)
gtest.Assert(empty.IsEmpty(gconv.Int32(tmpF1)), false)
gtest.Assert(empty.IsEmpty(gconv.Int64(tmpF1)), false)
gtest.Assert(empty.IsEmpty(gconv.Uint(tmpF1)), false)
gtest.Assert(empty.IsEmpty(gconv.Uint8(tmpF1)), false)
gtest.Assert(empty.IsEmpty(gconv.Uint16(tmpF1)), false)
gtest.Assert(empty.IsEmpty(gconv.Uint32(tmpF1)), false)
gtest.Assert(empty.IsEmpty(gconv.Uint64(tmpF1)), false)
gtest.Assert(empty.IsEmpty(gconv.Float32(tmpF1)), false)
gtest.Assert(empty.IsEmpty(gconv.Float64(tmpF1)), false)
gtest.Assert(empty.IsEmpty(true), false)
gtest.Assert(empty.IsEmpty(tmpT1), false)
gtest.Assert(empty.IsEmpty([]byte("1")), false)
gtest.Assert(empty.IsEmpty(g.Map{"a": 1}), false)
gtest.Assert(empty.IsEmpty(g.Slice{"1"}), false)
gtest.Assert(empty.IsEmpty(g.Array{"1"}), false)
gtest.Assert(empty.IsEmpty(tmpF2), false)
gtest.Assert(empty.IsEmpty(tmpF3), false)
gtest.Assert(empty.IsEmpty(tmpF4), false)
gtest.Assert(empty.IsEmpty(tmpF5), false)
})
}

View File

@ -0,0 +1,99 @@
// Copyright 2018 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 mutex_test
import (
"github.com/gogf/gf/g/container/garray"
"github.com/gogf/gf/g/internal/mutex"
"github.com/gogf/gf/g/test/gtest"
"testing"
"time"
)
func TestMutexIsSafe(t *testing.T) {
gtest.Case(t, func() {
lock := mutex.New()
gtest.Assert(lock.IsSafe(), true)
lock = mutex.New(false)
gtest.Assert(lock.IsSafe(), true)
lock = mutex.New(false, false)
gtest.Assert(lock.IsSafe(), true)
lock = mutex.New(true, false)
gtest.Assert(lock.IsSafe(), false)
lock = mutex.New(true, true)
gtest.Assert(lock.IsSafe(), false)
lock = mutex.New(true)
gtest.Assert(lock.IsSafe(), false)
})
}
func TestSafeMutex(t *testing.T) {
gtest.Case(t, func() {
safeLock := mutex.New(false)
array := garray.New()
go func() {
safeLock.Lock()
array.Append(1)
time.Sleep(100 * time.Millisecond)
array.Append(1)
safeLock.Unlock()
}()
go func() {
time.Sleep(10 * time.Millisecond)
safeLock.Lock()
array.Append(1)
time.Sleep(200 * time.Millisecond)
array.Append(1)
safeLock.Unlock()
}()
time.Sleep(50 * time.Millisecond)
gtest.Assert(array.Len(), 1)
time.Sleep(80 * time.Millisecond)
gtest.Assert(array.Len(), 3)
time.Sleep(100 * time.Millisecond)
gtest.Assert(array.Len(), 3)
time.Sleep(100 * time.Millisecond)
gtest.Assert(array.Len(), 4)
})
}
func TestUnsafeMutex(t *testing.T) {
gtest.Case(t, func() {
unsafeLock := mutex.New(true)
array := garray.New()
go func() {
unsafeLock.Lock()
array.Append(1)
time.Sleep(100 * time.Millisecond)
array.Append(1)
unsafeLock.Unlock()
}()
go func() {
time.Sleep(10 * time.Millisecond)
unsafeLock.Lock()
array.Append(1)
time.Sleep(200 * time.Millisecond)
array.Append(1)
unsafeLock.Unlock()
}()
time.Sleep(50 * time.Millisecond)
gtest.Assert(array.Len(), 2)
time.Sleep(100 * time.Millisecond)
gtest.Assert(array.Len(), 3)
time.Sleep(50 * time.Millisecond)
gtest.Assert(array.Len(), 3)
time.Sleep(100 * time.Millisecond)
gtest.Assert(array.Len(), 4)
})
}

View File

@ -0,0 +1,138 @@
// Copyright 2018 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 rwmutex_test
import (
"github.com/gogf/gf/g/container/garray"
"github.com/gogf/gf/g/internal/rwmutex"
"github.com/gogf/gf/g/test/gtest"
"testing"
"time"
)
func TestRwmutexIsSafe(t *testing.T) {
gtest.Case(t, func() {
lock := rwmutex.New()
gtest.Assert(lock.IsSafe(), true)
lock = rwmutex.New(false)
gtest.Assert(lock.IsSafe(), true)
lock = rwmutex.New(false, false)
gtest.Assert(lock.IsSafe(), true)
lock = rwmutex.New(true, false)
gtest.Assert(lock.IsSafe(), false)
lock = rwmutex.New(true, true)
gtest.Assert(lock.IsSafe(), false)
lock = rwmutex.New(true)
gtest.Assert(lock.IsSafe(), false)
})
}
func TestSafeRwmutex(t *testing.T) {
gtest.Case(t, func() {
safeLock := rwmutex.New()
array := garray.New()
go func() {
safeLock.Lock()
array.Append(1)
time.Sleep(100 * time.Millisecond)
array.Append(1)
safeLock.Unlock()
}()
go func() {
time.Sleep(10 * time.Millisecond)
safeLock.Lock()
array.Append(1)
time.Sleep(200 * time.Millisecond)
array.Append(1)
safeLock.Unlock()
}()
time.Sleep(50 * time.Millisecond)
gtest.Assert(array.Len(), 1)
time.Sleep(80 * time.Millisecond)
gtest.Assert(array.Len(), 3)
time.Sleep(100 * time.Millisecond)
gtest.Assert(array.Len(), 3)
time.Sleep(100 * time.Millisecond)
gtest.Assert(array.Len(), 4)
})
}
func TestSafeReaderRwmutex(t *testing.T) {
gtest.Case(t, func() {
safeLock := rwmutex.New()
array := garray.New()
go func() {
safeLock.RLock()
array.Append(1)
time.Sleep(100 * time.Millisecond)
array.Append(1)
safeLock.RUnlock()
}()
go func() {
time.Sleep(10 * time.Millisecond)
safeLock.RLock()
array.Append(1)
time.Sleep(200 * time.Millisecond)
array.Append(1)
time.Sleep(100 * time.Millisecond)
array.Append(1)
safeLock.RUnlock()
}()
go func() {
time.Sleep(50 * time.Millisecond)
safeLock.Lock()
array.Append(1)
safeLock.Unlock()
}()
time.Sleep(50 * time.Millisecond)
gtest.Assert(array.Len(), 2)
time.Sleep(100 * time.Millisecond)
gtest.Assert(array.Len(), 3)
time.Sleep(100 * time.Millisecond)
gtest.Assert(array.Len(), 4)
time.Sleep(100 * time.Millisecond)
gtest.Assert(array.Len(), 6)
})
}
func TestUnsafeRwmutex(t *testing.T) {
gtest.Case(t, func() {
unsafeLock := rwmutex.New(true)
array := garray.New()
go func() {
unsafeLock.Lock()
array.Append(1)
time.Sleep(100 * time.Millisecond)
array.Append(1)
unsafeLock.Unlock()
}()
go func() {
time.Sleep(10 * time.Millisecond)
unsafeLock.Lock()
array.Append(1)
time.Sleep(200 * time.Millisecond)
array.Append(1)
unsafeLock.Unlock()
}()
time.Sleep(50 * time.Millisecond)
gtest.Assert(array.Len(), 2)
time.Sleep(100 * time.Millisecond)
gtest.Assert(array.Len(), 3)
time.Sleep(50 * time.Millisecond)
gtest.Assert(array.Len(), 3)
time.Sleep(100 * time.Millisecond)
gtest.Assert(array.Len(), 4)
})
}

View File

@ -3,7 +3,6 @@
// 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.
// HTTP客户端请求返回.
package ghttp
@ -48,7 +47,7 @@ func (r *ClientResponse) ReadAllString() string {
}
// 关闭返回的HTTP链接
func (r *ClientResponse) Close() {
func (r *ClientResponse) Close() error {
r.Response.Close = true
r.Body.Close()
return r.Body.Close()
}

View File

@ -14,7 +14,6 @@ import (
"testing"
)
func Test_ValueAndOption(t *testing.T) {
os.Args = []string{"v1", "v2", "--o1=111", "-o2=222"}
doInit()
@ -23,6 +22,31 @@ func Test_ValueAndOption(t *testing.T) {
gtest.Assert(Value.Get(0), "v1")
gtest.Assert(Value.Get(1), "v2")
gtest.Assert(Value.Get(2), "")
gtest.Assert(Value.Get(2, "1"), "1")
gtest.Assert(Value.GetVar(1, "1").String(), "v2")
gtest.Assert(Value.GetVar(2, "1").String(), "1")
gtest.Assert(Option.GetAll(), map[string]string{"o1": "111", "o2": "222"})
gtest.Assert(Option.Get("o1"), "111")
gtest.Assert(Option.Get("o2"), "222")
gtest.Assert(Option.Get("o3", "1"), "1")
gtest.Assert(Option.GetVar("o2", "1").String(), "222")
gtest.Assert(Option.GetVar("o3", "1").String(), "1")
})
}
func Test_Handle(t *testing.T) {
os.Args = []string{"gf", "gf"}
doInit()
gtest.Case(t, func() {
num := 1
BindHandle("gf", func() {
num += 1
})
RunHandle("gf")
gtest.AssertEQ(num, 2)
AutoRun()
gtest.AssertEQ(num, 3)
})
}

View File

@ -1,33 +1,17 @@
package main
import (
"fmt"
"github.com/gogf/gf/g/container/gtree"
"github.com/gogf/gf/g/util/gutil"
"github.com/gogf/gf/g/container/gqueue"
"github.com/gogf/gf/g/test/gtest"
)
func main() {
expect := map[interface{}]interface{}{
20: "val20",
6: "val6",
10: "val10",
12: "val12",
1: "val1",
15: "val15",
19: "val19",
8: "val8",
4: "val4"}
m := gtree.NewAVLTreeFrom(gutil.ComparatorInt, expect)
m.Print()
max := 100
q := gqueue.New(max)
for i := 1; i < max; i++ {
q.Push(i)
}
q.Close()
gtest.Assert(q.Len(), 1)
//m := avltree.NewWithIntComparator()
//m.Remove()
fmt.Println(1, m.Remove(1))// 应该输出val1但输出nil
fmt.Println(2, m.Remove(1))
fmt.Println(3, m.Get(1))
fmt.Println(4, m.Remove(20))// 应该输出val20但输出nil
fmt.Println(5, m.Remove(20))
fmt.Println(6, m.Get(20))
}