Skip to content

Commit

Permalink
Add a few more tests / improve code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
stephentoub committed Jan 9, 2020
1 parent 3f270ff commit 95965c9
Show file tree
Hide file tree
Showing 12 changed files with 441 additions and 174 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -222,62 +222,66 @@ public static int OpcodeSize(int opcode)
}

#if DEBUG
private static readonly Dictionary<int, string> s_codeStr = new Dictionary<int, string>()
{
{ Onerep, nameof(Onerep) },
{ Notonerep, nameof(Notonerep) },
{ Setrep, nameof(Setrep) },
{ Oneloop, nameof(Oneloop) },
{ Notoneloop, nameof(Notoneloop) },
{ Setloop, nameof(Setloop) },
{ Onelazy, nameof(Onelazy) },
{ Notonelazy, nameof(Notonelazy) },
{ Setlazy, nameof(Setlazy) },
{ One, nameof(One) },
{ Notone, nameof(Notone) },
{ Set, nameof(Set) },
{ Multi, nameof(Multi) },
{ Ref, nameof(Ref) },
{ Bol, nameof(Bol) },
{ Eol, nameof(Eol) },
{ Boundary, nameof(Boundary) },
{ Nonboundary, nameof(Nonboundary) },
{ Beginning, nameof(Beginning) },
{ Start, nameof(Start) },
{ EndZ, nameof(EndZ) },
{ End, nameof(End) },
{ Nothing, nameof(Nothing) },
{ Lazybranch, nameof(Lazybranch) },
{ Branchmark, nameof(Branchmark) },
{ Lazybranchmark, nameof(Lazybranchmark) },
{ Nullcount, nameof(Nullcount) },
{ Setcount, nameof(Setcount) },
{ Branchcount, nameof(Branchcount) },
{ Lazybranchcount, nameof(Lazybranchcount) },
{ Nullmark, nameof(Nullmark) },
{ Setmark, nameof(Setmark) },
{ Capturemark, nameof(Capturemark) },
{ Getmark, nameof(Getmark) },
{ Setjump, nameof(Setjump) },
{ Backjump, nameof(Backjump) },
{ Forejump, nameof(Forejump) },
{ Testref, nameof(Testref) },
{ Goto, nameof(Goto) },
{ Stop, nameof(Stop) },
{ ECMABoundary, nameof(ECMABoundary) },
{ NonECMABoundary, nameof(NonECMABoundary) },
{ Oneloopatomic, nameof(Oneloopatomic) },
{ Notoneloopatomic, nameof(Notoneloopatomic) },
{ Setloopatomic, nameof(Setloopatomic) },
};

[ExcludeFromCodeCoverage]
private static string OperatorDescription(int Opcode) =>
(s_codeStr.TryGetValue(Opcode & Mask, out string? codeStr) ? codeStr : "(unknown)") +
((Opcode & Ci) != 0 ? "-Ci" : "") +
((Opcode & Rtl) != 0 ? "-Rtl" : "") +
((Opcode & Back) != 0 ? "-Back" : "") +
((Opcode & Back2) != 0 ? "-Back2" : "");
private static string OperatorDescription(int Opcode)
{
string codeStr = (Opcode & Mask) switch
{
Onerep => nameof(Onerep),
Notonerep => nameof(Notonerep),
Setrep => nameof(Setrep),
Oneloop => nameof(Oneloop),
Notoneloop => nameof(Notoneloop),
Setloop => nameof(Setloop),
Onelazy => nameof(Onelazy),
Notonelazy => nameof(Notonelazy),
Setlazy => nameof(Setlazy),
One => nameof(One),
Notone => nameof(Notone),
Set => nameof(Set),
Multi => nameof(Multi),
Ref => nameof(Ref),
Bol => nameof(Bol),
Eol => nameof(Eol),
Boundary => nameof(Boundary),
Nonboundary => nameof(Nonboundary),
Beginning => nameof(Beginning),
Start => nameof(Start),
EndZ => nameof(EndZ),
End => nameof(End),
Nothing => nameof(Nothing),
Lazybranch => nameof(Lazybranch),
Branchmark => nameof(Branchmark),
Lazybranchmark => nameof(Lazybranchmark),
Nullcount => nameof(Nullcount),
Setcount => nameof(Setcount),
Branchcount => nameof(Branchcount),
Lazybranchcount => nameof(Lazybranchcount),
Nullmark => nameof(Nullmark),
Setmark => nameof(Setmark),
Capturemark => nameof(Capturemark),
Getmark => nameof(Getmark),
Setjump => nameof(Setjump),
Backjump => nameof(Backjump),
Forejump => nameof(Forejump),
Testref => nameof(Testref),
Goto => nameof(Goto),
Stop => nameof(Stop),
ECMABoundary => nameof(ECMABoundary),
NonECMABoundary => nameof(NonECMABoundary),
Oneloopatomic => nameof(Oneloopatomic),
Notoneloopatomic => nameof(Notoneloopatomic),
Setloopatomic => nameof(Setloopatomic),
_ => "(unknown)"
};

return
codeStr +
((Opcode & Ci) != 0 ? "-Ci" : "") +
((Opcode & Rtl) != 0 ? "-Rtl" : "") +
((Opcode & Back) != 0 ? "-Back" : "") +
((Opcode & Back2) != 0 ? "-Back2" : "");
}

[ExcludeFromCodeCoverage]
public string OpcodeDescription(int offset)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1661,7 +1661,7 @@ LocalBuilder RentInt32Local()
// Returns a rented Int32 local.
void ReturnInt32Local(LocalBuilder int32Local)
{
Debug.Assert(int32Local != null);
Debug.Assert(iterationLocals != null);
Debug.Assert(int32Local.LocalType == typeof(int));
iterationLocals.Push(int32Local);
}
Expand Down Expand Up @@ -2637,16 +2637,6 @@ private void InitializeCultureForGoIfNecessary()
}
}

#if DEBUG
/// <summary>Debug only: emit code to print out a message.</summary>
[ExcludeFromCodeCoverage]
private void Message(string str)
{
Ldstr(str);
Call(s_debugWriteLine!);
}
#endif

/// <summary>
/// The main translation function. It translates the logic for a single opcode at
/// the current position. The structure of this function exactly mirrors
Expand All @@ -2664,37 +2654,9 @@ private void GenerateOneCode()
{
#if DEBUG
if ((_options & RegexOptions.Debug) != 0)
{
Mvlocfld(_runtextposLocal!, s_runtextposField);
Mvlocfld(_runtrackposLocal!, s_runtrackposField);
Mvlocfld(_runstackposLocal!, s_runstackposField);
Ldthis();
Callvirt(s_dumpStateM);

var sb = new StringBuilder();
if (_backpos > 0)
{
sb.AppendFormat("{0:D6} ", _backpos);
}
else
{
sb.Append(" ");
}
sb.Append(_code!.OpcodeDescription(_codepos));

if ((_regexopcode & RegexCode.Back) != 0)
{
sb.Append(" Back");
}

if ((_regexopcode & RegexCode.Back2) != 0)
{
sb.Append(" Back2");
}

Message(sb.ToString());
}
DumpBacktracking();
#endif

LocalBuilder charInClassLocal;

// Before executing any RegEx code in the unrolled loop,
Expand Down Expand Up @@ -4399,5 +4361,42 @@ private void EmitTimeoutCheck()
Callvirt(s_checkTimeoutMethod);
MarkLabel(label);
}

#if DEBUG
/// <summary>Emit code to print out the current state of the runner.</summary>
[ExcludeFromCodeCoverage]
private void DumpBacktracking()
{
Mvlocfld(_runtextposLocal!, s_runtextposField);
Mvlocfld(_runtrackposLocal!, s_runtrackposField);
Mvlocfld(_runstackposLocal!, s_runstackposField);
Ldthis();
Callvirt(s_dumpStateM);

var sb = new StringBuilder();
if (_backpos > 0)
{
sb.AppendFormat("{0:D6} ", _backpos);
}
else
{
sb.Append(" ");
}
sb.Append(_code!.OpcodeDescription(_codepos));

if ((_regexopcode & RegexCode.Back) != 0)
{
sb.Append(" Back");
}

if ((_regexopcode & RegexCode.Back2) != 0)
{
sb.Append(" Back2");
}

Ldstr(sb.ToString());
Call(s_debugWriteLine!);
}
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -926,65 +926,58 @@ public int ChildCount()
}

#if DEBUG
private static readonly Dictionary<int, string> s_typeStr = new Dictionary<int, string>()
{
{ Oneloop, nameof(Oneloop) },
{ Notoneloop, nameof(Notoneloop) },
{ Setloop, nameof(Setloop) },
{ Onelazy, nameof(Onelazy) },
{ Notonelazy, nameof(Notonelazy) },
{ Setlazy, nameof(Setlazy) },
{ One, nameof(One) },
{ Notone, nameof(Notone) },
{ Set, nameof(Set) },
{ Multi, nameof(Multi) },
{ Ref, nameof(Ref) },
{ Bol, nameof(Bol) },
{ Eol, nameof(Eol) },
{ Boundary, nameof(Boundary) },
{ Nonboundary, nameof(Nonboundary) },
{ ECMABoundary, nameof(ECMABoundary) },
{ NonECMABoundary, nameof(NonECMABoundary) },
{ Beginning, nameof(Beginning) },
{ Start, nameof(Start) },
{ EndZ, nameof(EndZ) },
{ End, nameof(End) },
{ Oneloopatomic, nameof(Oneloopatomic) },
{ Notoneloopatomic, nameof(Notoneloopatomic) },
{ Setloopatomic, nameof(Setloopatomic) },
{ Nothing, nameof(Nothing) },
{ Empty, nameof(Empty) },
{ Lazyloop, nameof(Lazyloop) },
{ Capture, nameof(Capture) },
{ Oneloop, nameof(Group) },
{ Oneloop, nameof(Require) },
{ Oneloop, nameof(Prevent) },
{ Oneloop, nameof(Atomic) },
{ Oneloop, nameof(Testref) },
{ Oneloop, nameof(Testgroup) },
};

[ExcludeFromCodeCoverage]
public string Description()
{
StringBuilder argSb = new StringBuilder();

argSb.Append(s_typeStr.TryGetValue(Type, out string? typeStr) ? typeStr : "(unknown)");

if ((Options & RegexOptions.ExplicitCapture) != 0)
argSb.Append("-C");
if ((Options & RegexOptions.IgnoreCase) != 0)
argSb.Append("-I");
if ((Options & RegexOptions.RightToLeft) != 0)
argSb.Append("-L");
if ((Options & RegexOptions.Multiline) != 0)
argSb.Append("-M");
if ((Options & RegexOptions.Singleline) != 0)
argSb.Append("-S");
if ((Options & RegexOptions.IgnorePatternWhitespace) != 0)
argSb.Append("-X");
if ((Options & RegexOptions.ECMAScript) != 0)
argSb.Append("-E");

string typeStr = Type switch
{
Oneloop => nameof(Oneloop),
Notoneloop => nameof(Notoneloop),
Setloop => nameof(Setloop),
Onelazy => nameof(Onelazy),
Notonelazy => nameof(Notonelazy),
Setlazy => nameof(Setlazy),
One => nameof(One),
Notone => nameof(Notone),
Set => nameof(Set),
Multi => nameof(Multi),
Ref => nameof(Ref),
Bol => nameof(Bol),
Eol => nameof(Eol),
Boundary => nameof(Boundary),
Nonboundary => nameof(Nonboundary),
ECMABoundary => nameof(ECMABoundary),
NonECMABoundary => nameof(NonECMABoundary),
Beginning => nameof(Beginning),
Start => nameof(Start),
EndZ => nameof(EndZ),
End => nameof(End),
Oneloopatomic => nameof(Oneloopatomic),
Notoneloopatomic => nameof(Notoneloopatomic),
Setloopatomic => nameof(Setloopatomic),
Nothing => nameof(Nothing),
Empty => nameof(Empty),
Lazyloop => nameof(Lazyloop),
Capture => nameof(Capture),
Group => nameof(Group),
Require => nameof(Require),
Prevent => nameof(Prevent),
Atomic => nameof(Atomic),
Testref => nameof(Testref),
Testgroup => nameof(Testgroup),
_ => "(unknown)"
};

var argSb = new StringBuilder().Append(typeStr);

if ((Options & RegexOptions.ExplicitCapture) != 0) argSb.Append("-C");
if ((Options & RegexOptions.IgnoreCase) != 0) argSb.Append("-I");
if ((Options & RegexOptions.RightToLeft) != 0) argSb.Append("-L");
if ((Options & RegexOptions.Multiline) != 0) argSb.Append("-M");
if ((Options & RegexOptions.Singleline) != 0) argSb.Append("-S");
if ((Options & RegexOptions.IgnorePatternWhitespace) != 0) argSb.Append("-X");
if ((Options & RegexOptions.ECMAScript) != 0) argSb.Append("-E");

switch (Type)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace System.Text.RegularExpressions
[Serializable]
internal sealed class RegexParseException : ArgumentException
{
private readonly RegexParseError _error;
private readonly RegexParseError _error; // tests access this via private reflection

/// <summary>
/// The error that happened during parsing.
Expand All @@ -27,28 +27,14 @@ public RegexParseException(RegexParseError error, int offset, string message) :
Offset = offset;
}

public RegexParseException() : base()
{
}

public RegexParseException(string message) : base(message)
{
}

public RegexParseException(string message, Exception inner) : base(message, inner)
{
}

private RegexParseException(SerializationInfo info, StreamingContext context)
: base(info, context)
private RegexParseException(SerializationInfo info, StreamingContext context) : base(info, context)
{
}

public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
base.GetObjectData(info, context);
// To maintain serialization support with netfx.
info.SetType(typeof(ArgumentException));
info.SetType(typeof(ArgumentException)); // To maintain serialization support with netfx.
}
}
}
Loading

0 comments on commit 95965c9

Please sign in to comment.