forked from DotNetKoans/DotNetKoans
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAboutArrays.cs
110 lines (88 loc) · 3.53 KB
/
AboutArrays.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
using System;
using System.Linq;
using System.Reflection;
using System.Collections;
using System.Collections.Generic;
using Xunit;
using DotNetCoreKoans.Engine;
namespace DotNetCoreKoans.Koans
{
public class AboutArrays : Koan
{
[Step(1)]
public void CreatingArrays()
{
var empty_array = new object[] { };
Assert.Equal(typeof(FillMeIn), empty_array.GetType());
//Note that you have to explicitly check for subclasses
Assert.True(typeof(Array).IsAssignableFrom(empty_array.GetType()));
Assert.Equal(FILL_ME_IN, empty_array.Length);
}
[Step(2)]
public void ArrayLiterals()
{
//You don't have to specify a type if the arguments can be inferred
var array = new[] { 42 };
Assert.Equal(typeof(int[]), array.GetType());
Assert.Equal(new int[] { 42 }, array);
//Are arrays 0-based or 1-based?
Assert.Equal(42, array[FILL_ME_IN]);
//This is important because...
Assert.True(array.IsFixedSize);
//...it means we can't do this: array[1] = 13;
Assert.Throws(typeof(FillMeIn), delegate () { array[1] = 13; });
//This is because the array is fixed at length 1. You could write a function
//which created a new array bigger than the last, copied the elements over, and
//returned the new array. Or you could do this:
List<int> dynamicArray = new List<int>();
dynamicArray.Add(42);
Assert.Equal(array, dynamicArray.ToArray());
dynamicArray.Add(13);
Assert.Equal((new int[] { 42, FILL_ME_IN }), dynamicArray.ToArray());
}
[Step(3)]
public void AccessingArrayElements()
{
var array = new[] { "peanut", "butter", "and", "jelly" };
Assert.Equal(FILL_ME_IN, array[0]);
Assert.Equal(FILL_ME_IN, array[3]);
//This doesn't work: Assert.Equal(FILL_ME_IN, array[-1]);
}
[Step(4)]
public void SlicingArrays()
{
var array = new[] { "peanut", "butter", "and", "jelly" };
Assert.Equal(new string[] { FILL_ME_IN, FILL_ME_IN }, array.Take(2).ToArray());
Assert.Equal(new string[] { FILL_ME_IN, FILL_ME_IN }, array.Skip(1).Take(2).ToArray());
}
[Step(5)]
public void PushingAndPopping()
{
var array = new[] { 1, 2 };
var stack = new Stack(array);
stack.Push("last");
Assert.Equal(FILL_ME_IN, stack.ToArray());
var poppedValue = stack.Pop();
Assert.Equal(FILL_ME_IN, poppedValue);
Assert.Equal(FILL_ME_IN, stack.ToArray());
}
[Step(6)]
public void Shifting()
{
//Shift == Remove First Element
//Unshift == Insert Element at Beginning
//C# doesn't provide this natively. You have a couple
//of options, but we'll use the LinkedList<T> to implement
var array = new[] { "Hello", "World" };
var list = new LinkedList<string>(array);
list.AddFirst("Say");
Assert.Equal(FILL_ME_IN, list.ToArray());
list.RemoveLast();
Assert.Equal(FILL_ME_IN, list.ToArray());
list.RemoveFirst();
Assert.Equal(FILL_ME_IN, list.ToArray());
list.AddAfter(list.Find("Hello"), "World");
Assert.Equal(FILL_ME_IN, list.ToArray());
}
}
}