Click here to Skip to main content
15,885,278 members
Articles / Desktop Programming / Windows Forms

A Tiny Expression Evaluator

Rate me:
Please Sign up or sign in to vote.
4.96/5 (71 votes)
18 Aug 2011CPOL10 min read 148.6K   4.1K   124  
A utility that allows you to enter simple and more complex mathematical formulas which will be evaluated and calculated on the spot
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using TinyExe;

namespace TinyExe.Tests
{
    /// <summary>
    /// tests reading and interpreting the basic literals
    /// supports: string, bool and double
    /// </summary>
    [TestClass]
    public class LiteralTests
    {
        [TestMethod]
        public void TestStringLiteral()
        {
            string answer = Expression.Eval<string>("\"Herre\"");
            Assert.AreEqual(answer, "Herre");
        }

        [TestMethod]
        public void TestStringLiteralAsObject()
        {
            object answer = new Expression("\"Herre\"").Eval();
            Assert.AreEqual(answer.ToString(), "Herre");
        }

        [TestMethod]
        public void TestRealLiteral()
        {
            double answer = Expression.Eval<double>("3.141592");
            Assert.AreEqual(answer, 3.141592);
        }

        [TestMethod]
        public void TestRealLiteralAsObject()
        {
            object answer = Expression.Eval("3.141592");
            Assert.AreEqual((double)answer, 3.141592);
        }

        [TestMethod]
        public void TestIntegerLiteral()
        {
            double answer = Expression.Eval<double>("42");
            Assert.AreEqual(answer, 42);
        }

        [TestMethod]
        public void TestBoolLiteral()
        {
            bool answer = Expression.Eval<bool>("true");
            Assert.AreEqual(answer, true);
        }

        [TestMethod]
        public void TestHexLiteral()
        {
            double answer = Expression.Eval<double>("0x42"); 
            Assert.AreEqual(answer, 0x42);

            answer = Expression.Eval<double>("0xFF");
            Assert.AreEqual(answer, 0xFF);
        }


        [TestMethod]
        public void TestBoolLiteralNegate()
        {
            bool answer = Expression.Eval<bool>("!true");
            Assert.AreEqual(answer, false);
        }

        [TestMethod]
        public void TestBoolLiteralNegateIllegal()
        {
            bool answer = Expression.Eval<bool>("!42"); // not default is return false
            Assert.AreEqual(answer, false);
        }

        [TestMethod]
        public void TestBoolLiteralNegateIllegalObject()
        {
            object answer = Expression.Eval("!42"); // expect null because of error
            Assert.AreEqual(answer, null);
        }

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Rubicon
Netherlands Netherlands
Currently Herre Kuijpers is employed at Rubicon. During his career he developed skills with all kinds of technologies, methodologies and programming languages such as c#, ASP.Net, .Net Core, VC++, Javascript, SQL, Agile, Scrum, DevOps, ALM. Currently he fulfills the role of software architect in various projects.

Herre Kuijpers is a very experienced software architect with deep knowledge of software design and development on the Microsoft .Net platform. He has a broad knowledge of Microsoft products and knows how these, in combination with custom software, can be optimally implemented in the often complex environment of the customer.

Comments and Discussions