Skip to content

Commit

Permalink
Parse model values of the form 5e1 (no decimal) (#861)
Browse files Browse the repository at this point in the history
Previously, models that contained real literals with no decimal point
(e.g., 5e1) would lead to an exception in the model parser. This PR
adapts Boogie to recognize such values.

Ensuring that Z3 will emit one of these values in a model is tricky, and
a test for it would be fragile. But I've seen it appear in practice.
  • Loading branch information
atomb authored Apr 11, 2024
1 parent acc4377 commit 5cc90dc
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 4 deletions.
7 changes: 3 additions & 4 deletions Source/Model/Model.cs
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,7 @@ public override string ToString()

#region factory methods

Element ConstructElement(string name)
public Element ConstructElement(string name)
{
if (name.ToLower() == "true")
{
Expand Down Expand Up @@ -699,7 +699,6 @@ Element ConstructElement(string name)
}

var allDigits = new Regex(@"^-?[0-9]*$");
var real = new Regex(@"^-?[0-9]+\.[0-9]+$");
if (allDigits.IsMatch(name))
{
if (szi > 0)
Expand All @@ -711,7 +710,7 @@ Element ConstructElement(string name)
return new Integer(this, name);
}
}
else if (real.IsMatch(name))
else if (double.TryParse(name, out var _))
{
return new Real(this, name);
}
Expand Down Expand Up @@ -1077,4 +1076,4 @@ public static List<Model> ParseModels(System.IO.TextReader rd, Func<string, stri
return p.resModels;
}
}
}
}
49 changes: 49 additions & 0 deletions Source/UnitTests/CoreTests/Model.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using Microsoft.Boogie;
using NUnit.Framework;

namespace ModelTests
{
[TestFixture()]
public class ModelTests
{
[Test]
public void ParseRealModelElements()
{
var m = new Model();
var e1 = m.ConstructElement("1e2");
Assert.AreEqual(e1.Kind, Model.ElementKind.Real);
Assert.AreEqual(e1.ToString(), "1e2");
var e2 = m.ConstructElement("-3e5");
Assert.AreEqual(e2.Kind, Model.ElementKind.Real);
Assert.AreEqual(e2.ToString(), "-3e5");
var e3 = m.ConstructElement("1.2e3");
Assert.AreEqual(e3.Kind, Model.ElementKind.Real);
Assert.AreEqual(e3.ToString(), "1.2e3");
var e4 = m.ConstructElement("-3.1e6");
Assert.AreEqual(e4.Kind, Model.ElementKind.Real);
Assert.AreEqual(e4.ToString(), "-3.1e6");
}

[Test]
public void ParseBoolModelElements()
{
var m = new Model();
var e1 = m.ConstructElement("true");
Assert.AreEqual(e1, m.True);
var e2 = m.ConstructElement("false");
Assert.AreEqual(e2, m.False);
}

[Test]
public void ParseIntModelElements()
{
var m = new Model();
var e1 = m.ConstructElement("3289");
Assert.AreEqual(e1.Kind, Model.ElementKind.Integer);
Assert.AreEqual(e1.ToString(), "3289");
var e2 = m.ConstructElement("-12389");
Assert.AreEqual(e2.Kind, Model.ElementKind.Integer);
Assert.AreEqual(e2.ToString(), "-12389");
}
}
}

0 comments on commit 5cc90dc

Please sign in to comment.