Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
2 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
 
#include "EpContextDataReadPolicy.h"
#include "LoadModelArgumentPolicy.h"
 
#include <array>
#include <cstdint>
#include <cstdio>
#include <limits>
#include <string>
 
namespace {
 
int g_failures = 0;
 
void check(bool condition, const std::string& what) {
  if (!condition) {
    ++g_failures;
    std::printf("FAILED: %s\n", what.c_str());
  }
}
 
void expectRejected(double raw, const std::string& what) {
  const auto result = onnxruntimejsi::parseMaxDataSize(raw);
  check(!result.ok, what);
  check(!result.error.empty(), what + " (reports an error)");
  check(result.value == 0, what + " (leaves the value zeroed)");
}
 
void expectAccepted(double raw, size_t expected, const std::string& what) {
  const auto result = onnxruntimejsi::parseMaxDataSize(raw);
  check(result.ok, what);
  check(result.error.empty(), what + " (reports no error)");
  check(result.value == expected, what + " (parses the value)");
}
 
// Kept out of line so a 32-bit build does not constant-fold a truncating cast in the branch that
// only ever runs on 64-bit targets.
size_t toSizeT(double value) { return static_cast<size_t>(value); }
 
void testParseMaxDataSize() {
  expectAccepted(1.0, 1, "accepts the smallest positive limit");
  expectAccepted(1024.0, 1024, "accepts a typical limit");
  expectAccepted(2147483648.0, toSizeT(2147483648.0),
                 "accepts a limit above INT32_MAX");
 
  expectRejected(0.0, "rejects zero");
  expectRejected(-0.0, "rejects negative zero");
  expectRejected(-1.0, "rejects a negative limit");
  expectRejected(1.5, "rejects a fractional limit");
  expectRejected(std::numeric_limits<double>::infinity(), "rejects infinity");
  expectRejected(-std::numeric_limits<double>::infinity(),
                 "rejects negative infinity");
  expectRejected(std::numeric_limits<double>::quiet_NaN(), "rejects NaN");
  expectRejected(onnxruntimejsi::kMaxSafeInteger + 2.0,
                 "rejects a value beyond Number.MAX_SAFE_INTEGER");
 
  // size_t is narrower than the safe-integer range on 32-bit targets, so the same JavaScript
  // number is accepted on 64-bit and rejected on 32-bit.
  if (sizeof(size_t) < sizeof(uint64_t)) {
    expectRejected(onnxruntimejsi::kMaxSafeInteger,
                   "rejects a limit that size_t cannot represent");
  } else {
    expectAccepted(onnxruntimejsi::kMaxSafeInteger,
                   toSizeT(onnxruntimejsi::kMaxSafeInteger),
                   "accepts Number.MAX_SAFE_INTEGER on 64-bit targets");
  }
}
 
void testCheckDataSize() {
  const auto underLimit = onnxruntimejsi::checkDataSize(10, 16, "ctx");
  check(underLimit.ok, "accepts a payload below the limit");
  check(underLimit.error.empty(), "accepts a payload below the limit quietly");
 
  const auto atLimit = onnxruntimejsi::checkDataSize(16, 16, "ctx");
  check(atLimit.ok, "accepts a payload exactly at the limit");
 
  const auto empty = onnxruntimejsi::checkDataSize(0, 1, "ctx");
  check(empty.ok, "accepts an empty payload");
 
  const auto overLimit = onnxruntimejsi::checkDataSize(17, 16, "ctx");
  check(!overLimit.ok, "rejects a payload above the limit");
  check(overLimit.error.find("ctx") != std::string::npos,
        "names the data in the limit error");
  check(overLimit.error.find("17") != std::string::npos,
        "reports the actual size in the limit error");
  check(overLimit.error.find("16") != std::string::npos,
        "reports the configured limit in the limit error");
}
 
void testLoadModelArgumentLayout() {
  using Type = onnxruntimejsi::LoadModelArgumentType;
 
  constexpr std::array pathArguments{Type::String, Type::Object};
  const auto pathLayout = onnxruntimejsi::resolveLoadModelArgumentLayout(
      pathArguments.data(), pathArguments.size());
  check(pathLayout.valid, "accepts the path overload");
  check(!pathLayout.usesModelBuffer, "identifies the path overload");
  check(pathLayout.optionsIndex == 1,
        "selects path options from argument 1");
 
  constexpr std::array bufferArguments{
      Type::ArrayBuffer, Type::Number, Type::Number, Type::Object};
  const auto bufferLayout = onnxruntimejsi::resolveLoadModelArgumentLayout(
      bufferArguments.data(), bufferArguments.size());
  check(bufferLayout.valid, "accepts the buffer overload");
  check(bufferLayout.usesModelBuffer, "identifies the buffer overload");
  check(bufferLayout.optionsIndex == 3,
        "selects buffer options from argument 3");
 
  constexpr std::array missingBufferOptions{
      Type::ArrayBuffer, Type::Number, Type::Number};
  check(!onnxruntimejsi::resolveLoadModelArgumentLayout(
             missingBufferOptions.data(), missingBufferOptions.size())
             .valid,
        "rejects a buffer overload without options");
 
  constexpr std::array wrongBufferOffset{
      Type::ArrayBuffer, Type::Object, Type::Number, Type::Object};
  check(!onnxruntimejsi::resolveLoadModelArgumentLayout(
             wrongBufferOffset.data(), wrongBufferOffset.size())
             .valid,
        "rejects a non-numeric buffer offset");
 
  constexpr std::array extraPathArgument{
      Type::String, Type::Object, Type::Other};
  check(!onnxruntimejsi::resolveLoadModelArgumentLayout(
             extraPathArgument.data(), extraPathArgument.size())
             .valid,
        "rejects extra path arguments");
}
 
}  // namespace
 
int main() {
  testParseMaxDataSize();
  testCheckDataSize();
  testLoadModelArgumentLayout();
 
  if (g_failures != 0) {
    std::printf("%d check(s) failed\n", g_failures);
    return 1;
  }
  std::printf("all checks passed\n");
  return 0;
}