FTXUI 7.0.0
C++ functional terminal UI.
Loading...
Searching...
No Matches
dom_layout_fuzzer.cpp
Go to the documentation of this file.
1// Copyright 2026 Arthur Sonzogni. All rights reserved.
2// Use of this source code is governed by the MIT license that can be found in
3// the LICENSE file.
4#include <cassert>
5#include <vector>
9
10using namespace ftxui;
11
12namespace {
13
14bool GeneratorBool(const char*& data, size_t& size) {
15 if (size == 0) {
16 return false;
17 }
18 auto out = bool(data[0] % 2);
19 data++;
20 size--;
21 return out;
22}
23
24std::string GeneratorString(const char*& data, size_t& size) {
25 int index = 0;
26 while (index < size && data[index]) {
27 ++index;
28 }
29 auto out = std::string(data, data + index);
30 data += index;
31 size -= index;
32 // Make sure it doesn't contain invalid UTF8
33 try {
34 to_wstring(out);
35 } catch (...) {
36 return "0";
37 }
38 return out;
39}
40
41int GeneratorInt(const char*& data, size_t& size) {
42 if (size == 0) {
43 return 0;
44 }
45 auto out = int(data[0]);
46 data++;
47 size--;
48 return out;
49}
50
51float GeneratorFloat(const char*& data, size_t& size) {
52 if (size == 0) {
53 return 0.0f;
54 }
55 auto out = float(data[0]) / 255.0f;
56 data++;
57 size--;
58 return out;
59}
60
61Elements GeneratorElements(const char*& data, size_t& size, int depth);
62
63Element GeneratorElement(const char*& data, size_t& size, int depth) {
64 depth--;
65 int value = GeneratorInt(data, size);
66 if (depth <= 0) {
67 int base_choice = value % 5;
68 if (base_choice == 0) {
69 return text(GeneratorString(data, size));
70 }
71 if (base_choice == 1) {
72 return paragraph(GeneratorString(data, size));
73 }
74 if (base_choice == 2) {
75 return separator();
76 }
77 if (base_choice == 3) {
78 return gauge(GeneratorFloat(data, size));
79 }
80 return emptyElement();
81 }
82
83 constexpr int value_max = 20;
84 value = (value % value_max + value_max) % value_max;
85 switch (value) {
86 case 0:
87 return text(GeneratorString(data, size));
88 case 1:
89 return paragraph(GeneratorString(data, size));
90 case 2:
91 return separator();
92 case 3:
93 return gauge(GeneratorFloat(data, size));
94 case 4:
95 return border(GeneratorElement(data, size, depth));
96 case 5:
97 return borderRounded(GeneratorElement(data, size, depth));
98 case 6:
99 return window(text(GeneratorString(data, size)),
100 GeneratorElement(data, size, depth));
101 case 7:
102 return vbox(GeneratorElements(data, size, depth));
103 case 8:
104 return hbox(GeneratorElements(data, size, depth));
105 case 9:
106 return dbox(GeneratorElements(data, size, depth));
107 case 10:
108 return flexbox(GeneratorElements(data, size, depth));
109 case 11:
110 return bold(GeneratorElement(data, size, depth));
111 case 12:
112 return dim(GeneratorElement(data, size, depth));
113 case 13:
114 return italic(GeneratorElement(data, size, depth));
115 case 14:
116 return inverted(GeneratorElement(data, size, depth));
117 case 15:
118 return underlined(GeneratorElement(data, size, depth));
119 case 16:
120 return blink(GeneratorElement(data, size, depth));
121 case 17:
122 return strikethrough(GeneratorElement(data, size, depth));
123 case 18:
124 return flex(GeneratorElement(data, size, depth));
125 case 19:
126 return filler();
127 default:
128 return emptyElement();
129 }
130}
131
132Elements GeneratorElements(const char*& data, size_t& size, int depth) {
133 Elements out;
134 if (depth > 0) {
135 while (size && (GeneratorInt(data, size) % 2)) {
136 out.push_back(GeneratorElement(data, size, depth - 1));
137 }
138 }
139 return out;
140}
141
142} // namespace
143
144extern "C" int LLVMFuzzerTestOneInput(const char* data, size_t size) {
145 if (size < 5) {
146 return 0;
147 }
148
149 int width = GeneratorInt(data, size);
150 int height = GeneratorInt(data, size);
151
152 width = (width % 200) + 1;
153 height = (height % 200) + 1;
154
155 int depth = 5;
156 auto element = GeneratorElement(data, size, depth);
157
158 auto screen =
159 Screen::Create(Dimension::Fixed(width), Dimension::Fixed(height));
160 Render(screen, element);
161 screen.ToString();
162
163 return 0;
164}
int LLVMFuzzerTestOneInput(const char *data, size_t size)
Element window(Element title, Element content, BorderStyle border=ROUNDED)
Draw window with a title and a border around the element.
Element text(std::string_view text)
Display a piece of UTF8 encoded unicode text.
Element flex(Element child)
Make a child element to expand proportionally to the space left in a container.
Definition flex.cpp:95
Element bold(Element child)
Use a bold font, for elements with more emphasis.
Definition bold.cpp:33
Element borderRounded(Element child)
Draw a rounded border around the element.
Element emptyElement()
Definition dom/util.cpp:140
Element underlined(Element child)
Underline the given element.
Element inverted(Element child)
Add a filter that will invert the foreground and the background colors.
Definition inverted.cpp:34
Element strikethrough(Element child)
Apply a strikethrough to text.
Element italic(Element child)
Apply a underlinedDouble to text.
Definition italic.cpp:17
Element dbox(Elements children_)
Stack several element on top of each other.
Element paragraph(std::string_view the_text)
Return an element drawing the paragraph on multiple lines.
Element separator()
Draw a vertical or horizontal separation in between two other elements.
Element filler()
An element that will expand proportionally to the space left in a container.
Definition flex.cpp:70
Element dim(Element child)
Use a light font, for elements with less emphasis.
Definition dim.cpp:33
Element blink(Element child)
The text drawn alternates in between visible and hidden.
Definition blink.cpp:33
Element gauge(float progress)
Draw a high definition progress bar.
Element border(Element child)
Draw a border around the element.
Element vbox(Elements children)
A container displaying elements vertically one by one.
Definition vbox.cpp:96
The FTXUI ftxui:: namespace.
Definition animation.hpp:11
Element flexbox(Elements, FlexboxConfig config=FlexboxConfig())
A container displaying elements on row/columns and capable of wrapping on the next column/row when fu...
Definition flexbox.cpp:252
std::shared_ptr< Node > Element
Definition elements.hpp:25
Element hbox(Elements children)
A container displaying elements horizontally one by one.
Definition hbox.cpp:94
std::vector< Element > Elements
Definition elements.hpp:26
FTXUI_EXPORT(SCREEN) std FTXUI_EXPORT(SCREEN) std std::wstring to_wstring(T s)
Definition string.hpp:18
const Element & element
Definition node.hpp:95
void Render(Screen &screen, Node *node, Selection &selection)
Definition node.cpp:105
int value
Definition elements.hpp:188