Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
ccan
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
mirror
ccan
Commits
8f2bfcd3
Commit
8f2bfcd3
authored
Dec 07, 2010
by
Rusty Russell
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
rbtree: add tests (which currently fail)
parent
c5074939
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
149 additions
and
14 deletions
+149
-14
ccan/rbtree/test/run-many.c
ccan/rbtree/test/run-many.c
+89
-0
ccan/rbtree/test/run.c
ccan/rbtree/test/run.c
+60
-14
No files found.
ccan/rbtree/test/run-many.c
0 → 100644
View file @
8f2bfcd3
#include <ccan/rbtree/rbtree.c>
#include <ccan/tap/tap.h>
#include <ccan/talloc/talloc.h>
#include <string.h>
#include <stdbool.h>
#define NUM_ELEMS 10000
static
bool
lookup_all
(
trbt_tree_t
*
rb
,
bool
exist
)
{
unsigned
int
i
;
for
(
i
=
0
;
i
<
NUM_ELEMS
;
i
++
)
{
int
*
p
=
trbt_lookup32
(
rb
,
i
);
if
(
p
)
{
if
(
!
exist
)
return
false
;
if
(
*
p
!=
i
)
return
false
;
}
else
if
(
exist
)
return
false
;
}
return
true
;
}
static
bool
insert_all
(
trbt_tree_t
*
rb
,
bool
exist
)
{
unsigned
int
i
;
for
(
i
=
0
;
i
<
NUM_ELEMS
;
i
++
)
{
int
*
p
=
trbt_insert32
(
rb
,
i
,
talloc_memdup
(
rb
,
&
i
,
sizeof
(
i
)));
if
(
p
)
{
if
(
!
exist
)
return
false
;
if
(
*
p
!=
i
)
return
false
;
}
else
if
(
exist
)
return
false
;
}
return
true
;
}
static
void
delete_all
(
trbt_tree_t
*
rb
)
{
unsigned
int
i
;
for
(
i
=
0
;
i
<
NUM_ELEMS
;
i
++
)
{
trbt_delete32
(
rb
,
i
);
}
}
int
main
(
void
)
{
trbt_tree_t
*
rb
;
void
*
ctx
=
talloc_init
(
"toplevel"
);
unsigned
int
i
;
plan_tests
(
7
);
rb
=
trbt_create
(
ctx
,
0
);
ok1
(
rb
);
/* None should be there. */
ok1
(
lookup_all
(
rb
,
false
));
/* Insert, none should be there previously. */
ok1
(
insert_all
(
rb
,
false
));
/* All there now. */
ok1
(
lookup_all
(
rb
,
true
));
/* Replace all. */
ok1
(
insert_all
(
rb
,
true
));
/* Delete all. */
delete_all
(
rb
);
/* One more time... */
ok1
(
lookup_all
(
rb
,
false
));
ok1
(
insert_all
(
rb
,
false
));
/* All are children of rb, so this is clean. */
talloc_free
(
rb
);
/* This exits depending on whether all tests passed */
return
exit_status
();
}
ccan/rbtree/test/run.c
View file @
8f2bfcd3
#include <ccan/rbtree/rbtree.c>
#include <ccan/tap/tap.h>
#include <ccan/talloc/talloc.h>
#include <string.h>
static
void
*
insert_callback
(
void
*
param
,
void
*
data
)
{
ok1
(
data
==
param
);
return
talloc_strdup
(
NULL
,
"insert_callback"
);
}
int
main
(
void
)
{
trbt_tree_t
*
rb
;
void
*
ctx
=
talloc_init
(
"toplevel"
);
char
*
data
,
*
data2
;
/* This is how many tests you plan to run */
plan_tests
(
3
);
/* Simple thing we expect to succeed */
ok1
(
some_test
())
/* Same, with an explicit description of the test. */
ok
(
some_test
(),
"%s with no args should return 1"
,
"some_test"
)
/* How to print out messages for debugging. */
diag
(
"Address of some_test is %p"
,
&
some_test
)
/* Conditional tests must be explicitly skipped. */
#if HAVE_SOME_FEATURE
ok1
(
test_some_feature
())
#else
skip
(
1
,
"Don't have SOME_FEATURE"
)
#endif
plan_tests
(
18
);
rb
=
trbt_create
(
ctx
,
0
);
ok1
(
rb
);
ok1
(
talloc_is_parent
(
rb
,
ctx
));
/* Failed lookup. */
ok1
(
trbt_lookup32
(
rb
,
0
)
==
NULL
);
ok1
(
trbt_lookup32
(
rb
,
-
1
)
==
NULL
);
/* Insert, should steal node onto data. */
data
=
talloc_strdup
(
NULL
,
"data"
);
ok1
(
trbt_insert32
(
rb
,
0
,
data
)
==
NULL
);
ok1
(
trbt_lookup32
(
rb
,
0
)
==
data
);
ok1
(
trbt_lookup32
(
rb
,
-
1
)
==
NULL
);
/* Thus, freeing the data will delete the node. */
talloc_free
(
data
);
ok1
(
trbt_lookup32
(
rb
,
0
)
==
NULL
);
/* Try again. */
data
=
talloc_strdup
(
NULL
,
"data"
);
ok1
(
trbt_insert32
(
rb
,
0
,
data
)
==
NULL
);
/* Another insert should return old one. */
data2
=
talloc_strdup
(
NULL
,
"data2"
);
ok1
(
trbt_insert32
(
rb
,
0
,
data2
)
==
data
);
ok1
(
trbt_lookup32
(
rb
,
0
)
==
data2
);
/* Freeing old data has no effect. */
talloc_free
(
data
);
ok1
(
trbt_lookup32
(
rb
,
0
)
==
data2
);
/* Insert with callback on non-existing. */
trbt_insert32_callback
(
rb
,
1
,
insert_callback
,
NULL
);
ok1
(
strcmp
(
trbt_lookup32
(
rb
,
1
),
"insert_callback"
)
==
0
);
/* Insert with callback on existing. */
trbt_insert32_callback
(
rb
,
0
,
insert_callback
,
data2
);
ok1
(
strcmp
(
trbt_lookup32
(
rb
,
0
),
"insert_callback"
)
==
0
);
/* Delete. */
trbt_delete32
(
rb
,
1
);
ok1
(
trbt_lookup32
(
rb
,
1
)
==
NULL
);
ok1
(
trbt_lookup32
(
rb
,
0
));
/* This should free everything. */
talloc_free
(
trbt_lookup32
(
rb
,
0
));
talloc_free
(
rb
);
/* This exits depending on whether all tests passed */
return
exit_status
();
...
...
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