Skip to content

Commit 81e636f

Browse files
committed
testing
1 parent 99686e9 commit 81e636f

3 files changed

Lines changed: 120 additions & 23 deletions

File tree

j2_library/include/j2_library/string/u8string.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,15 @@ namespace j2::string::u8
8181
u8str& from_u16string(const std::u16string& s16);
8282
u8str& from_u32string(const std::u32string& s32);
8383

84+
// 추가된 파싱/세터
85+
bool to_int64(int64_t& out) const;
86+
bool to_double(double& out) const;
87+
bool to_bool(bool& out) const;
88+
89+
u8str& from_int64(int64_t v);
90+
u8str& from_double(double v);
91+
u8str& from_bool(bool v);
92+
8493
// 검색 계열
8594
bool starts_with(const u8str& prefix) const;
8695
bool starts_with(const char* prefix_utf8) const;

j2_library/src/string/u8string.cpp

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
21
#include "j2_library/string/u8string.hpp"
2+
#include "j2_library/string/string_basic.hpp"
3+
#include "j2_library/string/to_string.hpp"
4+
#include <algorithm>
5+
#include <cctype>
36

47
#if defined(_WIN32)
58
#include <windows.h>
@@ -499,6 +502,51 @@ namespace j2::string::u8
499502
return *this;
500503
}
501504

505+
// --- 추가된 파서 / 세터 구현 ---
506+
bool u8str::to_int64(int64_t& out) const
507+
{
508+
std::string s = to_std_string();
509+
return j2::string::try_parse_int64(s, out);
510+
}
511+
512+
bool u8str::to_double(double& out) const
513+
{
514+
std::string s = to_std_string();
515+
return j2::string::try_parse_double(s, out);
516+
}
517+
518+
bool u8str::to_bool(bool& out) const
519+
{
520+
std::string s = to_std_string();
521+
std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c) -> unsigned char {
522+
return static_cast<unsigned char>(std::tolower(c));
523+
});
524+
if (s == "true") { out = true; return true; }
525+
if (s == "false") { out = false; return true; }
526+
return false;
527+
}
528+
529+
u8str& u8str::from_int64(int64_t v)
530+
{
531+
value_ = to_u8_string(std::to_string(v));
532+
return *this;
533+
}
534+
535+
u8str& u8str::from_double(double v)
536+
{
537+
// j2::string::to_string 템플릿을 사용하여 정밀도 일관성 확보
538+
std::string s = j2::string::to_string<double>(v);
539+
value_ = to_u8_string(s);
540+
return *this;
541+
}
542+
543+
u8str& u8str::from_bool(bool v)
544+
{
545+
value_ = to_u8_string(v ? "true" : "false");
546+
return *this;
547+
}
548+
// --- 추가된 파서 / 세터 구현 끝 ---
549+
502550
// starts_with / ends_with / contains
503551
bool u8str::starts_with(const u8str& prefix) const
504552
{

tests/test_u8string.cpp

Lines changed: 62 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
#include <gtest/gtest.h>
32

43
#ifdef _WIN32
@@ -399,28 +398,69 @@ TEST(u8str_algo, to_lower_upper)
399398
EXPECT_EQ(upper.to_std_string(), "HELLO WORLD!");
400399
}
401400

402-
// 연산자 테스트: +=, +, ==, !=
403-
TEST(u8str_operators, plus_and_compare)
401+
// -----------------------------------------------------------------------------
402+
// 추가된 파서/세터 기능에 대한 테스트
403+
// -----------------------------------------------------------------------------
404+
// to_int64 / from_int64 테스트
405+
TEST(u8str_parse, to_from_int64)
404406
{
405-
j2::string::u8::u8str a("Hello");
406-
j2::string::u8::u8str b("World");
407-
408-
a += " ";
409-
a += b;
410-
EXPECT_EQ(a.to_std_string(), "Hello World");
411-
412-
j2::string::u8::u8str c = ::u8("Hello") + " " + b;
413-
EXPECT_EQ(c.to_std_string(), "Hello World");
414-
415-
j2::string::u8::u8str d("Hello World");
416-
EXPECT_TRUE(c == d);
417-
EXPECT_FALSE(c != d);
407+
j2::string::u8::u8str s("1234567890");
408+
int64_t i = 0;
409+
EXPECT_TRUE(s.to_int64(i));
410+
EXPECT_EQ(i, 1234567890LL);
411+
412+
// 음수
413+
s.from_int64(-42);
414+
EXPECT_EQ(s.to_std_string(), std::to_string(-42));
415+
EXPECT_TRUE(s.to_int64(i));
416+
EXPECT_EQ(i, -42LL);
417+
418+
// 실패 케이스: 숫자와 잔여문자 존재
419+
s.from_cstr("12abc");
420+
EXPECT_FALSE(s.to_int64(i));
421+
}
418422

419-
j2::string::u8::u8str e("Different");
420-
EXPECT_FALSE(c == e);
421-
EXPECT_TRUE(c != e);
423+
// to_double / from_double 테스트
424+
TEST(u8str_parse, to_from_double)
425+
{
426+
j2::string::u8::u8str s("3.14159");
427+
double d = 0.0;
428+
EXPECT_TRUE(s.to_double(d));
429+
EXPECT_NEAR(d, 3.14159, 1e-12);
430+
431+
// 지수 표기
432+
s.from_cstr("1.2e3");
433+
EXPECT_TRUE(s.to_double(d));
434+
EXPECT_NEAR(d, 1200.0, 1e-12);
435+
436+
// from_double 로 설정 후 roundtrip
437+
s.from_double(-0.5);
438+
EXPECT_TRUE(s.to_double(d));
439+
EXPECT_NEAR(d, -0.5, 1e-12);
440+
441+
// 실패 케이스
442+
s.from_cstr("3.14abc");
443+
EXPECT_FALSE(s.to_double(d));
444+
}
422445

423-
// const char* + u8str 조합 테스트
424-
j2::string::u8::u8str f = "Say " + ::u8("Hi");
425-
EXPECT_EQ(f.to_std_string(), "Say Hi");
446+
// to_bool / from_bool 테스트
447+
TEST(u8str_parse, to_from_bool)
448+
{
449+
j2::string::u8::u8str s("true");
450+
bool b = false;
451+
EXPECT_TRUE(s.to_bool(b));
452+
EXPECT_TRUE(b);
453+
454+
s.from_cstr("FALSE");
455+
EXPECT_TRUE(s.to_bool(b));
456+
EXPECT_FALSE(b);
457+
458+
// from_bool 로 설정 후 roundtrip
459+
s.from_bool(true);
460+
EXPECT_TRUE(s.to_bool(b));
461+
EXPECT_TRUE(b);
462+
463+
// 실패 케이스: 허용되지 않는 표현
464+
s.from_cstr("yes");
465+
EXPECT_FALSE(s.to_bool(b));
426466
}

0 commit comments

Comments
 (0)