File: json_roundtrip_int.cpp

package info (click to toggle)
glaze 7.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 10,316 kB
  • sloc: cpp: 170,219; sh: 109; ansic: 26; makefile: 12
file content (75 lines) | stat: -rw-r--r-- 1,583 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <glaze/glaze.hpp>
#include <vector>

// must be outside test() to work in gcc<14
template <typename T>
struct Value
{
   T value{};
};

template <typename T>
void test(const uint8_t* Data, size_t Size)
{
   using S = Value<T>;
   S s{};

   if (Size >= sizeof(T)) {
      std::memcpy(&s.value, Data, sizeof(T));
   }
   else {
      return;
   }

   // Test normal mode (to_chars_40kb - 40KB tables)
   {
      auto str = glz::write_json(s).value_or(std::string{});
      [[maybe_unused]] auto restored = glz::read_json<S>(str);
      assert(restored);
      assert(restored.value().value == s.value);
   }

   // Test size mode (to_chars - 400B tables)
   {
      auto str = glz::write<glz::opts_size{}>(s).value_or(std::string{});
      S restored{};
      [[maybe_unused]] auto ec = glz::read<glz::opts_size{}>(restored, str);
      assert(!ec);
      assert(restored.value == s.value);
   }
}

extern "C" int LLVMFuzzerTestOneInput(const uint8_t* Data, size_t Size)
{
   if (Size < 2) {
      return 0;
   }
   const auto action = Data[0];
   ++Data;
   --Size;

   switch (action & 0b11) {
   case 0:
      test<short>(Data, Size);
      test<unsigned short>(Data, Size);
      break;
   case 1:
      test<int>(Data, Size);
      test<unsigned int>(Data, Size);
      break;
   case 2:
      test<long>(Data, Size);
      test<unsigned long>(Data, Size);
      break;
   case 3:
      test<long long>(Data, Size);
      test<unsigned long long>(Data, Size);
      break;
   }

   return 0;
}