[libc] Switch to using the generic `<gpuintrin.h>` implementations (#121810)
[llvm-project.git] / libc / test / UnitTest / ScanfMatcher.cpp
blob3e9f2a5f941a10ab01b05bbad6d980fa149dbd27
1 //===-- ScanfMatcher.cpp ----------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #include "ScanfMatcher.h"
11 #include "src/__support/FPUtil/FPBits.h"
12 #include "src/__support/macros/config.h"
13 #include "src/stdio/scanf_core/core_structs.h"
15 #include "test/UnitTest/StringUtils.h"
16 #include "test/UnitTest/Test.h"
18 #include <stdint.h>
20 namespace LIBC_NAMESPACE_DECL {
21 namespace testing {
23 using scanf_core::FormatFlags;
24 using scanf_core::FormatSection;
25 using scanf_core::LengthModifier;
27 bool FormatSectionMatcher::match(FormatSection actualValue) {
28 actual = actualValue;
29 return expected == actual;
32 namespace {
34 #define IF_FLAG_SHOW_FLAG(flag_name) \
35 do { \
36 if ((form.flags & FormatFlags::flag_name) == FormatFlags::flag_name) \
37 tlog << "\n\t\t" << #flag_name; \
38 } while (false)
39 #define CASE_LM(lm) \
40 case (LengthModifier::lm): \
41 tlog << #lm; \
42 break
44 void display(FormatSection form) {
45 tlog << "Raw String (len " << form.raw_string.size() << "): \"";
46 for (size_t i = 0; i < form.raw_string.size(); ++i) {
47 tlog << form.raw_string[i];
49 tlog << "\"";
50 if (form.has_conv) {
51 tlog << "\n\tHas Conv\n\tFlags:";
52 IF_FLAG_SHOW_FLAG(NO_WRITE);
53 IF_FLAG_SHOW_FLAG(ALLOCATE);
54 tlog << "\n";
55 tlog << "\tmax width: " << form.max_width << "\n";
56 tlog << "\tlength modifier: ";
57 switch (form.length_modifier) {
58 CASE_LM(NONE);
59 CASE_LM(l);
60 CASE_LM(ll);
61 CASE_LM(h);
62 CASE_LM(hh);
63 CASE_LM(j);
64 CASE_LM(z);
65 CASE_LM(t);
66 CASE_LM(L);
68 tlog << "\n";
69 // If the pointer is used (NO_WRITE is not set and the conversion isn't %).
70 if (((form.flags & FormatFlags::NO_WRITE) == 0) &&
71 (form.conv_name != '%')) {
72 tlog << "\tpointer value: "
73 << int_to_hex<uintptr_t>(
74 reinterpret_cast<uintptr_t>(form.output_ptr))
75 << "\n";
78 tlog << "\tconversion name: " << form.conv_name << "\n";
80 if (form.conv_name == '[') {
81 tlog << "\t\t";
82 for (size_t i = 0; i < 256 /* char max */; ++i) {
83 if (form.scan_set.test(i)) {
84 tlog << static_cast<char>(i);
87 tlog << "\n\t]\n";
91 } // anonymous namespace
93 void FormatSectionMatcher::explainError() {
94 tlog << "expected format section: ";
95 display(expected);
96 tlog << '\n';
97 tlog << "actual format section : ";
98 display(actual);
99 tlog << '\n';
102 } // namespace testing
103 } // namespace LIBC_NAMESPACE_DECL