[memprof] Move YAML support to MemProfYAML.h (NFC) (#119515)
[llvm-project.git] / libc / fuzzing / stdlib / StringParserOutputDiff.h
blob9e5accfdfc9866dedaef548582987b86bac5a51c
1 //===-- Template to diff single-input-single-output functions ---*- 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 #ifndef LLVM_LIBC_FUZZING_STDLIB_STRING_PARSER_OUTPUT_DIFF_H
10 #define LLVM_LIBC_FUZZING_STDLIB_STRING_PARSER_OUTPUT_DIFF_H
12 #include "fuzzing/math/Compare.h"
14 #include <stddef.h>
15 #include <stdint.h>
17 template <typename T> using StringInputSingleOutputFunc = T (*)(const char *);
19 template <typename T>
20 void StringParserOutputDiff(StringInputSingleOutputFunc<T> func1,
21 StringInputSingleOutputFunc<T> func2,
22 const uint8_t *data, size_t size) {
23 if (size < sizeof(T))
24 return;
26 const char *x = reinterpret_cast<const char *>(data);
28 T result1 = func1(x);
29 T result2 = func2(x);
31 if (!ValuesEqual(result1, result2))
32 __builtin_trap();
35 template <typename T>
36 using StringToNumberFunc = T (*)(const char *, char **, int);
38 template <typename T>
39 void StringToNumberOutputDiff(StringToNumberFunc<T> func1,
40 StringToNumberFunc<T> func2, const uint8_t *data,
41 size_t size) {
42 if (size < sizeof(T))
43 return;
45 const char *x = reinterpret_cast<const char *>(data + 1);
46 int base = data[0] % 36;
47 base = base + ((base == 0) ? 0 : 1);
49 char *outPtr1 = nullptr;
50 char *outPtr2 = nullptr;
52 T result1 = func1(x, &outPtr1, base);
53 T result2 = func2(x, &outPtr2, base);
55 if (!(ValuesEqual(result1, result2) && (*outPtr1 == *outPtr2)))
56 __builtin_trap();
59 #endif // LLVM_LIBC_FUZZING_STDLIB_STRING_PARSER_OUTPUT_DIFF_H