<commit_before>﻿using System;
using MonadLib;
using NUnit.Framework;

namespace MonadLibTests
{
    [TestFixture]
    internal class StateMonadLaws
    {
        [Test]
        public void BindLeftIdentity()
        {
            // (return x) >>= f == f x
            const double x = 5;
            Func<double, State<int, string>> f = d => State<int>.Return(Convert.ToString(d * d));
            var actual1 = State<int>.Return(x).Bind(f);
            var actual2 = f(x);
            const int s = 10;
            var pair1 = actual1.RunState(s);
            var pair2 = actual2.RunState(s);
            Assert.That(pair1, Is.EqualTo(pair2));
        }

        [Test]
        public void BindRightIdentity()
        {
            // m >>= return == m
            var m = State<int>.Return('X');
            var actual1 = m.Bind(State<int>.Return);
            var actual2 = m;
            const int s = 10;
            var pair1 = actual1.RunState(s);
            var pair2 = actual2.RunState(s);
            Assert.That(pair1, Is.EqualTo(pair2));
        }

        [Test]
        public void BindAssociativity()
        {
            // (m >>= f) >>= g == m >>= (\x -> f x >>= g)
            Func<int, State<int, double>> f = n => State<int>.Return(Convert.ToDouble(n * n + 0.1m));
            Func<double, State<int, string>> g = d => State<int>.Return(Convert.ToString(d));
            var m = State<int>.Return(5);
            var actual1 = m.Bind(f).Bind(g);
            var actual2 = m.Bind(x => f(x).Bind(g));
            const int s = 10;
            var pair1 = actual1.RunState(s);
            var pair2 = actual2.RunState(s);
            Assert.That(pair1, Is.EqualTo(pair2));
        }
    }
}
<commit_msg>Added unit tests for the Reader monad laws<commit_after>﻿using System;
using MonadLib;
using NUnit.Framework;

namespace MonadLibTests
{
    [TestFixture]
    internal class StateMonadLaws
    {
        [Test]
        public void BindLeftIdentity()
        {
            // (return x) >>= f == f x
            const double x = 5;
            Func<double, State<int, string>> f = d => State<int>.Return(Convert.ToString(d * d));
            var stateMonad1 = State<int>.Return(x).Bind(f);
            var stateMonad2 = f(x);
            const int s = 10;
            var pair1 = stateMonad1.RunState(s);
            var pair2 = stateMonad2.RunState(s);
            Assert.That(pair1, Is.EqualTo(pair2));
        }

        [Test]
        public void BindRightIdentity()
        {
            // m >>= return == m
            var m = State<int>.Return('X');
            var stateMonad1 = m.Bind(State<int>.Return);
            var stateMonad2 = m;
            const int s = 10;
            var pair1 = stateMonad1.RunState(s);
            var pair2 = stateMonad2.RunState(s);
            Assert.That(pair1, Is.EqualTo(pair2));
        }

        [Test]
        public void BindAssociativity()
        {
            // (m >>= f) >>= g == m >>= (\x -> f x >>= g)
            Func<int, State<int, double>> f = n => State<int>.Return(Convert.ToDouble(n * n + 0.1m));
            Func<double, State<int, string>> g = d => State<int>.Return(Convert.ToString(d));
            var m = State<int>.Return(5);
            var stateMonad1 = m.Bind(f).Bind(g);
            var stateMonad2 = m.Bind(x => f(x).Bind(g));
            const int s = 10;
            var pair1 = stateMonad1.RunState(s);
            var pair2 = stateMonad2.RunState(s);
            Assert.That(pair1, Is.EqualTo(pair2));
        }
    }
}

{'fraction_non_alphanumeric': 0.14249219415271075, 'fraction_numerical': 0.01930173147885325, 'mean_word_length': 1.6778115501519757, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 63, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5425775', 'n_tokens_mistral': 1136, 'n_tokens_neox': 1114, 'n_words': 259}