Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
O
ogs
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Yuhao Liu
ogs
Commits
bd13ce3e
Commit
bd13ce3e
authored
12 years ago
by
Dmitry Yu. Naumov
Browse files
Options
Downloads
Patches
Plain Diff
Add quicksort tests to check permutation vector.
parent
83e637ac
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
Tests/BaseLib/TestQuicksort.cpp
+72
-0
72 additions, 0 deletions
Tests/BaseLib/TestQuicksort.cpp
with
72 additions
and
0 deletions
Tests/BaseLib/TestQuicksort.cpp
+
72
−
0
View file @
bd13ce3e
...
...
@@ -17,6 +17,7 @@
#include
"quicksort.h"
#include
<algorithm>
#include
<sstream>
#include
<string>
#include
<vector>
...
...
@@ -58,3 +59,74 @@ TEST(BaseLib, QuicksortSortsCorrectly) {
ASSERT_TRUE
(
quicksortSortsAsSTLSort
.
check
());
}
// Permutations of sorted, unique vector remain untouched.
class
QuicksortCheckPermutations
:
public
Property
<
std
::
vector
<
int
>>
{
bool
accepts
(
const
std
::
vector
<
int
>&
xs
)
{
return
xs
.
size
()
>
2
&&
std
::
is_sorted
(
xs
.
begin
(),
xs
.
end
())
&&
(
std
::
adjacent_find
(
xs
.
begin
(),
xs
.
end
())
==
xs
.
end
());
}
bool
holdsFor
(
const
std
::
vector
<
int
>&
xs
)
{
std
::
vector
<
size_t
>
perm
(
xs
.
size
());
for
(
size_t
i
=
0
;
i
<
perm
.
size
();
++
i
)
perm
[
i
]
=
i
;
BaseLib
::
quicksort
((
int
*
)
&
(
xs
[
0
]),
0
,
xs
.
size
(),
&
(
perm
[
0
]));
for
(
size_t
i
=
0
;
i
<
perm
.
size
();
++
i
)
if
(
perm
[
i
]
!=
i
)
return
false
;
return
true
;
}
const
std
::
string
classify
(
const
std
::
vector
<
int
>&
xs
)
{
std
::
stringstream
ss
;
ss
<<
"size "
<<
xs
.
size
();
return
ss
.
str
();
}
};
TEST
(
BaseLib
,
QuicksortReportCorrectPermutations
)
{
QuicksortCheckPermutations
quicksortCheckPermutations
;
ASSERT_TRUE
(
quicksortCheckPermutations
.
check
(
200
,
100000
));
}
// Permutations of reverse sorted, unique vector is also reversed.
class
QuicksortCheckPermutationsReverse
:
public
Property
<
std
::
vector
<
int
>>
{
bool
accepts
(
const
std
::
vector
<
int
>&
xs
)
{
return
xs
.
size
()
>
2
&&
std
::
is_sorted
(
xs
.
rbegin
(),
xs
.
rend
())
&&
(
std
::
adjacent_find
(
xs
.
rbegin
(),
xs
.
rend
())
==
xs
.
rend
());
}
bool
holdsFor
(
const
std
::
vector
<
int
>&
xs
)
{
std
::
vector
<
size_t
>
perm
(
xs
.
size
());
for
(
size_t
i
=
0
;
i
<
perm
.
size
();
++
i
)
perm
[
i
]
=
i
;
BaseLib
::
quicksort
((
int
*
)
&
(
xs
[
0
]),
0
,
xs
.
size
(),
&
(
perm
[
0
]));
for
(
size_t
i
=
0
;
i
<
perm
.
size
();
++
i
)
if
(
perm
[
i
]
!=
perm
.
size
()
-
i
-
1
)
return
false
;
return
true
;
}
const
std
::
string
classify
(
const
std
::
vector
<
int
>&
xs
)
{
std
::
stringstream
ss
;
ss
<<
"size "
<<
xs
.
size
();
return
ss
.
str
();
}
};
TEST
(
BaseLib
,
QuicksortReportCorrectPermutationsReverse
)
{
QuicksortCheckPermutationsReverse
quicksortCheckPermutationsReverse
;
ASSERT_TRUE
(
quicksortCheckPermutationsReverse
.
check
(
200
,
100000
));
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment