Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
Current
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
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
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Yoan Zhao
Current
Commits
e307ecda
Commit
e307ecda
authored
10 months ago
by
Ari Trachtenberg
Browse files
Options
Downloads
Patches
Plain Diff
added tests
parent
be0fb183
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
tests/LabZero.cpp
+120
-4
120 additions, 4 deletions
tests/LabZero.cpp
with
120 additions
and
4 deletions
tests/LabZero.cpp
+
120
−
4
View file @
e307ecda
...
...
@@ -18,7 +18,17 @@ streambuf* orig_cout_buf; // records the standard output buffer
ostringstream
outStream
;
// captured output stream
void
labZero_pZero
();
// HELPERS
// Function to trim leading and trailing whitespace - from ChatGPT
string
trim
(
const
string
&
str
)
{
size_t
first
=
str
.
find_first_not_of
(
"
\t\n\r\f\v
"
);
if
(
first
==
std
::
string
::
npos
)
return
""
;
// The string is all whitespace or empty
size_t
last
=
str
.
find_last_not_of
(
"
\t\n\r\f\v
"
);
return
str
.
substr
(
first
,
(
last
-
first
+
1
));
}
/**
* Redirects input and ouput to strings
...
...
@@ -36,15 +46,18 @@ void redirIO(const string iStr) {
/**
* Restore input and output to STDIN/STDOUT, respectively.
* @return The output string that was captured since the redirect
* @return The output string that was captured since the redirect, trimmed of
* initial and final whitespace
*/
string
restoreIO
()
{
cin
.
rdbuf
(
orig_cin_buf
);
cout
.
rdbuf
(
orig_cout_buf
);
return
outStream
.
str
();
return
trim
(
outStream
.
str
()
)
;
}
// TESTS
bool
test_labZero_pZero
()
{
redirIO
(
"1
\n
2
\n
90
\n
"
);
// redirect input
labZero_pZero
();
...
...
@@ -55,9 +68,112 @@ bool test_labZero_pZero() {
else
{
return
false
;
}
}
bool
test_labZero_pOne
()
{
string
out
;
// example 1
redirIO
(
"0900
\n
1730
\n
"
);
labZero_pOne
();
out
=
restoreIO
();
if
(
out
!=
"8 hours 30 minutes"
)
{
return
false
;
}
// example 2
redirIO
(
"1730
\n
0900
\n
"
);
labZero_pOne
();
out
=
restoreIO
();
if
(
out
!=
"15 hours 30 minutes"
)
{
return
false
;
}
// passed all tests
return
true
;
}
bool
test_labZero_pTwo
()
{
redirIO
(
"123456"
);
labZero_pTwo
();
int
out
=
stoi
(
restoreIO
());
if
(
out
!=
21
)
return
false
;
return
true
;
}
bool
test_labZero_pThree
()
{
redirIO
(
"1 2 3 4"
);
labZero_pThree
();
string
out
=
restoreIO
();
if
(
out
!=
"in order"
)
return
false
;
return
true
;
}
bool
test_labZero_pFour
()
{
string
out
;
// example 1
redirIO
(
"0 0
\n
0 1
\n
1 0
\n
1 1
\n
"
);
labZero_pFour
();
out
=
restoreIO
();
if
(
out
!=
"square"
)
return
false
;
// example 2
redirIO
(
"0 0
\n
0 1
\n
1 0
\n
1 1
\n
"
);
labZero_pFour
();
out
=
restoreIO
();
if
(
out
!=
"trapezoid"
)
return
false
;
return
true
;
}
bool
test_labZero_pFive
()
{
string
out
;
// example 1
redirIO
(
"14
\n
"
);
labZero_pFive
();
out
=
restoreIO
();
if
(
out
!=
"XIV"
)
return
false
;
// example 2
redirIO
(
"1978
\n
"
);
labZero_pFive
();
out
=
restoreIO
();
if
(
out
!=
"MCMLXXVIII"
)
return
false
;
return
true
;
}
int
main
()
{
cout
<<
test_labZero_pZero
()
<<
endl
;
bool
t0
=
test_labZero_pZero
();
bool
t1
=
test_labZero_pOne
();
bool
t2
=
test_labZero_pTwo
();
bool
t3
=
test_labZero_pThree
();
bool
t4
=
test_labZero_pFour
();
bool
t5
=
test_labZero_pFive
();
cout
<<
"pZero: "
<<
(
t0
?
"passed"
:
"failed"
)
<<
endl
;
cout
<<
"pOne: "
<<
(
t1
?
"passed"
:
"failed"
)
<<
endl
;
cout
<<
"pTwo: "
<<
(
t2
?
"passed"
:
"failed"
)
<<
endl
;
cout
<<
"pThree: "
<<
(
t3
?
"passed"
:
"failed"
)
<<
endl
;
cout
<<
"pFour: "
<<
(
t4
?
"passed"
:
"failed"
)
<<
endl
;
cout
<<
"pFive: "
<<
(
t5
?
"passed"
:
"failed"
)
<<
endl
;
if
(
t0
&&
t1
&&
t2
&&
t3
&&
t4
&&
t5
)
{
// all tests passed
exit
(
0
);
// passed
}
else
{
exit
(
-
1
);
// failed
}
}
\ No newline at end of file
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