static unsafe void GetBoth <T>(LowWriter <T> lw, T s, out IEnumerable <byte> newtonsoft, out IEnumerable <byte> unsafeJson, int?minBuffer = null) { var ms = new MemoryStream(); int len; using (var writer = new StreamWriter(ms, _utf8)) { _serializer.Serialize(writer, s, typeof(string)); writer.Flush(); len = (int)ms.Position; } newtonsoft = ms.GetBuffer().Take(len); if (minBuffer.HasValue && minBuffer.Value > len) { len = minBuffer.Value; } var mine = new byte[len]; fixed(byte *bs = mine) { var result = lw.Invoke(s, bs, len); unsafeJson = result < 0 ? null : mine.Take(result); } }
static unsafe void Approve <T>(IEnumerable <T> values, LowWriter <T> writer, Action <StringBuilder, T> heading, int?minBuffer = null) { int failed = 0; var sb = new StringBuilder(); foreach (var v in values) { sb.AppendLine(); sb.AppendLine(); heading(sb, v); IEnumerable <byte> newtonsoft; IEnumerable <byte> mine; GetBoth(writer, v, out newtonsoft, out mine, minBuffer); sb.AppendLine(); sb.AppendLine(); sb.AppendLine("## Newtonsoft"); Hex.Dump(sb, newtonsoft); sb.AppendLine(); sb.AppendLine(); sb.AppendLine("## Cameronism.Json"); Hex.Dump(sb, mine); sb.AppendLine(); sb.AppendLine(); bool equal = mine != null && Enumerable.SequenceEqual(newtonsoft, mine); sb.AppendFormat("### Equal: {0}", equal); if (mine != null) { int diff = IndexOfDiff(newtonsoft, mine); if (diff != -1) { sb.AppendFormat("### Difference at: {0:x}", diff); } } if (!equal) { failed++; } } ApprovalTests.Approvals.Verify(sb.ToString()); Assert.True(failed == 0, String.Format("Look at the approval, {0} comparisons failed", failed)); }