Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
G
golang
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
golang
Commits
6498c1d4
Commit
6498c1d4
authored
Nov 11, 2010
by
Robert Griesemer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
sort.Search: added extra test to verify efficiency
R=r CC=golang-dev
https://golang.org/cl/3048041
parent
febde388
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
37 additions
and
0 deletions
+37
-0
search_test.go
src/pkg/sort/search_test.go
+37
-0
No files found.
src/pkg/sort/search_test.go
View file @
6498c1d4
...
...
@@ -55,6 +55,43 @@ func TestSearch(t *testing.T) {
}
// log2 computes the binary logarithm of x, rounded up to the next integer.
// (log2(0) == 0, log2(1) == 0, log2(2) == 1, log2(3) == 2, etc.)
//
func
log2
(
x
int
)
int
{
n
:=
0
for
p
:=
1
;
p
<
x
;
p
+=
p
{
// p == 2**n
n
++
}
// p/2 < x <= p == 2**n
return
n
}
func
TestSearchEfficiency
(
t
*
testing
.
T
)
{
n
:=
100
step
:=
1
for
exp
:=
2
;
exp
<
10
;
exp
++
{
// n == 10**exp
// step == 10**(exp-2)
max
:=
log2
(
n
)
for
x
:=
0
;
x
<
n
;
x
+=
step
{
count
:=
0
i
:=
Search
(
n
,
func
(
i
int
)
bool
{
count
++
;
return
i
<=
x
})
if
i
!=
x
{
t
.
Errorf
(
"n = %d: expected index %d; got %d"
,
n
,
x
,
i
)
}
if
count
>
max
{
t
.
Errorf
(
"n = %d, x = %d: expected <= %d calls; got %d"
,
n
,
x
,
max
,
count
)
}
}
n
*=
10
step
*=
10
}
}
// Smoke tests for convenience wrappers - not comprehensive.
var
fdata
=
[]
float
{
0
:
-
3.14
,
1
:
0
,
2
:
1
,
3
:
2
,
4
:
1000.7
}
...
...
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