Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
cdo
Manage
Activity
Members
Labels
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Analyze
Contributor analytics
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
mpim-sw
cdo
Commits
b2f0f624
Commit
b2f0f624
authored
2 months ago
by
Oliver Heidmann
Committed by
Uwe Schulzweida
2 months ago
Browse files
Options
Downloads
Patches
Plain Diff
updated tests on convert_number
parent
8d45ed25
No related branches found
Branches containing commit
No related tags found
Tags containing commit
2 merge requests
!326
convert_to_number not called string_to_number, fixed usage of .fail enabling...
,
!322
Fixing and cleaning up handling of operator argument sourrounding the use of scientific notations
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
test/bandit_tests/util_string.cc
+121
-25
121 additions, 25 deletions
test/bandit_tests/util_string.cc
with
121 additions
and
25 deletions
test/bandit_tests/util_string.cc
+
121
−
25
View file @
b2f0f624
...
...
@@ -4,15 +4,124 @@
#include
"../../src/util_string.h"
#include
<string>
#include
<limits>
// std::reference_wrapper
#include
<tuple>
#include
<cmath>
using
namespace
snowhouse
;
void
cdoExit
()
{
}
template
<
typename
T
>
void
test_string_to_number_unsigned
()
{
T
num_max
=
std
::
numeric_limits
<
T
>::
max
();
std
::
stringstream
str_num_max
;
str_num_max
<<
std
::
setprecision
(
std
::
numeric_limits
<
T
>::
max_digits10
)
<<
num_max
;
T
num_min
=
std
::
numeric_limits
<
T
>::
min
();
std
::
stringstream
str_num_min
;
str_num_min
<<
std
::
setprecision
(
std
::
numeric_limits
<
T
>::
max_digits10
)
<<
num_min
;
std
::
vector
<
std
::
string
>
to_be_tested
;
std
::
vector
<
T
>
expected
;
to_be_tested
=
{
"1"
,
"0"
,
"120"
,
str_num_max
.
str
()
};
expected
=
{
1
,
0
,
120
,
std
::
numeric_limits
<
T
>::
max
()
};
for
(
std
::
size_t
i
=
0
;
i
<
to_be_tested
.
size
();
i
++
)
{
auto
msg
=
"correctly converts "
+
to_be_tested
[
i
]
+
" to "
+
std
::
to_string
(
expected
[
i
]);
bandit
::
it
(
msg
,
[
&
]()
{
bool
success
;
T
result
;
std
::
tie
(
success
,
result
)
=
string_to_number
<
T
>
(
to_be_tested
[
i
]);
AssertThat
(
success
,
Equals
(
true
));
AssertThat
(
result
,
Equals
(
expected
[
i
]));
});
}
}
template
<
typename
T
>
void
test_string_to_number
()
{
T
num_max
=
std
::
numeric_limits
<
T
>::
max
();
std
::
stringstream
str_num_max
;
str_num_max
<<
std
::
setprecision
(
std
::
numeric_limits
<
T
>::
max_digits10
)
<<
num_max
;
T
num_min
=
std
::
numeric_limits
<
T
>::
min
();
std
::
stringstream
str_num_min
;
str_num_min
<<
std
::
setprecision
(
std
::
numeric_limits
<
T
>::
max_digits10
)
<<
num_min
;
std
::
vector
<
std
::
string
>
to_be_tested
;
std
::
vector
<
T
>
expected
;
to_be_tested
=
{
"1"
,
"0"
,
str_num_max
.
str
(),
"-1"
,
str_num_min
.
str
()
};
expected
=
{
1
,
0
,
std
::
numeric_limits
<
T
>::
max
(),
-
1
,
std
::
numeric_limits
<
T
>::
min
()
};
for
(
std
::
size_t
i
=
0
;
i
<
to_be_tested
.
size
();
i
++
)
{
auto
msg
=
"correctly converts "
+
to_be_tested
[
i
]
+
" to "
+
std
::
to_string
(
expected
[
i
]);
bandit
::
it
(
msg
,
[
&
]()
{
bool
success
;
T
result
;
std
::
tie
(
success
,
result
)
=
string_to_number
<
T
>
(
to_be_tested
[
i
]);
AssertThat
(
success
,
Equals
(
true
));
AssertThat
(
result
,
Equals
(
expected
[
i
]));
});
}
}
template
<
typename
T
>
void
test_string_to_number_fl
()
{
T
num_max
=
std
::
numeric_limits
<
T
>::
max
();
std
::
stringstream
str_num_max
;
str_num_max
<<
std
::
setprecision
(
std
::
numeric_limits
<
T
>::
max_digits10
)
<<
num_max
;
T
num_min
=
std
::
numeric_limits
<
T
>::
min
();
std
::
stringstream
str_num_min
;
str_num_min
<<
std
::
setprecision
(
std
::
numeric_limits
<
T
>::
max_digits10
)
<<
num_min
;
std
::
vector
<
std
::
string
>
to_be_tested
=
{
"0.0"
,
"0"
,
"0.0e1"
,
"2.e3"
,
"2.0e3"
,
"2.0625"
,
str_num_max
.
str
(),
str_num_min
.
str
()
};
std
::
vector
<
T
>
expected
=
{
0
,
0
,
0
,
2000
,
2000
,
2.0625
,
num_max
,
num_min
};
for
(
std
::
size_t
i
=
0
;
i
<
to_be_tested
.
size
();
i
++
)
{
auto
msg
=
"correctly converts "
+
to_be_tested
[
i
]
+
" to "
+
get_scientific
(
expected
[
i
]);
bandit
::
it
(
msg
,
[
&
]()
{
bool
success
;
T
result
;
std
::
tie
(
success
,
result
)
=
string_to_number
<
T
>
(
to_be_tested
[
i
]);
AssertThat
(
success
,
Equals
(
true
));
AssertThat
(
result
,
Equals
(
expected
[
i
]));
});
}
}
template
<
typename
T
>
void
test_string_to_number_failures
()
{
std
::
vector
<
std
::
string
>
to_be_tested
=
{
"a0.02"
,
"2.e3a"
,
"2.a0e3"
,
"2ff.01f"
};
for
(
std
::
size_t
i
=
0
;
i
<
to_be_tested
.
size
();
i
++
)
{
auto
msg
=
"correctly throws error on "
+
to_be_tested
[
i
];
bandit
::
it
(
msg
,
[
&
]()
{
auto
[
success
,
result
]
=
string_to_number
<
T
>
(
to_be_tested
[
i
]);
AssertThat
(
success
,
Equals
(
false
));
AssertThat
(
std
::
isnan
(
result
),
Equals
(
true
));
});
}
}
go_bandit
([]()
{
//==============================================================================
//
//
bandit
::
describe
(
"Testing trim functions"
,
[]()
{
bandit
::
describe
(
"Testing ltrim function"
,
[]()
{
std
::
string
to_be_trimmed
=
" spaces in front "
;
...
...
@@ -24,32 +133,19 @@ go_bandit([]() {
});
});
bandit
::
describe
(
"Testing string_to_number"
,
[]()
{
bandit
::
describe
(
"Testing float"
,
[]()
{
std
::
string
test_str
=
"0.02"
;
float
expected
=
0.02
;
bandit
::
it
(
"correctly converts"
,
[
&
]()
{
auto
[
success
,
result
]
=
string_to_number
<
float
>
(
test_str
);
AssertThat
(
success
,
Equals
(
true
));
AssertThat
(
result
,
Equals
(
expected
));
});
});
bandit
::
describe
(
"Testing int"
,
[]()
{
std
::
string
test_str
=
"2"
;
int
expected
=
2
;
bandit
::
it
(
"correctly converts"
,
[
&
]()
{
auto
[
success
,
result
]
=
string_to_number
<
int
>
(
test_str
);
AssertThat
(
success
,
Equals
(
true
));
AssertThat
(
result
,
Equals
(
expected
));
});
bandit
::
describe
(
"Testing string_to_number positive"
,
[]()
{
bandit
::
describe
(
"Testing int"
,
[]()
{
test_string_to_number
<
int
>
();
});
bandit
::
describe
(
"Testing long"
,
[]()
{
test_string_to_number
<
long
>
();
});
bandit
::
describe
(
"Testing unsigned"
,
[]()
{
test_string_to_number_unsigned
<
unsigned
>
();
});
bandit
::
describe
(
"Testing float"
,
[]()
{
test_string_to_number_fl
<
float
>
();
});
bandit
::
describe
(
"Testing double"
,
[]()
{
test_string_to_number_fl
<
double
>
();
});
bandit
::
describe
(
"Testing long double"
,
[]()
{
test_string_to_number_fl
<
long
double
>
();
});
});
bandit
::
describe
(
"Testing double"
,
[]()
{
std
::
string
test_str
=
"0.02"
;
double
expected
=
0.02
;
bandit
::
it
(
"correctly converts"
,
[
&
]()
{
auto
[
success
,
result
]
=
string_to_number
<
double
>
(
test_str
);
AssertThat
(
success
,
Equals
(
true
));
AssertThat
(
result
,
Equals
(
expected
));
});
bandit
::
describe
(
"Testing string_to_number negatives"
,
[]()
{
bandit
::
describe
(
"Testing errors for float"
,
[]()
{
test_string_to_number_failures
<
float
>
();
});
bandit
::
describe
(
"Testing errors for double"
,
[]()
{
test_string_to_number_failures
<
double
>
();
});
bandit
::
describe
(
"Testing errors for long double"
,
[]()
{
test_string_to_number_failures
<
long
double
>
();
});
});
});
...
...
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