// test_geometry.cpp - §13.3.1 geometry/colour/glyphs #include "../tttest.hpp" #include "../ttgui.hpp" using namespace ttgui; TEST_CASE("geometry: Rect intersect/union/clamp/inset", "[geometry][unit]"){ Rect a{0,0,10,10}, b{5,5,10,10}; auto i=a.intersect(b); CHECK_EQ(i.x,5); CHECK_EQ(i.y,5); CHECK_EQ(i.w,5); CHECK_EQ(i.h,5); auto u=a.unionRect(b); CHECK_EQ(u.w,15); CHECK_EQ(u.h,15); CHECK_EQ(u.x,0); Rect empty{0,0,0,0}; CHECK(empty.empty()); CHECK_EQ(a.intersect({20,20,2,2}).w,0); auto uni=empty.unionRect(a); CHECK_EQ(uni.w,10); Rect c=Rect{ -5,-5,20,20 }.clampInside({0,0,10,10}); CHECK_GE(c.x,0); CHECK_GE(c.y,0); CHECK_LE(c.right(),10); auto ins=a.inset(2,1); CHECK_EQ(ins.x,2); CHECK_EQ(ins.w,6); CHECK(a.contains({5,5})); CHECK_FALSE(a.contains({10,10})); CHECK_EQ(a.right(),10); CHECK_EQ(a.bottom(),10); } TEST_CASE("geometry: empty and negative-safe rects", "[geometry][unit]"){ Rect r{5,5,0,5}; CHECK(r.empty()); auto in=r.inset(10,10); CHECK_GE(in.w,0); CHECK_GE(in.h,0); Rect n{0,0,-3,-2}; CHECK(n.empty()); } TEST_CASE("colour: named/256/RGB parsing and invalid rejection", "[geometry][unit]"){ auto red=Color::named("red"); CHECK_EQ(red.r,205); auto hex=Color::named("#ff8000"); CHECK_EQ(hex.r,255); CHECK_EQ(hex.g,128); CHECK_EQ(hex.b,0); auto idx=Color::named("42"); CHECK_EQ(idx.idx,42); auto bad=Color::named("not-a-colour!"); CHECK(bad.isDefault()); auto rgb=Color::rgb(1,2,3); CHECK_EQ(rgb.g,2); Config cfg=Config::defaults(); CHECK_EQ(Config::colorToStr(rgb), std::string("#010203")); auto pc=Config::parseColor(json::Value("red")); CHECK_EQ(pc.r,205); auto pc2=Config::parseColor(json::Value(7)); CHECK_EQ(pc2.idx,7); } TEST_CASE("colour: TT palette 16 colours present", "[geometry][config][unit]"){ Config c=Config::defaults(); for(auto n: {"DarkBlue","PaleGreen","Pink","Yellow","Red","LightBlue","Green","DarkGreen","Blue","Cream","Mauve","Purple","Orange","Brown","Grey","White","Black"}) CHECK_FALSE(c.ttColor(n).isDefault()); CellStyle s; CHECK(s.fg.isDefault()); } TEST_CASE("glyph/utf8: wide and multibyte text occupies cells not bytes", "[geometry][unit]"){ Canvas cv(10,2); cv.drawText(0,0,"éx",{}); CHECK_EQ(cv.get(0,0).ch, std::string("é")); CHECK_EQ(cv.get(1,0).ch, std::string("x")); Canvas cv2(10,2); cv2.drawText(0,0,"▲▼",{}); CHECK_EQ(cv2.get(0,0).ch, std::string("▲")); CHECK_EQ(cv2.get(1,0).ch, std::string("▼")); CHECK_EQ(ellipsize("abcdefgh",5), std::string("ab...")); CHECK_EQ(ellipsize("abc",5), std::string("abc")); CHECK_EQ(ellipsize("abc",0), std::string("")); auto lines=wrapText("hello world foo",5); CHECK_GE(lines.size(),(size_t)2); }