Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
H
hashtable-open-addressing
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
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
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
David Nieder
hashtable-open-addressing
Commits
7183be26
Commit
7183be26
authored
2 years ago
by
David Nieder
Browse files
Options
Downloads
Patches
Plain Diff
some tests
parent
5a554820
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
tests/CMakeLists.txt
+1
-1
1 addition, 1 deletion
tests/CMakeLists.txt
tests/test_constructor.cpp
+58
-0
58 additions, 0 deletions
tests/test_constructor.cpp
tests/test_hashtable.cpp
+164
-0
164 additions, 0 deletions
tests/test_hashtable.cpp
with
223 additions
and
1 deletion
tests/CMakeLists.txt
+
1
−
1
View file @
7183be26
...
...
@@ -6,7 +6,7 @@ include(CTest)
# the executables that run the tests
add_executable
(
test_main test_main.cpp
)
add_executable
(
test_hashtable test_hashtable.cpp
)
add_executable
(
test_hashtable
test_constructor.cpp
test_hashtable.cpp
)
# link needed libraries
target_link_libraries
(
test_main gtest gtest_main
)
...
...
This diff is collapsed.
Click to expand it.
tests/test_constructor.cpp
0 → 100644
+
58
−
0
View file @
7183be26
#include
<gtest/gtest.h>
#include
"hashtable.h"
TEST
(
HashtableConstructor
,
DefaultParams
)
{
Hashtable
h
;
EXPECT_EQ
(
0
,
h
.
size
());
EXPECT_EQ
(
8
,
h
.
capacity
());
EXPECT_EQ
(
0
,
h
.
load_factor
());
}
TEST
(
HashtableConstructor
,
TableSizeZero
)
{
Hashtable
h
(
0
);
EXPECT_EQ
(
0
,
h
.
size
());
EXPECT_EQ
(
0
,
h
.
capacity
());
EXPECT_EQ
(
0
,
h
.
load_factor
());
}
TEST
(
HashtableConstructor
,
VariousTableSizes
)
{
Hashtable
h1
(
1
);
EXPECT_EQ
(
0
,
h1
.
size
());
EXPECT_EQ
(
1
,
h1
.
capacity
());
EXPECT_EQ
(
0
,
h1
.
load_factor
());
Hashtable
h2
(
42
);
EXPECT_EQ
(
0
,
h2
.
size
());
EXPECT_EQ
(
42
,
h2
.
capacity
());
EXPECT_EQ
(
0
,
h2
.
load_factor
());
Hashtable
h3
(
65535
);
EXPECT_EQ
(
0
,
h3
.
size
());
EXPECT_EQ
(
65535
,
h3
.
capacity
());
EXPECT_EQ
(
0
,
h3
.
load_factor
());
}
TEST
(
HashtableConstructor
,
ValidArguments
)
{
EXPECT_NO_THROW
(
Hashtable
h1
(
0
,
0.01
));
EXPECT_NO_THROW
(
Hashtable
h1
(
0
,
0.5
));
EXPECT_NO_THROW
(
Hashtable
h1
(
0
,
0.99
));
EXPECT_NO_THROW
(
Hashtable
h1
(
0
,
0.75
,
1.01
));
EXPECT_NO_THROW
(
Hashtable
h1
(
0
,
0.01
,
1.5
));
EXPECT_NO_THROW
(
Hashtable
h1
(
0
,
0.01
,
8
));
}
TEST
(
HashtableConstructor
,
InvalidArguments
)
{
EXPECT_THROW
(
Hashtable
h1
(
0
,
0
),
std
::
invalid_argument
);
EXPECT_THROW
(
Hashtable
h1
(
0
,
-
1.2
),
std
::
invalid_argument
);
EXPECT_THROW
(
Hashtable
h1
(
0
,
1.0
),
std
::
invalid_argument
);
EXPECT_THROW
(
Hashtable
h1
(
0
,
1.01
),
std
::
invalid_argument
);
EXPECT_THROW
(
Hashtable
h1
(
0
,
5
),
std
::
invalid_argument
);
EXPECT_THROW
(
Hashtable
h1
(
0
,
0.75
,
-
1
),
std
::
invalid_argument
);
EXPECT_THROW
(
Hashtable
h1
(
0
,
0.75
,
0
),
std
::
invalid_argument
);
EXPECT_THROW
(
Hashtable
h1
(
0
,
0.75
,
0.1
),
std
::
invalid_argument
);
EXPECT_THROW
(
Hashtable
h1
(
0
,
0.75
,
0.99
),
std
::
invalid_argument
);
EXPECT_THROW
(
Hashtable
h1
(
0
,
0.75
,
1
),
std
::
invalid_argument
);
}
This diff is collapsed.
Click to expand it.
tests/test_hashtable.cpp
+
164
−
0
View file @
7183be26
#include
<gtest/gtest.h>
#include
<iostream>
#include
"hashtable.h"
using
::
testing
::
TestWithParam
;
using
::
testing
::
ValuesIn
;
struct
HashtableArguments
{
size_t
m
;
float
a
;
float
s
;
// make error output readable
friend
std
::
ostream
&
operator
<<
(
std
::
ostream
&
os
,
const
HashtableArguments
&
args
)
{
return
os
<<
"[m: "
<<
args
.
m
<<
", a: "
<<
args
.
a
<<
", s: "
<<
args
.
s
<<
"]"
;
}
};
// below tests will run on hashtables contstructed with these arguments
HashtableArguments
argv
[]
=
{
{
8
,
0.75
,
2
},
// default table
{
0
,
0.75
,
2
},
// empty table
{
65536
,
0.75
,
2
},
// big table
{
8
,
0.10
,
2
},
// small alpha, default s
{
8
,
0.95
,
2
},
// large alpha, default s
{
8
,
0.10
,
1.1
},
// small alpha, tiny s
{
8
,
0.95
,
1.5
},
// large alpha, small s
{
8
,
0.10
,
4
},
// small alpha, large s
{
8
,
0.95
,
1.1
},
// large alpha, tiny s
{
8
,
0.10
,
1.5
},
// small alpha, small s
{
8
,
0.95
,
4
}
// large alpha, large s
};
class
PTest
:
public
TestWithParam
<
HashtableArguments
>
{
public:
Hashtable
*
table
;
size_t
m
;
float
a
;
float
s
;
void
SetUp
()
override
{
m
=
GetParam
().
m
;
a
=
GetParam
().
a
;
s
=
GetParam
().
s
;
table
=
new
Hashtable
(
m
,
a
,
s
);
}
void
TearDown
()
override
{
delete
table
;
table
=
nullptr
;
}
};
TEST_P
(
PTest
,
TestSetup
)
{
EXPECT_EQ
(
m
,
table
->
capacity
());
EXPECT_EQ
(
0
,
table
->
size
());
EXPECT_EQ
(
0
,
table
->
load_factor
());
}
TEST_P
(
PTest
,
InsertIntoEmpty
)
{
EXPECT_EQ
(
true
,
table
->
insert
(
0
));
EXPECT_EQ
(
true
,
table
->
insert
(
23
));
EXPECT_EQ
(
true
,
table
->
insert
(
512
));
}
TEST_P
(
PTest
,
InsertExisting
)
{
EXPECT_EQ
(
true
,
table
->
insert
(
2
));
EXPECT_EQ
(
true
,
table
->
insert
(
9
));
EXPECT_EQ
(
true
,
table
->
insert
(
32
));
EXPECT_EQ
(
true
,
table
->
insert
(
575
));
EXPECT_EQ
(
false
,
table
->
insert
(
2
));
EXPECT_EQ
(
false
,
table
->
insert
(
9
));
EXPECT_EQ
(
false
,
table
->
insert
(
32
));
EXPECT_EQ
(
false
,
table
->
insert
(
575
));
}
TEST_P
(
PTest
,
RemoveFromEmpty
)
{
EXPECT_EQ
(
false
,
table
->
remove
(
0
));
EXPECT_EQ
(
false
,
table
->
remove
(
7
));
EXPECT_EQ
(
false
,
table
->
remove
(
77
));
EXPECT_EQ
(
false
,
table
->
remove
(
2087
));
}
TEST_P
(
PTest
,
RemoveExisting
)
{
EXPECT_EQ
(
true
,
table
->
insert
(
4
));
EXPECT_EQ
(
true
,
table
->
insert
(
64
));
EXPECT_EQ
(
true
,
table
->
insert
(
1023
));
EXPECT_EQ
(
true
,
table
->
insert
(
408
));
EXPECT_EQ
(
true
,
table
->
remove
(
4
));
EXPECT_EQ
(
true
,
table
->
remove
(
64
));
EXPECT_EQ
(
true
,
table
->
remove
(
1023
));
EXPECT_EQ
(
true
,
table
->
remove
(
408
));
}
TEST_P
(
PTest
,
RemoveSome
)
{
for
(
int
i
=
0
;
i
<=
100
;
i
+=
2
)
{
EXPECT_EQ
(
true
,
table
->
insert
(
i
));
EXPECT_EQ
(
true
,
table
->
contains
(
i
));
EXPECT_EQ
(
false
,
table
->
contains
(
i
+
1
));
}
for
(
int
i
=
0
;
i
<=
100
;
i
+=
2
)
{
EXPECT_EQ
(
true
,
table
->
remove
(
i
));
EXPECT_EQ
(
false
,
table
->
remove
(
i
+
1
));
EXPECT_EQ
(
false
,
table
->
contains
(
i
));
}
}
TEST_P
(
PTest
,
Contains
)
{
EXPECT_EQ
(
false
,
table
->
contains
(
3
));
EXPECT_EQ
(
false
,
table
->
contains
(
53
));
EXPECT_EQ
(
false
,
table
->
contains
(
4265
));
EXPECT_EQ
(
false
,
table
->
contains
(
10e6
));
for
(
int
i
=
0
;
i
<
200
;
i
++
)
EXPECT_EQ
(
true
,
table
->
insert
(
i
));
for
(
int
i
=
0
;
i
<
200
;
i
++
)
EXPECT_EQ
(
true
,
table
->
contains
(
i
));
for
(
int
i
=
0
;
i
<
200
;
i
++
)
EXPECT_EQ
(
true
,
table
->
remove
(
i
));
for
(
int
i
=
0
;
i
<
200
;
i
++
)
EXPECT_EQ
(
false
,
table
->
contains
(
i
));
}
TEST_P
(
PTest
,
Size
)
{
EXPECT_EQ
(
0
,
table
->
size
());
table
->
insert
(
0
);
EXPECT_EQ
(
1
,
table
->
size
());
table
->
insert
(
23
);
EXPECT_EQ
(
2
,
table
->
size
());
table
->
remove
(
55
);
EXPECT_EQ
(
2
,
table
->
size
());
table
->
remove
(
80
);
EXPECT_EQ
(
2
,
table
->
size
());
table
->
remove
(
23
);
EXPECT_EQ
(
1
,
table
->
size
());
table
->
remove
(
0
);
EXPECT_EQ
(
0
,
table
->
size
());
for
(
int
i
=
0
;
i
<
200
;
i
++
)
{
EXPECT_EQ
(
i
,
table
->
size
());
table
->
insert
(
i
);
EXPECT_EQ
(
i
+
1
,
table
->
size
());
}
for
(
int
i
=
0
;
i
<
200
;
i
++
)
{
EXPECT_EQ
(
200
-
i
,
table
->
size
());
table
->
remove
(
i
);
EXPECT_EQ
(
199
-
i
,
table
->
size
());
}
}
TEST_P
(
PTest
,
Capacity
)
{
EXPECT_EQ
(
m
,
table
->
capacity
());
for
(
int
i
=
0
;
i
<
200
;
i
++
)
{
table
->
insert
(
i
);
EXPECT_GT
(
table
->
capacity
(),
table
->
size
());
}
}
INSTANTIATE_TEST_SUITE_P
(
ParameterizedTests
,
PTest
,
ValuesIn
(
argv
));
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