FTXUI  2.0.0
C++ functional terminal UI.
Loading...
Searching...
No Matches
component_fuzzer.cpp
Go to the documentation of this file.
1#include <iostream>
2#include <cassert>
3#include <vector>
6
7using namespace ftxui;
8namespace {
9
10bool GeneratorBool(const char*& data, size_t& size) {
11 if (size == 0)
12 return false;
13
14 auto out = bool(data[0] % 2);
15 data++;
16 size--;
17 return out;
18}
19
20std::string GeneratorString(const char*& data, size_t& size) {
21 int index = 0;
22 while (index < size && data[index])
23 ++index;
24
25 auto out = std::string(data, data + index);
26 data += index;
27 size -= index;
28
29 // The input component do not support invalid UTF8 yet.
30 try {
31 to_wstring(out);
32 } catch (...) {
33 return "0";
34 }
35 return std::move(out);
36}
37
38int GeneratorInt(const char* data, size_t size) {
39 if (size == 0)
40 return 0;
41 auto out = int(data[0]);
42 data++;
43 size--;
44 return out;
45}
46
47bool g_bool;
48int g_int;
49std::vector<std::string> g_list;
50
51Components GeneratorComponents(const char*& data, size_t& size, int depth);
52
53Component GeneratorComponent(const char*& data, size_t& size, int depth) {
54 depth--;
55 int value = GeneratorInt(data, size);
56 if (depth <= 0)
57 return Button(GeneratorString(data, size), [] {});
58
59 constexpr int value_max = 19;
60 value = (value % value_max + value_max) % value_max;
61 switch (value) {
62 case 0:
63 return Button(GeneratorString(data, size), [] {});
64 case 1:
65 return Checkbox(GeneratorString(data, size), &g_bool);
66 case 2:
67 return Input(GeneratorString(data, size), GeneratorString(data, size));
68 case 3:
69 return Menu(&g_list, &g_int);
70 case 4:
71 return Radiobox(&g_list, &g_int);
72 case 5:
73 return Toggle(&g_list, &g_int);
74 case 6:
75 return Slider(GeneratorString(data, size), &g_int,
76 GeneratorInt(data, size), GeneratorInt(data, size),
77 GeneratorInt(data, size));
78 case 7:
79 return ResizableSplitLeft(GeneratorComponent(data, size, depth - 1),
80 GeneratorComponent(data, size, depth - 1),
81 &g_int);
82 case 8:
83 return ResizableSplitRight(GeneratorComponent(data, size, depth - 1),
84 GeneratorComponent(data, size, depth - 1),
85 &g_int);
86 case 9:
87 return ResizableSplitTop(GeneratorComponent(data, size, depth - 1),
88 GeneratorComponent(data, size, depth - 1),
89 &g_int);
90 case 10:
91 return ResizableSplitBottom(GeneratorComponent(data, size, depth - 1),
92 GeneratorComponent(data, size, depth - 1),
93 &g_int);
94 case 11:
95 return Container::Vertical(GeneratorComponents(data, size, depth - 1));
96
97 case 12:
98 return Container::Vertical(GeneratorComponents(data, size, depth - 1),
99 &g_int);
100
101 case 13:
102 return Container::Horizontal(GeneratorComponents(data, size, depth - 1));
103 case 14:
104 return Container::Horizontal(GeneratorComponents(data, size, depth - 1),
105 &g_int);
106 case 15:
107 return Container::Tab(GeneratorComponents(data, size, depth - 1), &g_int);
108 case 16:
109 return Maybe(GeneratorComponent(data, size, depth - 1), &g_bool);
110 case 17:
111 return Dropdown(&g_list, &g_int);
112 case 18:
113 return Collapsible(GeneratorString(data, size),
114 GeneratorComponent(data, size, depth - 1),
115 GeneratorBool(data, size));
116 default:
117 assert(false);
118 }
119}
120
121Components GeneratorComponents(const char*& data, size_t& size, int depth) {
122 Components out;
123 if (depth > 0) {
124 while (size && GeneratorInt(data, size) % 2) {
125 out.push_back(GeneratorComponent(data, size, depth - 1));
126 }
127 }
128 return std::move(out);
129}
130
131} // namespace
132extern "C" int LLVMFuzzerTestOneInput(const char* data, size_t size) {
133 g_bool = GeneratorBool(data, size);
134 g_int = GeneratorInt(data, size);
135 g_list = {
136 "test_1", "test_2", "test_3", "test_4", "test_5",
137 };
138
139 int depth = 10;
140 auto component = GeneratorComponent(data, size, depth);
141
142 int width = GeneratorInt(data, size);
143 int height = GeneratorInt(data, size);
144
145 width %= 500;
146 width += 500;
147
148 height %= 500;
149 height += 500;
150
151 auto screen =
153
154 auto event_receiver = MakeReceiver<Event>();
155 {
156 auto parser = TerminalInputParser(event_receiver->MakeSender());
157 for (size_t i = 0; i < size; ++i)
158 parser.Add(data[i]);
159 }
160
161 Event event;
162 while (event_receiver->Receive(&event)) {
163 component->OnEvent(event);
164 auto document = component->Render();
165 Render(screen, document);
166 }
167 return 0; // Non-zero return values are reserved for future use.
168}
169
170// Copyright 2021 Arthur Sonzogni. All rights reserved.
171// Use of this source code is governed by the MIT license that can be found in
172// the LICENSE file.
static Screen Create(Dimensions dimension)
Create a screen with the given dimension.
Definition screen.cpp:348
int LLVMFuzzerTestOneInput(const char *data, size_t size)
Component Horizontal(Components children)
A list of components, drawn one by one horizontally and navigated horizontally using left/right arrow...
Component Vertical(Components children)
A list of components, drawn one by one vertically and navigated vertically using up/down arrow key or...
Component Tab(Components children, int *selector)
A list of components, where only one is drawn and interacted with at a time. The |selector| gives the...
Dimensions Fixed(int)
Component Checkbox(ConstStringRef label, bool *checked, Ref< CheckboxOption > option={})
Draw checkable element.
Definition checkbox.cpp:117
Component Radiobox(ConstStringListRef entries, int *selected_, Ref< RadioboxOption > option={})
A list of element, where only one can be selected.
Definition radiobox.cpp:206
Component Toggle(ConstStringListRef entries, int *selected, Ref< ToggleOption > option={})
An horizontal list of elements. The user can navigate through them.
Definition toggle.cpp:134
Component ResizableSplitTop(Component main, Component back, int *main_size)
An vertical split in between two components, configurable using the mouse.
Component Input(StringRef content, ConstStringRef placeholder, Ref< InputOption > option={})
An input box for editing text.
Definition input.cpp:278
std::vector< Component > Components
std::wstring to_wstring(const std::string &s)
Convert a std::wstring into a UTF8 std::string.
Definition string.cpp:397
Receiver< T > MakeReceiver()
Definition receiver.hpp:117
Component Button(ConstStringRef label, std::function< void()> on_click, Ref< ButtonOption >={})
Draw a button. Execute a function when clicked.
Definition button.cpp:90
Component Menu(ConstStringListRef entries, int *selected_, Ref< MenuOption >={})
A list of text. The focused element is selected.
Definition menu.cpp:189
Component Maybe(Component, const bool *show)
Definition maybe.cpp:12
Component ResizableSplitRight(Component main, Component back, int *main_size)
An horizontal split in between two components, configurable using the mouse.
Component Dropdown(ConstStringListRef entries, int *selected)
Definition dropdown.cpp:14
Component ResizableSplitBottom(Component main, Component back, int *main_size)
An vertical split in between two components, configurable using the mouse.
Component Slider(ConstStringRef label, T *value, T min, T max, T increment)
An horizontal slider.
Definition slider.cpp:123
void Render(Screen &screen, const Element &node)
Display an element on a ftxui::Screen.
Definition node.cpp:40
Decorator size(Direction, Constraint, int value)
Apply a constraint on the size of an element.
Definition size.cpp:86
Component ResizableSplitLeft(Component main, Component back, int *main_size)
An horizontal split in between two components, configurable using the mouse.
std::shared_ptr< ComponentBase > Component
Component Collapsible(ConstStringRef label, Component child, Ref< bool > show=false)
Represent an event. It can be key press event, a terminal resize, or more ...
Definition event.hpp:25