test-wrap.cpp
Go to the documentation of this file.
1 // License: Apache 2.0. See LICENSE file in root directory.
2 // Copyright(c) 2020 Intel Corporation. All Rights Reserved.
3 
4 //#cmake:add-file ../../../common/utilities/imgui/wrap.h
5 //#cmake:add-file ../../../common/utilities/imgui/wrap.cpp
6 //#cmake:add-file ../../../third-party/imgui/imgui.h
7 
8 #include "common.h"
9 #include "../../../common/utilities/imgui/wrap.h"
10 #include "../../../third-party/imgui/imgui.h"
11 
12 namespace ImGui {
13 // Mock ImGui function for test
14 // all characters are at length of 10 pixels except 'i' and 'j' which is 5 pixels
15 ImVec2 CalcTextSize( const char * text,
16  const char * text_end,
17  bool hide_text_after_double_hash,
18  float wrap_width )
19 {
20  if (!text) return ImVec2(0.0f, 0.0f);
21 
22  float total_size = 0.0f;
23  while( *text )
24  {
25  if( *text == 'i' || *text == 'j' )
26  total_size += 5.0f;
27  else
28  total_size += 10.0f;
29  text++;
30  }
31  return ImVec2( total_size, 0.0f );
32 }
33 } // namespace ImGui
34 
35 using namespace utilities::imgui;
36 
37 TEST_CASE( "wrap-text", "[string]" )
38 {
39  // Verify illegal inputs return empty string
40  CHECK( wrap( "", 0 ) == "" );
41  CHECK( wrap( "", 10 ) == "" );
42  CHECK( wrap( "abc", 0 ) == "abc" );
43  CHECK( wrap( "abc", 10 ) == "abc" );
44  CHECK( wrap( "abc\nabc", 0 ) == "abc\nabc" );
45  CHECK( wrap( "abc abc", 5 ) == "abc abc" );
46  CHECK( wrap( "", 10 ) == "" );
47 
48  // Verify no wrap if not needed
49  CHECK( wrap( "abc", 100 ) == "abc" );
50  CHECK( wrap( "abc\nabc", 100 ) == "abc\nabc" );
51  CHECK( wrap( "abc abc a", 100 ) == "abc abc a" );
52 
53  // No wrapping possible, copy line until first space and continue wrapping
54  CHECK( wrap( "abcdefgh", 40 ) == "abcdefgh" );
55  CHECK( wrap( "aabbccddff das ds fr", 50 ) == "aabbccddff\ndas\nds fr" );
56  CHECK( wrap( "das aabbccddff ds fr", 50 ) == "das\naabbccddff\nds fr" );
57 
58  // Exact wrap position test
59  CHECK( wrap( "abcde abcde", 50 ) == "abcde\nabcde" );
60 
61  // Short letters test
62  CHECK(wrap("aaaa bbbb cc", 100) == "aaaa bbbb\ncc");
63  // i and j are only 5 pixels so we get more characters inside the wrap
64  CHECK(wrap("aaaa iijj cc", 100) == "aaaa iijj cc");
65 
66 
67  // Check wrapping of 3 paragraphs
68  CHECK( wrap( "this is the first line\nthis is the second line\nthis is the last line", 150 )
69  == "this is the\nfirst line\nthis is the\nsecond line\nthis is the last\nline" );
70 
71  CHECK( wrap( "this is the first line\nthis is the second line\nthis is the last line", 60 )
72  == "this is\nthe\nfirst\nline\nthis is\nthe\nsecond\nline\nthis is\nthe\nlast\nline" );
73 
74  // Spaces checks
75  CHECK(wrap("ab cd ", 32) == "ab\ncd"); // Ending spaces
76  CHECK(wrap("ab cd", 32) == "ab\ncd"); // Middle spaces
77  CHECK(wrap(" ab cd ", 32) == "ab\ncd"); // Mixed multiple spaces
78  CHECK(wrap(" ab cd ", 32) == "ab\ncd"); // Mixed multiple spaces
79  CHECK(wrap(" ab ", 33) == "ab"); // Mixed multiple spaces
80  CHECK(wrap("ab ", 33) == "ab"); // Ending multiple spaces
81  CHECK(wrap(" ab", 33) == "ab");
82 
83  // Only spaces
84  CHECK(wrap(" ", 33) == "");
85  CHECK(wrap(" ", 33) == "");
86 
87  CHECK(wrap("ab cd ", 100) == "ab cd");
88  CHECK(wrap("ab cd", 100) == "ab cd"); // Known corner case - we trim multiple spaces
89 
90 }
Definition: imgui.h:88
GLdouble f
IMGUI_API ImVec2 CalcTextSize(const char *text, const char *text_end=NULL, bool hide_text_after_double_hash=false, float wrap_width=-1.0f)
Definition: test-wrap.cpp:15
TEST_CASE("wrap-text","[string]")
Definition: test-wrap.cpp:37
Definition: imgui.h:131
std::string wrap(const std::string &text, int wrap_pixels_width)
Definition: wrap.cpp:98
#define CHECK(condition)


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:50:11