1,257,058
clicked '
Ask Pex!
'
Sign In
Random Puzzle
Learn
APCS
New
C#
Visual Basic
F#
Does the Parameterized Unit Test pass for all input values? Click
Ask Pex!
to find out.
using System; using Microsoft.Pex.Framework; [PexClass] public class TestClass { // Which values will trigger collisions in MyHashSet? Ask Pex to find out! [PexMethod] // this puzzle is a 'Parameterized Unit Test' public void TestAddContains(int x, int y) { var s = new MyHashSet(); s.Add(x); s.Add(y); PexAssert.IsTrue(s.Contains(x)); PexAssert.IsTrue(s.Contains(y)); } } class MyHashSet { // a simple implementation of a hashtable, fixed in size, slightly buggy int[] buckets = new int[13]; private int getInitialBucketIndex(int value) { return (value ^ 101010101) % buckets.Length; } public void Add(int value) { if (value==0) throw new ArgumentException("'0' not allowed"); var index = getInitialBucketIndex(value); // this loop deals with collisions while (buckets[index] != value && buckets[index] != 0) { index = (index + 1) % buckets.Length; Console.WriteLine("[DEBUG INFO] collision in Add at index {0}", index); } buckets[index] = value; } public bool Contains(int value) { if (value == 0) throw new ArgumentException("'0' not allowed"); var index = getInitialBucketIndex(value); while (buckets[index] != value && buckets[index] != 0) index = (index + 1) % buckets.Length; return buckets[index] == value; } }
Permalink
Coding Duel Name:
Help
Community
Live Feed
Publications
About
© 2013 Microsoft - Pex v0.94 - .NET v4 -
Terms of Use
-
Privacy