Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
B
beego
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
beego
Commits
189c7398
Commit
189c7398
authored
Dec 29, 2016
by
Faissal Elamraoui
Committed by
GitHub
Dec 29, 2016
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #2351 from amrfaissal/add-safemap-count
Add Count method to BeeMap struct
parents
99093693
fe21305b
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
43 additions
and
19 deletions
+43
-19
safemap.go
utils/safemap.go
+9
-4
safemap_test.go
utils/safemap_test.go
+34
-15
No files found.
utils/safemap.go
View file @
189c7398
...
...
@@ -61,10 +61,8 @@ func (m *BeeMap) Set(k interface{}, v interface{}) bool {
func
(
m
*
BeeMap
)
Check
(
k
interface
{})
bool
{
m
.
lock
.
RLock
()
defer
m
.
lock
.
RUnlock
()
if
_
,
ok
:=
m
.
bm
[
k
];
!
ok
{
return
false
}
return
true
_
,
ok
:=
m
.
bm
[
k
]
return
ok
}
// Delete the given key and value.
...
...
@@ -84,3 +82,10 @@ func (m *BeeMap) Items() map[interface{}]interface{} {
}
return
r
}
// Count returns the number of items within the map.
func
(
m
*
BeeMap
)
Count
()
int
{
m
.
lock
.
RLock
()
defer
m
.
lock
.
RUnlock
()
return
len
(
m
.
bm
)
}
utils/safemap_test.go
View file @
189c7398
...
...
@@ -14,25 +14,44 @@
package
utils
import
(
"testing"
)
func
Test_beemap
(
t
*
testing
.
T
)
{
bm
:=
NewBeeMap
()
if
!
bm
.
Set
(
"astaxie"
,
1
)
{
t
.
Error
(
"set Error"
)
import
"testing"
var
safeMap
*
BeeMap
func
TestNewBeeMap
(
t
*
testing
.
T
)
{
safeMap
=
NewBeeMap
()
if
safeMap
==
nil
{
t
.
Fatal
(
"expected to return non-nil BeeMap"
,
"got"
,
safeMap
)
}
}
func
TestSet
(
t
*
testing
.
T
)
{
if
ok
:=
safeMap
.
Set
(
"astaxie"
,
1
);
!
ok
{
t
.
Error
(
"expected"
,
true
,
"got"
,
false
)
}
}
func
TestCheck
(
t
*
testing
.
T
)
{
if
exists
:=
safeMap
.
Check
(
"astaxie"
);
!
exists
{
t
.
Error
(
"expected"
,
true
,
"got"
,
false
)
}
if
!
bm
.
Check
(
"astaxie"
)
{
t
.
Error
(
"check err"
)
}
func
TestGet
(
t
*
testing
.
T
)
{
if
val
:=
safeMap
.
Get
(
"astaxie"
);
val
.
(
int
)
!=
1
{
t
.
Error
(
"expected value"
,
1
,
"got"
,
val
)
}
}
if
v
:=
bm
.
Get
(
"astaxie"
);
v
.
(
int
)
!=
1
{
t
.
Error
(
"get err"
)
func
TestDelete
(
t
*
testing
.
T
)
{
safeMap
.
Delete
(
"astaxie"
)
if
exists
:=
safeMap
.
Check
(
"astaxie"
);
exists
{
t
.
Error
(
"expected element to be deleted"
)
}
}
bm
.
Delete
(
"astaxie"
)
if
bm
.
Check
(
"astaxie"
)
{
t
.
Error
(
"
delete err"
)
func
TestCount
(
t
*
testing
.
T
)
{
if
count
:=
safeMap
.
Count
();
count
!=
0
{
t
.
Error
(
"
expected count to be"
,
0
,
"got"
,
count
)
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment