Created
March 13, 2009 16:36
-
-
Save agross/78643 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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