Skip to content

Instantly share code, notes, and snippets.

@uxdxdev
Last active January 27, 2023 16:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uxdxdev/20d458e5bc2ac2b092bb9bb7c1f312c7 to your computer and use it in GitHub Desktop.
Save uxdxdev/20d458e5bc2ac2b092bb9bb7c1f312c7 to your computer and use it in GitHub Desktop.
Unit tests for Utils class
// Copyright 2016 David Morton. All rights reserved.
// Use of this source code is governed by a license that can be
// found in the LICENSE file.
#include "gtest/gtest.h"
#include "Base/Utils.h"
TEST(UtilsTest, to_strCanConvertIntZeroToString) {
std::string result = Utils::to_str(0);
ASSERT_EQ("0", result);
}
TEST(UtilsTest, to_strCanConvertPositiveIntToString) {
std::string result = Utils::to_str(1);
ASSERT_EQ("1", result);
}
TEST(UtilsTest, to_strCanConvertNegativeIntToString) {
std::string result = Utils::to_str(-1);
ASSERT_EQ("-1", result);
}
TEST(UtilsTest, to_strCanConvertFloatToString) {
float floatNum = 123.987;
std::string result = Utils::to_str(floatNum);
ASSERT_EQ("123.987", result);
}
TEST(UtilsTest, to_strCanConvertDoubleToString) {
double doubleNum = 123.987;
std::string result = Utils::to_str(doubleNum);
ASSERT_EQ("123.987", result);
}
TEST(UtilsTest, to_intCanConvertZeroStringToInt) {
int result = Utils::to_int("0");
ASSERT_EQ(0, result);
}
TEST(UtilsTest, to_intCanConvertPositiveStringToInt) {
int result = Utils::to_int("1");
ASSERT_EQ(1, result);
}
TEST(UtilsTest, to_intCanConvertNegativeStringToInt) {
int result = Utils::to_int("-1");
ASSERT_EQ(-1, result);
}
TEST(UtilsTest, vec_to_stringCanConvertVecToString) {
std::vector<uint8_t> vectorOfBytes = {'t', 'e', 's', 't'};
std::string result = Utils::vec_to_string(vectorOfBytes);
ASSERT_EQ("test", result);
}
TEST(UtilsTest, str_to_vectorCanConvertStringToVec) {
std::string stringValue = "test";
std::vector<uint8_t> result = Utils::str_to_vector(stringValue);
ASSERT_EQ(4, result.size());
}
TEST(UtilsTest, milli_to_secCanConvertPositiveUnsignedLongLongToSeconds) {
unsigned long long milliseconds = 30000;
unsigned long long result = Utils::milli_to_sec(milliseconds);
ASSERT_EQ(30, result);
}
TEST(UtilsTest, milli_to_secCanConvertZeroToSeconds) {
unsigned long long milliseconds = 0;
unsigned long long result = Utils::milli_to_sec(milliseconds);
ASSERT_EQ(0, result);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment