public void Insert() { RleArray <char> chars = new RleArray <char>(); Set(chars, "aabb"); chars.Insert(2, 'a'); Assert.AreEqual(5, chars.Count); Assert.AreEqual('a', chars[0]); Assert.AreEqual('a', chars[1]); Assert.AreEqual('a', chars[2]); Assert.AreEqual('b', chars[3]); Assert.AreEqual('b', chars[4]); MyAssert.Throws <ArgumentOutOfRangeException>(delegate { chars[-1].ToString(); }); MyAssert.Throws <ArgumentOutOfRangeException>(delegate { chars[5].ToString(); }); Set(chars, "aabb"); chars.Insert(2, 'b'); Assert.AreEqual(5, chars.Count); Assert.AreEqual('a', chars[0]); Assert.AreEqual('a', chars[1]); Assert.AreEqual('b', chars[2]); Assert.AreEqual('b', chars[3]); Assert.AreEqual('b', chars[4]); Set(chars, "aabb"); chars.Insert(2, 'c'); Assert.AreEqual(5, chars.Count); Assert.AreEqual("a a c b b", chars.ToString()); Set(chars, "aabb"); chars.Insert(1, 'a'); Assert.AreEqual(5, chars.Count); Assert.AreEqual('a', chars[0]); Assert.AreEqual('a', chars[1]); Assert.AreEqual('a', chars[2]); Assert.AreEqual('b', chars[3]); Assert.AreEqual('b', chars[4]); Set(chars, "aabb"); chars.Insert(1, 'c'); Assert.AreEqual(5, chars.Count); Assert.AreEqual('a', chars[0]); Assert.AreEqual('c', chars[1]); Assert.AreEqual('a', chars[2]); Assert.AreEqual('b', chars[3]); Assert.AreEqual('b', chars[4]); }
static void Test_Insert() { RleArray <char> chars = new RleArray <char>(); Set(chars, "aabb"); chars.Insert(2, 'a'); TestUtl.AssertEquals(5, chars.Count); TestUtl.AssertEquals('a', chars[0]); TestUtl.AssertEquals('a', chars[1]); TestUtl.AssertEquals('a', chars[2]); TestUtl.AssertEquals('b', chars[3]); TestUtl.AssertEquals('b', chars[4]); try{ chars[-1].ToString(); throw new Exception(); } catch (Exception ex) { TestUtl.AssertExceptionType <ArgumentOutOfRangeException>(ex); } try{ chars[5].ToString(); throw new Exception(); } catch (Exception ex) { TestUtl.AssertExceptionType <ArgumentOutOfRangeException>(ex); } Set(chars, "aabb"); chars.Insert(2, 'b'); TestUtl.AssertEquals(5, chars.Count); TestUtl.AssertEquals('a', chars[0]); TestUtl.AssertEquals('a', chars[1]); TestUtl.AssertEquals('b', chars[2]); TestUtl.AssertEquals('b', chars[3]); TestUtl.AssertEquals('b', chars[4]); Set(chars, "aabb"); chars.Insert(2, 'c'); TestUtl.AssertEquals(5, chars.Count); TestUtl.AssertEquals("a a c b b", chars.ToString()); Set(chars, "aabb"); chars.Insert(1, 'a'); TestUtl.AssertEquals(5, chars.Count); TestUtl.AssertEquals('a', chars[0]); TestUtl.AssertEquals('a', chars[1]); TestUtl.AssertEquals('a', chars[2]); TestUtl.AssertEquals('b', chars[3]); TestUtl.AssertEquals('b', chars[4]); Set(chars, "aabb"); chars.Insert(1, 'c'); TestUtl.AssertEquals(5, chars.Count); TestUtl.AssertEquals('a', chars[0]); TestUtl.AssertEquals('c', chars[1]); TestUtl.AssertEquals('a', chars[2]); TestUtl.AssertEquals('b', chars[3]); TestUtl.AssertEquals('b', chars[4]); }
static void Test_GetSet() { RleArray <char> chars; // get chars = new RleArray <char>(); Set(chars, "abc"); TestUtl.AssertEquals(3, chars.Count); TestUtl.AssertEquals('a', chars[0]); TestUtl.AssertEquals('b', chars[1]); TestUtl.AssertEquals('c', chars[2]); try{ chars[-1].ToString(); throw new Exception(); } catch (Exception ex) { TestUtl.AssertExceptionType <ArgumentOutOfRangeException>(ex); } try{ chars[5].ToString(); throw new Exception(); } catch (Exception ex) { TestUtl.AssertExceptionType <ArgumentOutOfRangeException>(ex); } // set - out of bounds chars = new RleArray <char>(); Set(chars, "abc"); try{ chars[-1] = 'z'; throw new Exception(); } catch (Exception ex) { TestUtl.AssertExceptionType <ArgumentOutOfRangeException>(ex); } try{ chars[4] = 'd'; throw new Exception(); } catch (Exception ex) { TestUtl.AssertExceptionType <ArgumentOutOfRangeException>(ex); } { // set - same value chars = new RleArray <char>(); Set(chars, "aabbbcc"); chars[2] = 'b'; TestUtl.AssertEquals("a a b b b c c", chars.ToString()); TestUtl.AssertEquals("[2|a] [3|b] [2|c]", chars._Nodes.ToString()); // set - new value on left boundary chars = new RleArray <char>(); Set(chars, "aabbbcc"); chars[2] = 'X'; TestUtl.AssertEquals("a a X b b c c", chars.ToString()); TestUtl.AssertEquals("[2|a] [1|X] [2|b] [2|c]", chars._Nodes.ToString()); // set - same value as the previous node on left boundary chars = new RleArray <char>(); Set(chars, "aabbbcc"); chars[2] = 'a'; TestUtl.AssertEquals("a a a b b c c", chars.ToString()); TestUtl.AssertEquals("[3|a] [2|b] [2|c]", chars._Nodes.ToString()); // set - new value in the middle of a node chars = new RleArray <char>(); Set(chars, "aabbbcc"); chars[3] = 'a'; TestUtl.AssertEquals("a a b a b c c", chars.ToString()); TestUtl.AssertEquals("[2|a] [1|b] [1|a] [1|b] [2|c]", chars._Nodes.ToString()); // set - new value on right boundary chars = new RleArray <char>(); Set(chars, "aabbbcc"); chars[4] = 'X'; TestUtl.AssertEquals("a a b b X c c", chars.ToString()); TestUtl.AssertEquals("[2|a] [2|b] [1|X] [2|c]", chars._Nodes.ToString()); // set - same value as the next node on right boundary chars = new RleArray <char>(); Set(chars, "aabbbcc"); chars[4] = 'c'; TestUtl.AssertEquals("a a b b c c c", chars.ToString()); TestUtl.AssertEquals("[2|a] [2|b] [3|c]", chars._Nodes.ToString()); } { // set - combines with previous node and the node disappears chars = new RleArray <char>(); Set(chars, "aabcc"); chars[2] = 'a'; TestUtl.AssertEquals("a a a c c", chars.ToString()); TestUtl.AssertEquals("[3|a] [2|c]", chars._Nodes.ToString()); // set - combines with next node and the node disappears chars = new RleArray <char>(); Set(chars, "aabcc"); chars[2] = 'c'; TestUtl.AssertEquals("a a c c c", chars.ToString()); TestUtl.AssertEquals("[2|a] [3|c]", chars._Nodes.ToString()); // set - combines with next and previous node, and the node disappears chars = new RleArray <char>(); Set(chars, "aabaa"); chars[2] = 'a'; TestUtl.AssertEquals("a a a a a", chars.ToString()); TestUtl.AssertEquals("[5|a]", chars._Nodes.ToString()); // set - combines with previous node and the node disappears chars = new RleArray <char>(); Set(chars, "aabcc"); chars[2] = 'B'; TestUtl.AssertEquals("a a B c c", chars.ToString()); TestUtl.AssertEquals("[2|a] [1|B] [2|c]", chars._Nodes.ToString()); } }
public void GetSet() { RleArray <char> chars; // get chars = new RleArray <char>(); Set(chars, "abc"); Assert.AreEqual(3, chars.Count); Assert.AreEqual('a', chars[0]); Assert.AreEqual('b', chars[1]); Assert.AreEqual('c', chars[2]); MyAssert.Throws <ArgumentOutOfRangeException>(delegate { chars[-1].ToString(); }); MyAssert.Throws <ArgumentOutOfRangeException>(delegate { chars[5].ToString(); }); // set - out of bounds chars = new RleArray <char>(); Set(chars, "abc"); MyAssert.Throws <ArgumentOutOfRangeException>(delegate { chars[-1] = 'z'; }); MyAssert.Throws <ArgumentOutOfRangeException>(delegate { chars[4] = 'd'; }); { // set - same value chars = new RleArray <char>(); Set(chars, "aabbbcc"); chars[2] = 'b'; Assert.AreEqual("a a b b b c c", chars.ToString()); Assert.AreEqual("[2|a] [3|b] [2|c]", chars._Nodes.ToString()); // set - new value on left boundary chars = new RleArray <char>(); Set(chars, "aabbbcc"); chars[2] = 'X'; Assert.AreEqual("a a X b b c c", chars.ToString()); Assert.AreEqual("[2|a] [1|X] [2|b] [2|c]", chars._Nodes.ToString()); // set - same value as the previous node on left boundary chars = new RleArray <char>(); Set(chars, "aabbbcc"); chars[2] = 'a'; Assert.AreEqual("a a a b b c c", chars.ToString()); Assert.AreEqual("[3|a] [2|b] [2|c]", chars._Nodes.ToString()); // set - new value in the middle of a node chars = new RleArray <char>(); Set(chars, "aabbbcc"); chars[3] = 'a'; Assert.AreEqual("a a b a b c c", chars.ToString()); Assert.AreEqual("[2|a] [1|b] [1|a] [1|b] [2|c]", chars._Nodes.ToString()); // set - new value on right boundary chars = new RleArray <char>(); Set(chars, "aabbbcc"); chars[4] = 'X'; Assert.AreEqual("a a b b X c c", chars.ToString()); Assert.AreEqual("[2|a] [2|b] [1|X] [2|c]", chars._Nodes.ToString()); // set - same value as the next node on right boundary chars = new RleArray <char>(); Set(chars, "aabbbcc"); chars[4] = 'c'; Assert.AreEqual("a a b b c c c", chars.ToString()); Assert.AreEqual("[2|a] [2|b] [3|c]", chars._Nodes.ToString()); } { // set - combines with previous node and the node disappears chars = new RleArray <char>(); Set(chars, "aabcc"); chars[2] = 'a'; Assert.AreEqual("a a a c c", chars.ToString()); Assert.AreEqual("[3|a] [2|c]", chars._Nodes.ToString()); // set - combines with next node and the node disappears chars = new RleArray <char>(); Set(chars, "aabcc"); chars[2] = 'c'; Assert.AreEqual("a a c c c", chars.ToString()); Assert.AreEqual("[2|a] [3|c]", chars._Nodes.ToString()); // set - combines with next and previous node, and the node disappears chars = new RleArray <char>(); Set(chars, "aabaa"); chars[2] = 'a'; Assert.AreEqual("a a a a a", chars.ToString()); Assert.AreEqual("[5|a]", chars._Nodes.ToString()); // set - combines with previous node and the node disappears chars = new RleArray <char>(); Set(chars, "aabcc"); chars[2] = 'B'; Assert.AreEqual("a a B c c", chars.ToString()); Assert.AreEqual("[2|a] [1|B] [2|c]", chars._Nodes.ToString()); } }