From 673f298d9d3c2c853b49b86d75a400c7e2e1f3b3 Mon Sep 17 00:00:00 2001 From: itsmattkc Date: Wed, 5 May 2021 20:15:06 +1000 Subject: [PATCH] actually commit new digit header and tests --- app/common/digit.h | 45 +++++++++++++++++++++++++++++++ tests/CMakeLists.txt | 2 +- tests/general/CMakeLists.txt | 1 + tests/general/common-tests.cpp | 49 ++++++++++++++++++++++++++++++++++ tests/testutil.h | 2 ++ 5 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 app/common/digit.h create mode 100644 tests/general/common-tests.cpp diff --git a/app/common/digit.h b/app/common/digit.h new file mode 100644 index 000000000..d4e208944 --- /dev/null +++ b/app/common/digit.h @@ -0,0 +1,45 @@ +/*** + + Olive - Non-Linear Video Editor + Copyright (C) 2021 Olive Team + + 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 . + +***/ + +#ifndef DIGIT_H +#define DIGIT_H + +#include + +namespace olive { + +inline int64_t GetDigitCount(int64_t input) +{ + input = std::abs(input); + + int64_t lim = 10; + int64_t digit = 1; + + while (input >= lim) { + lim *= 10; + digit++; + } + + return digit; +} + +} + +#endif // DIGIT_H diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 175c900eb..78dbfd7c2 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -23,7 +23,7 @@ function(olive_add_test GROUP NAME SOURCE) foreach (TEST_FUNC ${TEST_FUNCTIONS}) string(REPLACE "OLIVE_ADD_TEST(" "" TEST_FUNC "${TEST_FUNC}") string(APPEND TEST_BODY " std::cout << \"[${TEST_INDEX}/${TEST_COUNT}] ${GROUP} - ${TEST_FUNC}\";\n") - string(APPEND TEST_BODY " if (olive::Test${TEST_FUNC}()) {std::cout << \" - PASSED\" << std::endl;}else{std::cout << \" - FAILED\" << std::endl;return 1;\n}\n") + string(APPEND TEST_BODY " if (olive::Test${TEST_FUNC}()) {std::cout << \" - PASSED\" << std::endl;}else{std::cout << \" - FAILED\" << std::endl;return 1;}\n") MATH(EXPR TEST_INDEX "${TEST_INDEX}+1") endforeach() string(APPEND TEST_BODY " return 0;\n}") diff --git a/tests/general/CMakeLists.txt b/tests/general/CMakeLists.txt index 4b2c84b44..11f3472ed 100644 --- a/tests/general/CMakeLists.txt +++ b/tests/general/CMakeLists.txt @@ -14,4 +14,5 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . +olive_add_test(General common-tests common-tests.cpp) olive_add_test(General rational-tests rational-tests.cpp) diff --git a/tests/general/common-tests.cpp b/tests/general/common-tests.cpp new file mode 100644 index 000000000..be3414dab --- /dev/null +++ b/tests/general/common-tests.cpp @@ -0,0 +1,49 @@ +/*** + + Olive - Non-Linear Video Editor + Copyright (C) 2021 Olive Team + + 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 . + +***/ + +#include "testutil.h" + +#include "common/digit.h" + +namespace olive { + +OLIVE_ADD_TEST(DigitTest) +{ + OLIVE_ASSERT(GetDigitCount(1) == 1); + OLIVE_ASSERT(GetDigitCount(69) == 2); + OLIVE_ASSERT(GetDigitCount(420) == 3); + OLIVE_ASSERT(GetDigitCount(1337) == 4); + OLIVE_ASSERT(GetDigitCount(80085) == 5); + OLIVE_ASSERT(GetDigitCount(555555) == 6); + OLIVE_ASSERT(GetDigitCount(8675309) == 7); + OLIVE_ASSERT(GetDigitCount(78956423) == 8); + OLIVE_ASSERT(GetDigitCount(148497523) == 9); + OLIVE_ASSERT(GetDigitCount(4845821233) == 10); + OLIVE_ASSERT(GetDigitCount(18002738255) == 11); + OLIVE_ASSERT(GetDigitCount(180027382556) == 12); + OLIVE_ASSERT(GetDigitCount(1800273825568) == 13); + OLIVE_ASSERT(GetDigitCount(18002738255685) == 14); + OLIVE_ASSERT(GetDigitCount(180027382556857) == 15); + OLIVE_ASSERT(GetDigitCount(1800273825564857) == 16); + + OLIVE_TEST_END; +} + +} diff --git a/tests/testutil.h b/tests/testutil.h index e9c1013f2..9f916c2d0 100644 --- a/tests/testutil.h +++ b/tests/testutil.h @@ -18,6 +18,8 @@ ***/ +#include + #define OLIVE_ASSERT(x) if (!(x)) return false #define OLIVE_TEST_END return true