Non-autoregressive System 1 decision engine. Typed choice, score and yes/no decisions over any text in a single forward pass, in 100+ languages, with a router that picks the right checkpoint per request.
Python calibrationclassificationdecision-modelhuggingfacejevmodernbertmultilingualnlppythonpytorchroutingtyped-decisionszero-shot
using System.Diagnostics;
using System.Numerics;
using System.Text.Json;
namespace Laya.Tests;
/// <summary>
/// <see cref="PythonJsonReader"/> against CPython 3.12 <c>json.loads</c>, the warning repr of each JSON literal,
/// float parsing bit for bit, and the once-per-process warning filter. Every expectation below was produced by
/// running the Python reference (<c>json.loads</c>, <c>float</c>, <c>laya.common.clamp_temperature</c>,
/// <c>window_budget</c> under <c>warnings.simplefilter("always")</c>) on the same input; regenerate with the
/// scratch generator rather than editing by hand.
/// </summary>
public sealed class PythonJsonReaderTests
{
public static TheoryData<string, string?, bool> Accepts() => new()
{
{ "nan", null, false },
{ "NaN", "nan", true },
{ "+Infinity", null, false },
{ "Infinity", "inf", true },
{ "-Infinity", "-inf", true },
{ "-NaN", null, false },
{ "Infinityx", null, false },
{ "-Infinityx", null, false },
{ "01", null, false },
{ "-", null, false },
{ "-0", "0", true },
{ "-0.0", "-0.0", true },
{ "1.", null, false },
{ ".5", null, false },
{ "1e5", "100000.0", true },
{ "1E+2", "100.0", true },
{ "1e", null, false },
{ "1e+", null, false },
{ "0e0", "0.0", true },
{ "-01", null, false },
{ "[1,]", null, false },
{ "[,1]", null, false },
{ "[1 2]", null, false },
{ "[]", "[]", true },
{ "[ ]", "[]", true },
{ "{}", "{}", true },
{ "{\"a\":1,}", null, false },
{ "{\"a\" 1}", null, false },
{ "{'a': 1}", null, false },
{ "['a']", null, false },
{ "{NaN: 1}", null, false },
{ "{1: 1}", null, false },
{ "{\"a\": NaN}", "{'a': nan}", true },
{ "[NaN, Infinity, -Infinity]", "[nan, inf, -inf]", true },
{ "{\"a\": -Infinity, \"b\": [Infinity]}", "{'a': -inf, 'b': [inf]}", true },
{ "{\"a\": nan}", null, false },
{ "[nul]", null, false },
{ "[tru]", null, false },
{ "[True]", null, false },
{ "[null, true, false]", "[None, True, False]", true },
{ "\"abc", null, false },
{ "\"ab\\qc\"", null, false },
{ "\"\\ud83d\\ude00\"", "'\ud83d\ude00'", true },
{ "\"\\ud800\"", "'\\ud800'", true },
{ "\"\\u12\"", null, false },
{ "\"a\u0009b\"", null, false },
{ "\"a\\tb\"", "'a\\tb'", true },
{ "\"\u0001\"", null, false },
{ "/*c*/1", null, false },
{ "// c\u000a1", null, false },
{ "1 // c", null, false },
{ " \u0009\u000a\u000d[1]\u000d\u000a", "[1]", true },
{ "\u00a0[1]", null, false },
{ "[1]\u00a0", null, false },
{ "\ufeff[1]", null, false },
{ "", null, false },
{ " ", null, false },
{ "[1] x", null, false },
{ "[1][2]", null, false },
{ "{\"a\":1,\"a\":2}", "{'a': 2}", true },
{ "{\"a\":1,\"b\":2,\"a\":3}", "{'a': 3, 'b': 2}", true },
{ "123456789012345678901234567890", "123456789012345678901234567890", true },
{ "-123456789012345678901234567890", "-123456789012345678901234567890", true },
{ "1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111", null, true },
{ "11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111", null, false },
{ "9223372036854775807", "9223372036854775807", true },
{ "9223372036854775808", "9223372036854775808", true },
{ "-9223372036854775808", "-9223372036854775808", true },
{ "-9223372036854775809", "-9223372036854775809", true },
{ "1.0", "1.0", true },
{ "7.0", "7.0", true },
{ "0.1", "0.1", true },
{ "1e400", "inf", true },
{ "-1e400", "-inf", true },
{ "1e-400", "0.0", true },
{ "[[[[1]]]]", "[[[[1]]]]", true },
{ "{\"a\": {\"b\": [1, {\"c\": null}]}}", "{'a': {'b': [1, {'c': None}]}}", true },
{ "\"Infinity\"", "'Infinity'", true },
{ "\"NaN\"", "'NaN'", true },
{ "[\"\\ud83d\"]", "['\\ud83d']", true },
{ "{\"\\u00e9\": \"\\u00e9\"}", "{'\u00e9': '\u00e9'}", true },
{ "2.5e", null, false },
{ "2.5E-3", "0.0025", true },
{ "\u0663", null, false },
{ "[\u0661]", null, false },
{ "\uff11", null, false },
};
[Theory]
[MemberData(nameof(Accepts))]
public void AcceptsAndRejectsExactlyWhenPythonDoes(string text, string? repr, bool accepted)
{
if (!accepted)
{
Assert.ThrowsAny<JsonException>(() => PythonJsonReader.Parse(text));
return;
}
var value = PythonJsonReader.Parse(text);
if (repr is not null) Assert.Equal(repr, PythonFloat.Repr(value));
}
public static TheoryData<string, long, string> Floats() => new()
{
{ "2.2250738585072011e-308", 4503599627370495L, "2.225073858507201e-308" },
{ "2.2250738585072014e-308", 4503599627370496L, "2.2250738585072014e-308" },
{ "0.1", 4591870180066957722L, "0.1" },
{ "0.3", 4599075939470750515L, "0.3" },
{ "0.30000000000000004", 4599075939470750516L, "0.30000000000000004" },
{ "1.7976931348623157e308", 9218868437227405311L, "1.7976931348623157e+308" },
{ "1.7976931348623158e308", 9218868437227405311L, "1.7976931348623157e+308" },
{ "1.7976931348623159e308", 9218868437227405312L, "inf" },
{ "9007199254740993.0", 4845873199050653696L, "9007199254740992.0" },
{ "9007199254740993e0", 4845873199050653696L, "9007199254740992.0" },
{ "9007199254740992.5", 4845873199050653696L, "9007199254740992.0" },
{ "5e-324", 1L, "5e-324" },
{ "4.9406564584124654e-324", 1L, "5e-324" },
{ "2.4703282292062327e-324", 0L, "0.0" },
{ "2.4703282292062328e-324", 1L, "5e-324" },
{ "1e-323", 2L, "1e-323" },
{ "1.00000000000000011102230246251565404236316680908203125", 4607182418800017408L, "1.0" },
{ "1.00000000000000011102230246251565404236316680908203126", 4607182418800017409L, "1.0000000000000002" },
{ "8.41e21", 4934958393344299186L, "8.41e+21" },
{ "7.0e-10", 4469837215314003459L, "7e-10" },
{ "123456789012345678901234567890e-10", 4892433759222981601L, "1.2345678901234567e+19" },
{ "4.35", 4616583683022153318L, "4.35" },
{ "0.000001", 4517329193108106637L, "1e-06" },
{ "1e22", 4936209963552724370L, "1e+22" },
{ "1e23", 4950912855330343670L, "1e+23" },
{ "1e21", 4921056587992461136L, "1e+21" },
{ "1e-5", 4532020583610935537L, "1e-05" },
{ "1e16", 4846369599423283200L, "1e+16" },
{ "1e15", 4831355200913801216L, "1000000000000000.0" },
{ "123456789.123456789", 4728057454355442549L, "123456789.12345679" },
{ "-0.0", -9223372036854775808L, "-0.0" },
{ "0.0", 0L, "0.0" },
{ "100.0", 4636737291354636288L, "100.0" },
{ "2.5", 4612811918334230528L, "2.5" },
{ "0.5", 4602678819172646912L, "0.5" },
{ "5.0", 4617315517961601024L, "5.0" },
{ "1.5e300", 9097811302482466869L, "1.5e+300" },
{ "3.14159265358979323846264338327950288", 4614256656552045848L, "3.141592653589793" },
{ "10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.0", 9218868437227405312L, "inf" },
{ "0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001", 0L, "0.0" },
{ "1e308", 9214871658872686752L, "1e+308" },
{ "1e309", 9218868437227405312L, "inf" },
{ "589.773e-66", 3661160551921729496L, "5.89773e-64" },
{ "9890240870969119.84e-47", 4143325121061772819L, "9.89024087096912e-32" },
{ "70.0033592", 4634626465411991810L, "70.0033592" },
{ "98725432407308880.76", 4861050737830851493L, "9.872543240730888e+16" },
{ "5761058061281496373e+107", 6488345599367672440L, "5.761058061281496e+125" },
{ "-21409026285024355765949748639e-68", -5209918890524461499L, "-2.1409026285024356e-40" },
{ "-49558865774533561827692011.0e-247", -7927274691076673688L, "-4.955886577453356e-222" },
{ "1070241417.79", 4742261044085595832L, "1070241417.79" },
{ "142973103200e-146", 2589746254242631162L, "1.429731032e-135" },
{ "-245586195999701292.0e+169", -1827881155231506903L, "-2.455861959997013e+186" },
{ "5515451689549098e+38", 5410802935147893406L, "5.515451689549098e+53" },
{ "2174151.70638386378173234614e-39", 4118141512346634435L, "2.174151706383864e-33" },
{ "-90052157802223625946388384098.0e+210", -1041553556867938829L, "-9.005215780222362e+238" },
{ "18.577544364e+269", 8650326167602130540L, "1.8577544364e+270" },
{ "-343.379e+302", -60506935699092264L, "-3.43379e+304" },
{ "1094e+102", 6178524366699236661L, "1.094e+105" },
{ "93468655916.186969246451", 4770933967592107997L, "93468655916.18697" },
{ "4.6", 4616865157998863974L, "4.6" },
{ "1036300192629047180132.0", 4921333536483588510L, "1.0363001926290472e+21" },
{ "-6482203456254545.5", -4380023833602608558L, "-6482203456254546.0" },
{ "584501114346614378.0e+246", 8553056883673417103L, "5.845011143466144e+263" },
{ "-812935950329914212297.5", -4303742634299861103L, "-8.129359503299142e+20" },
{ "756128707606555e+218", 8090815721794448434L, "7.56128707606555e+232" },
{ "8.3454822753665398514537271457", 4620887706912954378L, "8.345482275366539" },
{ "827002066984754.515e+2", 4860049161224049404L, "8.270020669847546e+16" },
{ "286732114859191630413170.0e+171", 7516345396997012067L, "2.867321148591916e+194" },
{ "2293573137504623974985115941.39e+145", 7185419934915850854L, "2.293573137504624e+172" },
{ "5956547941627243923118.11435316e+71", 5994931679605799054L, "5.956547941627244e+92" },
{ "-109853.23e-208", -7652867289783984088L, "-1.0985323e-203" },
{ "78208072976786455144e-230", 1463798063640768485L, "7.820807297678646e-211" },
{ "984388456717607885.0", 4876081732005612488L, "9.84388456717608e+17" },
{ "9.065242645513447959e+11", 4785745451415300873L, "906524264551.3448" },
{ "616.4e-299", 175675555446034971L, "6.164e-297" },
{ "193709049088815348057109453365.0e+146", 7229285078893848047L, "1.9370904908881534e+175" },
{ "448672292245580362309655e+184", 7713526195429197804L, "4.486722922455804e+207" },
{ "8683378656433886565108326.3e+283", 9214059114725894834L, "8.683378656433887e+307" },
{ "-28.4181e-85", -5866478163108931173L, "-2.84181e-84" },
{ "356576.0e+228", 8101049532934230075L, "3.56576e+233" },
{ "26496678585940525652.0", 4897378110994754292L, "2.6496678585940525e+19" },
{ "4075.847", 4661181297412794221L, "4075.847" },
{ "1.2178213189", 4608163398810648798L, "1.2178213189" },
{ "79214968058166.07531656", 4814914159987412357L, "79214968058166.08" },
{ "954050.1500", 4696442440478674125L, "954050.15" },
{ "-8976010571e-110", -6113318437113755177L, "-8.976010571e-101" },
{ "39.55178653618156437606496315", 4630763236406567664L, "39.55178653618157" },
{ "97095057824.8922882", 4771171627467596909L, "97095057824.89229" },
{ "72999.150585231338818086490629e-270", 640307887851073669L, "7.299915058523134e-266" },
{ "9061496597394310321297800.941e-126", 3095182002697426466L, "9.061496597394311e-102" },
{ "24619.3350e-192", 1800151515651920268L, "2.4619335e-188" },
{ "6314486095813.13510103625016", 4798296037284320394L, "6314486095813.135" },
{ "86101857.47976950557063e+51", 5488602628152256209L, "8.61018574797695e+58" },
{ "-9228406647553151.7491e+278", -218672533017604270L, "-9.228406647553152e+293" },
{ "-109116263664436163761961952.5", -4227032247252742235L, "-1.0911626366443617e+26" },
{ "-108.0e+25", -4212014816622836652L, "-1.08e+27" },
{ "175905.0e-252", 915266163476354630L, "1.75905e-247" },
{ "-892308217983694516192177909.0e+249", -488141606772896842L, "-8.923082179836945e+275" },
{ "566622208781851001e-178", 2209435534181310078L, "5.66622208781851e-161" },
{ "452800311895686290894792629868.0e-197", 2103487415330792755L, "4.528003118956863e-168" },
{ "1.063e-109", 2976866746370904503L, "1.063e-109" },
{ "-2673455e+36", -3981550870848177602L, "-2.673455e+42" },
{ "-66.605e-251", -8344304590302386922L, "-6.6605e-250" },
{ "925937158701.0e-35", 4262203249624775218L, "9.25937158701e-24" },
{ "29422736699.34649e+228", 8174617011458726069L, "2.942273669934649e+238" },
{ "101037977942856208.1301755983e-137", 2811617633051290470L, "1.0103797794285621e-120" },
{ "6.0", 4618441417868443648L, "6.0" },
{ "9.0e-220", 1329781493142383157L, "9e-220" },
{ "67634678919493557584989669483e+211", 8194853033308816974L, "6.763467891949355e+239" },
{ "702982603311441710284686632.0e-134", 3004044460287497697L, "7.029826033114417e-108" },
{ "815e-255", 835389832106102001L, "8.15e-253" },
{ "9766676350389724.630400508e+169", 7374358766207213511L, "9.766676350389724e+184" },
{ "-33205335789386189589964448.642e-262", -8154436352499320006L, "-3.320533578938619e-237" },
{ "824.7232", 4650465557803754429L, "824.7232" },
{ "2e-301", 108408437193569761L, "2e-301" },
{ "-1.178893106510238019359e+252", -845387005382895447L, "-1.178893106510238e+252" },
{ "-43655914131839848.0e+121", -2557265014845614752L, "-4.365591413183985e+137" },
{ "0.9938056755411020e-35", 4083194199714898251L, "9.93805675541102e-36" },
{ "710500.48325268947893055e-23", 4350585052196504161L, "7.105004832526895e-18" },
{ "686240811847e+209", 7910657241990298994L, "6.86240811847e+220" },
{ "679622257854033598.0e-205", 1806766875166682218L, "6.796222578540336e-188" },
{ "-7e-289", -8927461087006752900L, "-7e-289" },
{ "30702319630037594023513e-245", 1278012107044104135L, "3.0702319630037592e-223" },
{ "1461537561921258853812.672209", 4923506116429650887L, "1.4615375619212588e+21" },
{ "62815381205888002175.0", 4903080688165338527L, "6.2815381205888e+19" },
{ "-9544.176484812597718553915516e-224", -7907969144704734809L, "-9.544176484812598e-221" },
{ "5.7076014885587062543823e-53", 3825207549279336301L, "5.707601488558706e-53" },
{ "2209125363642.0e-265", 826949312482712499L, "2.209125363642e-253" },
{ "1254014211346568.149967618053", 4832874857386557985L, "1254014211346568.2" },
{ "860887042490279377421.608405238e+174", 7523146718194340208L, "8.608870424902794e+194" },
{ "-40.7360686023e+228", -1181387509002387742L, "-4.07360686023e+229" },
{ "-29940542806192093569.792496", -4325153138696874160L, "-2.9940542806192095e+19" },
{ "7398798278076082.4334347135e+111", 6504847345485541630L, "7.398798278076083e+126" },
{ "-4.5997e+89", -3274988697286904235L, "-4.5997e+89" },
{ "81385107.001069753242e+198", 7687707508636978020L, "8.138510700106975e+205" },
{ "1.448885406", 4609204018947211062L, "1.448885406" },
{ "3.1e-326", 0L, "0.0" },
{ "5602.818565224318691e-328", 0L, "0.0" },
{ "1052.7859812208216130e+239", 8227957510357109448L, "1.0527859812208216e+242" },
{ "6406683254176726224698.0", 4933047881616180983L, "6.406683254176727e+21" },
{ "6164444212.5268252e+4", 4813231290996353568L, "61644442125268.25" },
{ "3098522319958316587745298e-108", 3357455367953734192L, "3.098522319958317e-84" },
{ "-8.335", -4602490230938250772L, "-8.335" },
{ "9795650524228224.5", 4846267424685397312L, "9795650524228224.0" },
{ "8.803e+208", 7733000584992601379L, "8.803e+208" },
{ "-40.4754e+2", -4562252987193276498L, "-4047.54" },
{ "3244.25740767e-74", 3552400129709224697L, "3.24425740767e-71" },
{ "4.888896784054e-27", 4213176179556737267L, "4.888896784054e-27" },
{ "287121071301879032571.0e-269", 888491831123499208L, "2.8712107130187903e-249" },
{ "2.667e-247", 918125189786468461L, "2.667e-247" },
{ "4.1e+258", 8476074615440912419L, "4.1e+258" },
{ "201468628786749.0e+33", 5314710545822263247L, "2.01468628786749e+47" },
{ "-46624500e+109", -2871138759936641890L, "-4.66245e+116" },
{ "-67005471.564448369e-128", -6414340721194690622L, "-6.7005471564448365e-121" },
{ "34028800569731647400907.5", 4944058289878429476L, "3.402880056973165e+22" },
{ "16951922792118695.0e-284", 600777099768678075L, "1.6951922792118696e-268" },
{ "84401390207367115115034126102.5", 5039822046976206397L, "8.440139020736712e+28" },
{ "29.6086480596433068665402", 4629027311205151006L, "29.608648059643308" },
{ "106921231602280537550264.0e+285", 9215218441707426373L, "1.0692123160228053e+308" },
{ "129230277299152998284216e+217", 8199030327693238427L, "1.29230277299153e+240" },
{ "1553257291099189753e+172", 7452300297099932243L, "1.5532572910991898e+190" },
{ "4494291574322159225375204.0e-71", 3913746587349763910L, "4.494291574322159e-47" },
{ "40031996724480682520e-129", 2970143182746377635L, "4.0031996724480683e-110" },
{ "8188e-216", 1433848121983751043L, "8.188e-213" },
{ "534.539e-204", 1595667330951917634L, "5.34539e-202" },
{ "35971902786626390752494945.0", 4989356549151033225L, "3.597190278662639e+25" },
{ "0e-281", 0L, "0.0" },
{ "-682368482150667510117784768e+114", -2509447815885517798L, "-6.823684821506675e+140" },
{ "640419741.860009635e-141", 2629283826755081122L, "6.404197418600096e-133" },
{ "3867.83640e+220", 7951879328509240763L, "3.8678364e+223" },
{ "7484084994110360e+114", 6549808393743915945L, "7.48408499411036e+129" },
{ "226947377103.54566963", 4776748617038284249L, "226947377103.5457" },
{ "482577188729681.0e+89", 6157981773288755775L, "4.82577188729681e+103" },
{ "103e+251", 8392029547058071552L, "1.03e+253" },
{ "4365.5", 4661521932712148992L, "4365.5" },
{ "-4512.6364295269155707e-44", -5219827205513723182L, "-4.5126364295269156e-41" },
{ "448266066116924326410170055e+207", 8102551607070509961L, "4.4826606611692435e+233" },
{ "5.19722e+244", 8268171589802821021L, "5.19722e+244" },
{ "-39131082994071626.0", -4368103852802492663L, "-3.9131082994071624e+16" },
{ "407035748452268.2813538408e+300", 9218868437227405312L, "inf" },
{ "527993861.0", 4737637875791691776L, "527993861.0" },
{ "-60907153604348718e-96", -5801512463652081565L, "-6.090715360434872e-80" },
{ "-7900758140089280526542175413.0e+4", -4139043653209105069L, "-7.90075814008928e+31" },
{ "7625540528025970792853.1e-329", 12220732555163875L, "7.625540528025971e-308" },
{ "4476804528917239647583.0e-89", 3599308095700970046L, "4.47680452891724e-68" },
{ "116.2675e+17", 4892082719720425660L, "1.162675e+19" },
{ "787.90e-92", 3274130948369826539L, "7.879e-90" },
{ "41394131928838786182935e-104", 3389276706488298453L, "4.1394131928838786e-82" },
{ "-653.36283e-221", -7880609436539950119L, "-6.5336283e-219" },
{ "955794988208664771687346.0e+191", 7823243632544452582L, "9.557949882086648e+214" },
{ "17754507637222071162381875e+247", 8679845126018147466L, "1.775450763722207e+272" },
{ "7.68e+195", 7537421850789065372L, "7.68e+195" },
{ "8282734863971946419860335.0e-263", 1045189542936631201L, "8.282734863971947e-239" },
{ "3.74625606e+246", 8295952952511376097L, "3.74625606e+246" },
{ "50285.95e-158", 2313434100645552143L, "5.028595e-154" },
{ "5885669970437227285774.92e-60", 4035231547988945579L, "5.885669970437227e-39" },
{ "375369467390947253.0e-185", 2102148746146269705L, "3.7536946739094724e-168" },
{ "6122.4562", 4663453726483542127L, "6122.4562" },
{ "720.1809e+69", 5681878342431414333L, "7.201809e+71" },
{ "560840.2578625272264e+14", 4902258989505715080L, "5.608402578625272e+19" },
{ "2.1e+131", 6571577934564546031L, "2.1e+131" },
{ "50984094523449e+204", 7863984994275004437L, "5.0984094523449e+217" },
{ "7104325.92287798999108e+113", 6400067620129659834L, "7.10432592287799e+119" },
{ "68754142819712478990052954.0e+157", 7342193561072622828L, "6.875414281971248e+182" },
{ "6371135377620827668511.0e-285", 669257222946182605L, "6.371135377620828e-264" },
{ "86.6719915e-139", 2556300397935060845L, "8.66719915e-138" },
{ "-924847124631e-136", -6471869011214791394L, "-9.24847124631e-125" },
{ "783763203245432683477359324e-241", 1403720925441841835L, "7.837632032454327e-215" },
{ "33013808597453.66732629574952e+41", 5422681294914886070L, "3.301380859745367e+54" },
{ "5232506686.5", 4752280688179806208L, "5232506686.5" },
{ "7530696e-30", 4261026773984002807L, "7.530696e-24" },
{ "2601136e+99", 6184046376648300779L, "2.601136e+105" },
{ "718784768300756617568.14448e-204", 1866864073262966569L, "7.187847683007566e-184" },
{ "148522773723924508475718e+212", 8125249407171543644L, "1.485227737239245e+235" },
{ "102584274921242367.2282441693e-160", 2467976006030512767L, "1.0258427492124238e-143" },
{ "42861464006636e+76", 5947828761292558790L, "4.2861464006636e+89" },
{ "2014800757348487.0e+267", 8830362297817994366L, "2.014800757348487e+282" },
{ "830083.5", 4695377575063584768L, "830083.5" },
{ "-10710250632410346.7352087243259", -4376647312115287435L, "-1.0710250632410346e+16" },
{ "-86786996481861727835861e-303", -8806449666860697700L, "-8.678699648186173e-281" },
{ "72298747.0", 4724624296264073216L, "72298747.0" },
{ "3.0e+308", 9218868437227405312L, "inf" },
{ "106347261478462469789542676.0e-300", 508190658297005694L, "1.0634726147846248e-274" },
{ "548207.364", 4692956277492299071L, "548207.364" },
{ "336344063227909494072513889.0e-304", 455730275051153250L, "3.363440632279095e-278" },
{ "76120650225431013839366081273.8e+304", 9218868437227405312L, "inf" },
{ "3.0e+132", 6589021296160631994L, "3e+132" },
{ "2682540.0e-322", 54295214L, "2.68254e-316" },
{ "2563458070685408450447.0e+186", 7710145140987739075L, "2.5634580706854084e+207" },
{ "-31587694393529104224.738610482e+4", -4264705513488230457L, "-3.1587694393529107e+23" },
{ "697532084472580e+0", 4828935457589581856L, "697532084472580.0" },
{ "6382609957350453420168813860e+41", 5636163289936670526L, "6.382609957350454e+68" },
{ "698361.34572929904460289472328", 4694246090374082113L, "698361.345729299" },
{ "73496914.7102e-149", 2495536008417178287L, "7.34969147102e-142" },
{ "-47606.0e-288", -8855113716859869028L, "-4.7606e-284" },
{ "6075.62917237797270389723e-143", 2524029061219851261L, "6.075629172377973e-140" },
{ "-25930253551497.0e-112", -6091488245390213054L, "-2.5930253551497e-99" },
{ "196749581454.0e-135", 2756385367201602600L, "1.96749581454e-124" },
{ "50505023755247012e-36", 4318336429831471741L, "5.050502375524701e-20" },
{ "83477775675082701478947576630e+259", 8914292473251887725L, "8.34777756750827e+287" },
{ "7.3e-13", 4425260804279883197L, "7.3e-13" },
{ "-620.5", -4574702845315514368L, "-620.5" },
};
[Theory]
[MemberData(nameof(Floats))]
public void FloatLiteralsAreBitIdenticalToPythonAndReprTheSame(string literal, long bits, string repr)
{
var value = Assert.IsType<double>(PythonJsonReader.Parse(literal));
Assert.Equal(bits, BitConverter.DoubleToInt64Bits(value));
Assert.Equal(repr, PythonFloat.Repr(value));
}
[Theory]
[InlineData("-0", 0.5, "temperature[0]=0 -> 0.5")]
[InlineData("0", 0.5, "temperature[0]=0 -> 0.5")]
[InlineData("7", 5.0, "temperature[0]=7 -> 5")]
[InlineData("7.0", 5.0, "temperature[0]=7.0 -> 5")]
[InlineData("-0.0", 0.5, "temperature[0]=-0.0 -> 0.5")]
[InlineData("1", 1.0, "")]
[InlineData("1.0", 1.0, "")]
[InlineData("2.5", 2.5, "")]
[InlineData("100000000000000000000000000000", 5.0, "temperature[0]=100000000000000000000000000000 -> 5")]
[InlineData("-100000000000000000000000000000", 0.5, "temperature[0]=-100000000000000000000000000000 -> 0.5")]
[InlineData("100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", 5.0, "temperature[0]=100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 -> 5")]
[InlineData("1e400", 1.0, "temperature[0]=inf -> 1")]
[InlineData("-1e400", 1.0, "temperature[0]=-inf -> 1")]
[InlineData("0.1006", 0.5, "temperature[0]=0.1006 -> 0.5")]
[InlineData("1E2", 5.0, "temperature[0]=100.0 -> 5")]
[InlineData("5e0", 5.0, "")]
[InlineData("0.5", 0.5, "")]
[InlineData("10", 5.0, "temperature[0]=10 -> 5")]
[InlineData("100000000000000000000.0", 5.0, "temperature[0]=1e+20 -> 5")]
[InlineData("1e22", 5.0, "temperature[0]=1e+22 -> 5")]
[InlineData("1e-7", 0.5, "temperature[0]=1e-07 -> 0.5")]
[InlineData("true", 1.0, "temperature[0]=True -> 1")]
[InlineData("null", 1.0, "temperature[0]=None -> 1")]
[InlineData("\"2.5\"", 2.5, "")]
[InlineData("\"x\"", 1.0, "temperature[0]='x' -> 1")]
[InlineData("[1]", 1.0, "temperature[0]=[1] -> 1")]
[InlineData("NaN", 1.0, "temperature[0]=nan -> 1")]
[InlineData("Infinity", 1.0, "temperature[0]=inf -> 1")]
[InlineData("-Infinity", 1.0, "temperature[0]=-inf -> 1")]
public void WarningEntryUsesTheLiteralsPythonType(string literal, double applied, string entry)
{
// Distinct text per row is not needed: the entry list is on the config, the trace is deduplicated separately.
var config = LayaConfig.Parse("{\"temperature\": [" + literal + ", 1, 1]}");
Assert.Equal(applied, config.Temperature[0]);
Assert.Equal<IEnumerable<string>>(entry.Length == 0 ? [] : new[] { entry }, config.ClampedTemperatures);
}
[Fact]
public void ARealStringIsNeverReadAsNaNOrInfinity()
{
var config = LayaConfig.Parse("{\"temperature\": [\"NaN\", \"Infinity\", \"\\uE000laya-nan\"], \"encoder\": \"NaN\"}");
Assert.Equal("NaN", config.Encoder);
Assert.Equal(["temperature[0]='NaN' -> 1", "temperature[1]='Infinity' -> 1", "temperature[2]='\\ue000laya-nan' -> 1"], config.ClampedTemperatures);
}
[Fact]
public void ConfigRejectsMalformedJsonAsPythonDoes()
{
foreach (var bad in new[] { "{NaN: 1}", "{\"temperature\": [nan]}", "{\"temperature\": [1,]}", "{\"temperature\": [01]}", "{'a': 1}" })
Assert.ThrowsAny<JsonException>(() => LayaConfig.Parse(bad));
Assert.Throws<InvalidDataException>(() => LayaConfig.Parse("[1]"));
}
[Fact]
public void IntegerLiteralsKeepPythonsIntTypeAndFloatLiteralsStayFloats()
{
Assert.IsType<long>(PythonJsonReader.Parse("7"));
Assert.IsType<long>(PythonJsonReader.Parse("-0"));
Assert.IsType<BigInteger>(PythonJsonReader.Parse("9223372036854775808"));
Assert.IsType<double>(PythonJsonReader.Parse("7.0"));
Assert.IsType<double>(PythonJsonReader.Parse("7e0"));
}
}
/// <summary>
/// Classes that observe or reset the process-wide warning state (<see cref="WarnOnce"/>, <c>Trace.Listeners</c>) share
/// this collection, and it never runs in parallel: xunit runs it alone, so no other class warns or resets meanwhile.
/// </summary>
[CollectionDefinition(Name, DisableParallelization = true)]
public sealed class WarningStateCollection
{
/// <summary>The collection name.</summary>
public const string Name = "warning-state";
}
/// <summary>Python's default warnings filter: one trace per distinct (site, text) per process.</summary>
[Collection(WarningStateCollection.Name)]
public sealed class WarnOnceTests
{
private sealed class Capture : TraceListener
{
private readonly object _gate = new();
private readonly List<string> _messages = [];
private readonly int? _thread;
public Capture(int? onlyThread) => _thread = onlyThread;
public List<string> Messages { get { lock (_gate) return [.. _messages]; } }
public override void Write(string? message) { lock (_gate) _messages.Add(message ?? ""); }
public override void WriteLine(string? message) { lock (_gate) _messages.Add(message ?? ""); }
public override void TraceEvent(TraceEventCache? eventCache, string source, TraceEventType eventType, int id, string? format, params object?[]? args)
{
// Other test classes warn from their own threads and call sites; only the ones raised here count.
if (_thread is { } thread && Environment.CurrentManagedThreadId != thread) return;
lock (_gate) _messages.Add(args is null ? format ?? "" : string.Format(format ?? "", args));
}
}
private static List<string> Capturing(Action action, string needle = "laya", bool anyThread = false)
{
var capture = new Capture(anyThread ? null : Environment.CurrentManagedThreadId);
Trace.Listeners.Add(capture);
try { action(); }
finally { Trace.Listeners.Remove(capture); }
return capture.Messages.Where(m => m.StartsWith("laya", StringComparison.Ordinal) && m.Contains(needle)).ToList();
}
private const string Config = "{\"temperature\": [0.2, 1, 1]}";
// ── call sites (Python keys the default filter on the frame stacklevel selects) ──
private static string TempConfig(string value) => "{\"temperature\": [" + value + ", 1, 1]}";
[Fact]
public void TheSameCallSiteInALoopWarnsOnce()
{
WarnOnce.Reset();
var messages = Capturing(() => { for (var i = 0; i < 5; i++) LayaConfig.Parse(Config); }, "temperature[0]=0.2 ");
Assert.Single(messages);
Assert.StartsWith("laya ONNX: this checkpoint ships temperatures outside [0.5, 5]", messages[0]);
}
[Fact]
public void TwoCallSitesWarnTwiceForTheSameText()
{
WarnOnce.Reset();
var messages = Capturing(() =>
{
LayaConfig.Parse(Config);
LayaConfig.Parse(Config);
}, "temperature[0]=0.2 ");
Assert.Equal(2, messages.Count);
}
[Fact]
public void ANewTextFromTheSameSiteWarnsAgain()
{
WarnOnce.Reset();
var messages = Capturing(() =>
{
foreach (var value in new[] { "0.3", "0.3", "0.4" }) LayaConfig.Parse(TempConfig(value));
}, "temperature[0]=0.");
Assert.Equal(2, messages.Count(m => m.Contains("temperature[0]=0.3 ")) + messages.Count(m => m.Contains("temperature[0]=0.4 ")));
Assert.Single(messages, m => m.Contains("temperature[0]=0.3 "));
}
// A public entry is attributed to its caller, and overloads that call each other are one entry.
[WarnSite, System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
private static void EntryOverload(string text) => EntryCore(text);
[WarnSite, System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
private static void EntryCore(string text) => WarnOnce.Warn(text);
[Fact]
public void AnEntryIsKeyedByItsCallerNotByTheLibraryLine()
{
WarnOnce.Reset();
var once = Capturing(() => { for (var i = 0; i < 4; i++) EntryOverload("laya: site test loop"); }, "site test loop");
Assert.Single(once);
var twice = Capturing(() =>
{
EntryOverload("laya: site test lines");
EntryOverload("laya: site test lines");
}, "site test lines");
Assert.Equal(2, twice.Count);
}
// Through the router the site is the router's own call into the checkpoint, as Python attributes it to router.py.
private sealed class WarningScanner : ILayaPredictor, ILayaLongPredictor
{
public LayaResult Predict(object? state, QuestionSet questions) => throw new NotSupportedException();
[WarnSite, System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
public LayaResult PredictLong(object state, QuestionSet questions, int? window = null, int? stride = null,
string aggregate = "auto", int? batchSize = null)
{
WarnOnce.Warn("laya: predict_long: via the router");
return new LayaResult("scanner", [], new Dictionary<string, Answer>(StringComparer.Ordinal), new Usage(0, 0));
}
}
[Fact]
public void CallsThroughTheRouterFromDifferentUserLinesWarnOnce()
{
WarnOnce.Reset();
using var router = new LayaRouter(new LayaRouterOptions(), _ => new WarningScanner());
var messages = Capturing(() =>
{
router.PredictLong("hello", new QuestionSet());
router.PredictLong("hello again", new QuestionSet());
}, "via the router");
Assert.Single(messages);
}
// One un-optimized call site shared by every thread: a jitted-and-re-jitted lambda may report a different IL offset.
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining | System.Runtime.CompilerServices.MethodImplOptions.NoOptimization)]
private static void LoadFromOneSite() => LayaConfig.Parse(TempConfig("0.25"));
// xunit runs other classes (ConfigFieldTypeTests) in parallel and they warn about temperature[0]=0.2 from their
// own threads while this listener is attached, so this test counts only a value no other test uses, and a
// foreign warning from another thread is raised on purpose to prove it is not counted.
[Fact]
public void ConcurrentLoadsWarnOnce()
{
WarnOnce.Reset();
var messages = Capturing(() =>
{
var foreign = Task.Run(() => LayaConfig.Parse("{\"temperature\": [0.2, 1, 1], \"temperature_by_options\": {\"x\": 9}}"));
Parallel.For(0, 32, _ => LoadFromOneSite());
foreign.Wait();
}, "temperature[0]=0.25 ", anyThread: true);
Assert.Single(messages);
}
public static TheoryData<int, int, int, int?, int?, string, string[]> Scenarios() => new()
{
{ 100, 512, 192, 1000, null, "100,50,100", new string[] { "laya: predict_long: window=1000 is wider than the 100 state tokens these questions leave inside max_len=512, so every window would be truncated to 100 on the way to the model; scanning with window=100 instead" } },
{ 100, 512, 192, null, null, "100,50,100", new string[] { "laya: predict_long: these questions leave only 100 of max_len=512 for the state (their heads take the rest), so the scan window is capped 312 -> 100 and roughly 3.1x as many windows -- each a full forward pass -- are needed to read the document. Fewer or shorter options, a larger max_len, or laya.shortlist.predict_shortlist for a large label set will all cost less than scanning at this width" } },
{ 24, 174, 100, null, null, "24,12,24", new string[] { "laya: predict_long: these questions leave only 24 of max_len=174 for the state (their heads take the rest), so the scan window is capped 66 -> 24 and roughly 2.8x as many windows -- each a full forward pass -- are needed to read the document. Fewer or shorter options, a larger max_len, or laya.shortlist.predict_shortlist for a large label set will all cost less than scanning at this width" } },
{ 32, 180, 100, null, null, "32,16,32", new string[] { "laya: predict_long: these questions leave only 32 of max_len=180 for the state (their heads take the rest), so the scan window is capped 72 -> 32 and roughly 2.2x as many windows -- each a full forward pass -- are needed to read the document. Fewer or shorter options, a larger max_len, or laya.shortlist.predict_shortlist for a large label set will all cost less than scanning at this width" } },
{ 100, 512, 192, 1000, 500, "100,50,100", new string[] { "laya: predict_long: window=1000 is wider than the 100 state tokens these questions leave inside max_len=512, so every window would be truncated to 100 on the way to the model; scanning with window=100 instead", "laya: predict_long: stride=500 was a 50% step for the window=1000 you asked for, but the window was reduced to 100 to fit the room these questions leave; scanning with stride=50 instead" } },
{ 200, 512, 192, null, null, "200,100,200", new string[] { } },
{ 100, 512, 192, 500, 400, "100,50,100", new string[] { "laya: predict_long: window=500 is wider than the 100 state tokens these questions leave inside max_len=512, so every window would be truncated to 100 on the way to the model; scanning with window=100 instead", "laya: predict_long: stride=400 was a 50% step for the window=500 you asked for, but the window was reduced to 100 to fit the room these questions leave; scanning with stride=50 instead" } },
{ 150, 512, 192, null, null, "150,75,150", new string[] { "laya: predict_long: these questions leave only 150 of max_len=512 for the state (their heads take the rest), so the scan window is capped 312 -> 150 and roughly 2.1x as many windows -- each a full forward pass -- are needed to read the document. Fewer or shorter options, a larger max_len, or laya.shortlist.predict_shortlist for a large label set will all cost less than scanning at this width" } },
{ 104, 512, 192, null, null, "104,52,104", new string[] { "laya: predict_long: these questions leave only 104 of max_len=512 for the state (their heads take the rest), so the scan window is capped 312 -> 104 and roughly 3.0x as many windows -- each a full forward pass -- are needed to read the document. Fewer or shorter options, a larger max_len, or laya.shortlist.predict_shortlist for a large label set will all cost less than scanning at this width" } },
{ 399, 512, 192, 400, 399, "399,399,399", new string[] { "laya: predict_long: window=400 is wider than the 399 state tokens these questions leave inside max_len=512, so every window would be truncated to 399 on the way to the model; scanning with window=399 instead" } },
{ 100, 512, 192, 1000, 2000, "ERR", new string[] { "laya: predict_long: window=1000 is wider than the 100 state tokens these questions leave inside max_len=512, so every window would be truncated to 100 on the way to the model; scanning with window=100 instead" } },
};
[Theory]
[MemberData(nameof(Scenarios))]
public void PredictLongWarningsHavePythonsTextAndAreShownOnce(int room, int maxLen, int head, int? window, int? stride, string result, string[] expected)
{
var requested = window is > 0 ? window.Value : Math.Max(64, maxLen - head - 8);
WarnOnce.Reset();
(int, int, int) got = default;
var threw = false;
void Run() { try { var b = LongPrediction.BudgetForRoom(requested, room, maxLen, head, window, stride); got = (b.Window, b.Stride, b.Room); } catch (ArgumentException) { threw = true; } }
var first = Capturing(Run);
Assert.Equal(result == "ERR", threw);
if (!threw) Assert.Equal(result, $"{got.Item1},{got.Item2},{got.Item3}");
Assert.Equal(expected, first.Where(m => m.StartsWith("laya: predict_long")).ToArray());
var second = Capturing(Run);
Assert.DoesNotContain(second, m => m.StartsWith("laya: predict_long"));
}
}
/// <summary>
/// Config files are read like Python's <c>json.load(open(path))</c>: strict UTF-8, a BOM is an error
/// (<c>json.loads</c>: "Unexpected UTF-8 BOM (decode using utf-8-sig)"), invalid bytes are an error.
/// Checked against CPython 3.12 reading the same bytes with <c>encoding="utf-8"</c>.
/// </summary>
public sealed class ConfigFileEncodingTests
{
private static string Write(byte[] bytes)
{
var path = Path.Combine(Path.GetTempPath(), "laya-cfg-" + Guid.NewGuid().ToString("N") + ".json");
File.WriteAllBytes(path, bytes);
return path;
}
private static readonly byte[] Bom = [0xEF, 0xBB, 0xBF];
[Fact]
public void AValidUtf8FileLoads()
{
var path = Write("{\"encoder\": \"café\", \"max_len\": 64}"u8.ToArray());
try { Assert.Equal("café", LayaConfig.Load(path).Encoder); }
finally { File.Delete(path); }
}
[Fact]
public void ABomIsRejectedLikePython()
{
var path = Write([.. Bom, .. "{\"max_len\": 64}"u8.ToArray()]);
try { Assert.Contains("Unexpected UTF-8 BOM", Assert.Throws<JsonException>(() => LayaConfig.Load(path)).Message); }
finally { File.Delete(path); }
}
[Fact]
public void InvalidUtf8IsRejectedLikePython()
{
var path = Write([.. "{\"encoder\": \"caf"u8.ToArray(), 0xE9, .. "\"}"u8.ToArray()]);
try { Assert.Throws<JsonException>(() => LayaConfig.Load(path)); }
finally { File.Delete(path); }
}
}