1*87c01607SMichael Jones //===-- Template to diff single-input-single-output functions ---*- C++ -*-===//
2*87c01607SMichael Jones //
3*87c01607SMichael Jones // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*87c01607SMichael Jones // See https://llvm.org/LICENSE.txt for license information.
5*87c01607SMichael Jones // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*87c01607SMichael Jones //
7*87c01607SMichael Jones //===----------------------------------------------------------------------===//
8*87c01607SMichael Jones 
9*87c01607SMichael Jones #ifndef LLVM_LIBC_FUZZING_STDLIB_STRING_PARSER_OUTPUT_DIFF_H
10*87c01607SMichael Jones #define LLVM_LIBC_FUZZING_STDLIB_STRING_PARSER_OUTPUT_DIFF_H
11*87c01607SMichael Jones 
12*87c01607SMichael Jones #include "fuzzing/math/Compare.h"
13*87c01607SMichael Jones 
14*87c01607SMichael Jones #include <stddef.h>
15*87c01607SMichael Jones #include <stdint.h>
16*87c01607SMichael Jones 
17*87c01607SMichael Jones template <typename T> using StringInputSingleOutputFunc = T (*)(const char *);
18*87c01607SMichael Jones 
19*87c01607SMichael Jones template <typename T>
StringParserOutputDiff(StringInputSingleOutputFunc<T> func1,StringInputSingleOutputFunc<T> func2,const uint8_t * data,size_t size)20*87c01607SMichael Jones void StringParserOutputDiff(StringInputSingleOutputFunc<T> func1,
21*87c01607SMichael Jones                             StringInputSingleOutputFunc<T> func2,
22*87c01607SMichael Jones                             const uint8_t *data, size_t size) {
23*87c01607SMichael Jones   if (size < sizeof(T))
24*87c01607SMichael Jones     return;
25*87c01607SMichael Jones 
26*87c01607SMichael Jones   const char *x = reinterpret_cast<const char *>(data);
27*87c01607SMichael Jones 
28*87c01607SMichael Jones   T result1 = func1(x);
29*87c01607SMichael Jones   T result2 = func2(x);
30*87c01607SMichael Jones 
31*87c01607SMichael Jones   if (!ValuesEqual(result1, result2))
32*87c01607SMichael Jones     __builtin_trap();
33*87c01607SMichael Jones }
34*87c01607SMichael Jones 
35*87c01607SMichael Jones #endif // LLVM_LIBC_FUZZING_STDLIB_STRING_PARSER_OUTPUT_DIFF_H
36