Skip to content

Commit

Permalink
utils/convert
Browse files Browse the repository at this point in the history
  • Loading branch information
KenjiTakahashi committed Mar 9, 2014
1 parent 9e63070 commit 1a80dce
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 1 deletion.
27 changes: 27 additions & 0 deletions src/utils/convert.cpp
@@ -0,0 +1,27 @@
/*
This is a part of newsoul @ http://github.com/KenjiTakahashi/newsoul
Karol "Kenji Takahashi" Woźniak © 2014
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "convert.h"

bool convert::string2bool(const std::string &str) {
return string::tolower(str) == "true";
}

int convert::string2int(const std::string &str) {
return atoi(str.c_str());
}
42 changes: 42 additions & 0 deletions src/utils/convert.h
@@ -0,0 +1,42 @@
/*
This is a part of newsoul @ http://github.com/KenjiTakahashi/newsoul
Karol "Kenji Takahashi" Woźniak © 2014
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef __UTILS_CONVERT_H__
#define __UTILS_CONVERT_H__

#include <cstdlib>
#include <string>
#include "string.h"

namespace convert {
/*!
* Converts string to bool, i.e. "true" is true and any other
* string is false. Case insensitive.
* \param str String to convert.
* \return true or false.
*/
bool string2bool(const std::string &str);
/*!
* Converts string to integer.
* \param str String to convert.
* \return An integer.
*/
int string2int(const std::string &str);
}

#endif /* __UTILS_CONVERT_H__ */
2 changes: 1 addition & 1 deletion src/utils/os.h
Expand Up @@ -57,4 +57,4 @@ namespace os {
const std::string config();
}

#endif // __UTILS_OS_H__
#endif /* __UTILS_OS_H__ */
49 changes: 49 additions & 0 deletions tests/utils/test_convert.cpp
@@ -0,0 +1,49 @@
/*
This is a part of newsoul @ http://github.com/KenjiTakahashi/newsoul
Karol "Kenji Takahashi" Woźniak © 2014
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include <CppUTest/TestHarness.h>
#include "../../src/utils/convert.h"

TEST_GROUP(string2bool) { };
TEST(string2bool, low_true) {
bool result = convert::string2bool("true");

CHECK_EQUAL(true, result);
}
TEST(string2bool, mixed_true) {
bool result = convert::string2bool("TrUe");

CHECK_EQUAL(true, result);
}
TEST(string2bool, false_) {
bool result = convert::string2bool("False");

CHECK_EQUAL(false, result);
}

TEST_GROUP(string2int) { };
TEST(string2int, succcess) {
int result = convert::string2int("123456");

CHECK_EQUAL(123456, result);
}
TEST(string2int, failure) {
int result = convert::string2int("NaN");

CHECK_EQUAL(0, result);
}

0 comments on commit 1a80dce

Please sign in to comment.