61 lines
2.3 KiB
C#
61 lines
2.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Text.RegularExpressions;
|
|
using ParadoxSaveParser.Lib;
|
|
|
|
namespace ParadoxSaveParser.Benchmarks;
|
|
|
|
/// <summary>
|
|
/// Checks whether the regex approach associates the right country tag with the right
|
|
/// technology block. It cannot: 933 of the 2803 country blocks in the test save have no
|
|
/// technology block at all, so a lazy "tag ... technology" pattern silently pairs a tag
|
|
/// with a technology block belonging to a later country.
|
|
/// </summary>
|
|
public static class Mispairing
|
|
{
|
|
public static Dictionary<string, long> FromParser(byte[] pdx)
|
|
{
|
|
var result = new Dictionary<string, long>();
|
|
using var stream = new MemoryStream(pdx, false);
|
|
var root = new SaveParserEU4(stream, SearchExpressionCompiler.Compile(BenchData.PdxQuery)).Parse();
|
|
foreach (var (tag, value) in (Dictionary<string, object>)root["countries"])
|
|
{
|
|
if (value is Dictionary<string, object> c
|
|
&& c.TryGetValue("technology", out var t)
|
|
&& t is Dictionary<string, object> tech
|
|
&& tech.TryGetValue("adm_tech", out var adm))
|
|
result[tag] = (long)adm;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public static Dictionary<string, long> FromRegex(string text, Regex pathAware)
|
|
{
|
|
var result = new Dictionary<string, long>();
|
|
foreach (Match m in pathAware.Matches(text))
|
|
result[m.Groups[1].Value] = long.Parse(m.Groups[2].ValueSpan);
|
|
return result;
|
|
}
|
|
|
|
public static void Report(byte[] pdx, string text)
|
|
{
|
|
var expected = FromParser(pdx);
|
|
var actual = FromRegex(text, new Regex(Extractors.PathAwarePattern, RegexOptions.Compiled));
|
|
int missing = 0, wrong = 0, extra = 0;
|
|
foreach (var (tag, adm) in expected)
|
|
{
|
|
if (!actual.TryGetValue(tag, out long got)) { missing++; Console.WriteLine($" missing tag: {tag}"); }
|
|
else if (got != adm) wrong++;
|
|
}
|
|
|
|
foreach (var tag in actual.Keys)
|
|
if (!expected.ContainsKey(tag))
|
|
extra++;
|
|
|
|
Console.WriteLine($"path aware regex pairing: {expected.Count} countries have technology, " +
|
|
$"regex reported {actual.Count}; missing={missing} wrong_value={wrong} invented={extra}");
|
|
}
|
|
}
|