Skip to content

Instantly share code, notes, and snippets.

@agross
Created March 13, 2009 16:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agross/78643 to your computer and use it in GitHub Desktop.
Save agross/78643 to your computer and use it in GitHub Desktop.
using System;
using System.Diagnostics;
using System.Threading;
using Indigo.ForTesting;
using Machine.Specifications;
namespace Indigo.Tools.Tests
{
[Subject(typeof(Memoize))]
public class When_a_function_is_memoized
{
static Func<int, int> Func;
static int FirstResult;
static int SecondResult;
static Stopwatch FirstTime;
static Stopwatch SecondTime;
Establish context = () =>
{
Func = x =>
{
Thread.Sleep(1000);
return x * 10;
};
Func = Memoize.This(Func);
};
Because of = () =>
{
FirstTime = Measure.Time(() => FirstResult = Func(9));
SecondTime = Measure.Time(() => SecondResult = Func(9));
};
It should_compute_the_correct_result = () => FirstResult.ShouldEqual(90);
It should_take_a_long_time_on_the_first_invocation = () => FirstTime.Elapsed.ShouldBeGreaterThan(TimeSpan.FromMilliseconds(900));
It should_compute_the_correct_result_on_the_second_invocation = () => SecondResult.ShouldEqual(90);
It should_take_a_short_time_on_the_second_invocation = () => SecondTime.Elapsed.ShouldBeLessThan(TimeSpan.FromMilliseconds(500));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment