Skip to content

Commit 0281b0d

Browse files
committed
Start adding unit tests for cmdlib
1 parent 50ce00d commit 0281b0d

File tree

4 files changed

+75
-41
lines changed

4 files changed

+75
-41
lines changed

common/cmdlib.cpp

+2-37
Original file line numberDiff line numberDiff line change
@@ -560,14 +560,14 @@ static int _isspace(int c)
560560
// Trim whitespace from the start of a string
561561
std::string &TrimStringStart(std::string &s)
562562
{
563-
s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not_fn([](int c){ return _isspace(c); })));
563+
s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not_fn( _isspace )));
564564
return s;
565565
}
566566

567567
// Trim whitespace from the end of a string
568568
std::string &TrimStringEnd(std::string &s)
569569
{
570-
s.erase(std::find_if(s.rbegin(), s.rend(), std::not_fn([](int c){ return _isspace(c); })).base(), s.end());
570+
s.erase(std::find_if(s.rbegin(), s.rend(), std::not_fn( _isspace )).base(), s.end());
571571
return s;
572572
}
573573

@@ -751,39 +751,4 @@ uint32_t Log2(uint32_t n)
751751
return (t = (n >> 8)) ? 8 + LogTable256[t] : LogTable256[n];
752752
}
753753

754-
/**
755-
* This file has no copyright assigned and is placed in the Public Domain.
756-
* This file is part of the mingw-w64 runtime package.
757-
* No warranty is given; refer to the file DISCLAIMER.PD within this package.
758-
*/
759-
760-
/**
761-
* @brief Returns the next representable value of from in the direction of to.
762-
*/
763-
float NextAfter(const float from, const float to)
764-
{
765-
const float x = from;
766-
const float y = to;
767-
union {
768-
float f;
769-
unsigned int i;
770-
} u;
771-
if (isnan(y) || isnan(x))
772-
return x + y;
773-
if (x == y)
774-
/* nextafter (0.0, -O.0) should return -0.0. */
775-
return y;
776-
u.f = x;
777-
if (x == 0.0F)
778-
{
779-
u.i = 1;
780-
return y > 0.0F ? u.f : -u.f;
781-
}
782-
if (((x > 0.0F) ^ (y > x)) == 0)
783-
u.i++;
784-
else
785-
u.i--;
786-
return u.f;
787-
}
788-
789754
VERSION_CONTROL (cmdlib_cpp, "$Id$")

common/cmdlib.h

-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ void StripColorCodes(std::string& str);
106106
double Remap(const double value, const double low1, const double high1, const double low2,
107107
const double high2);
108108
uint32_t Log2(uint32_t n);
109-
float NextAfter(const float from, const float to);
110109

111110
/**
112111
* @brief Initialize an array with a specific value.

common/p_hordedefine.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ size_t P_HordePickDefine(const int current, const int total)
193193
const float section_size = static_cast<float>(::WAVE_DEFINES.size()) / total;
194194
const float section_offset = (current - 1) * section_size;
195195
const float section_choice = M_RandomFloat() * section_size;
196-
const float section_limit = NextAfter(section_offset + section_size, 0.0f);
196+
const float section_limit = nextafter(section_offset + section_size, 0.0f);
197197
return MIN<size_t>(section_offset + section_choice, section_limit);
198198
}
199199
else if (current <= 1)
@@ -372,9 +372,9 @@ BEGIN_COMMAND(hordedefine)
372372
const float section_size =
373373
static_cast<float>(::WAVE_DEFINES.size()) / total;
374374
const float section_offset = (current - 1) * section_size;
375-
const float section_choice = NextAfter(1.0f, 0.0f) * section_size;
375+
const float section_choice = nextafter(1.0f, 0.0f) * section_size;
376376
const float section_limit =
377-
NextAfter(section_offset + section_size, 0.0f);
377+
nextafter(section_offset + section_size, 0.0f);
378378
const size_t start = static_cast<size_t>(section_offset);
379379
const size_t end =
380380
MIN<size_t>(section_offset + section_choice, section_limit);

tests/unit-tests/test/t_cmdlib.cpp

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#include "gtest/gtest.h"
2+
#include "odamex.h"
3+
#include "cmdlib.h"
4+
5+
TEST(CmdLib, CopyString) {
6+
const char* str1 = "test";
7+
const char* str2 = copystring(str1);
8+
EXPECT_STREQ(str2, "test");
9+
EXPECT_STREQ(str2, str1);
10+
}
11+
12+
TEST(CmdLib, CopyNullString) {
13+
const char* str1 = nullptr;
14+
const char* str2 = copystring(str1);
15+
EXPECT_STREQ(str2, "");
16+
EXPECT_EQ(str2[0], '\0');
17+
}
18+
19+
TEST(CmdLib, CopyEmptyString) {
20+
const char* str1 = "";
21+
const char* str2 = copystring(str1);
22+
EXPECT_STREQ(str2, "");
23+
EXPECT_EQ(str2[0], '\0');
24+
}
25+
26+
TEST(CmdLib, CheckWildcardsNullArgs) {
27+
const char* str1 = nullptr;
28+
const char* str2 = "test";
29+
EXPECT_TRUE(CheckWildcards(str1, str2));
30+
str1 = "test";
31+
str2 = nullptr;
32+
EXPECT_TRUE(CheckWildcards(str1, str2));
33+
str1 = nullptr;
34+
str2 = nullptr;
35+
EXPECT_TRUE(CheckWildcards(str1, str2));
36+
}
37+
38+
TEST(CmdLib, TrimString) {
39+
// spaces
40+
std::string test = "hello ";
41+
EXPECT_EQ(TrimString(test), "hello");
42+
test = " hello";
43+
EXPECT_EQ(TrimString(test), "hello");
44+
test = " hello ";
45+
EXPECT_EQ(TrimString(test), "hello");
46+
// tabs
47+
test = " hello ";
48+
EXPECT_EQ(TrimString(test), "hello");
49+
// newlines
50+
test = "\nhello\n";
51+
EXPECT_EQ(TrimString(test), "hello");
52+
// carriage return
53+
test = "\rhello\r";
54+
EXPECT_EQ(TrimString(test), "hello");
55+
test = "\r\nhello\r\n";
56+
EXPECT_EQ(TrimString(test), "hello");
57+
// the other things
58+
test = "\vhello\v";
59+
EXPECT_EQ(TrimString(test), "hello");
60+
test = "\fhello\f";
61+
EXPECT_EQ(TrimString(test), "hello");
62+
test = "\rhello\r";
63+
EXPECT_EQ(TrimString(test), "hello");
64+
// everything
65+
test = "\r\v\f \n\thello\n \r\f \v";
66+
EXPECT_EQ(TrimString(test), "hello");
67+
// in the middle
68+
test = "he llo";
69+
EXPECT_EQ(TrimString(test), "he llo");
70+
}

0 commit comments

Comments
 (0)