1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . Linq ;
4+
5+ using NUnit . Framework ;
6+
7+ namespace CodeJam . Collections
8+ {
9+ [ TestFixture ]
10+ public class DictionaryCollectionExtensionsTest
11+ {
12+ #region List
13+
14+ [ Test ]
15+ public void AddOrCreateList_ICollection ( )
16+ {
17+ var d = new Dictionary < string , ICollection < int > > ( ) ;
18+ d . AddOrCreateList ( "a" , 1 ) ;
19+ Assert . AreEqual ( 1 , d [ "a" ] . First ( ) ) ;
20+
21+ d [ "b" ] = new List < int > { 2 } ;
22+ d . AddOrCreateList ( "b" , 3 ) ;
23+ Assert . AreEqual ( 2 , d [ "b" ] . First ( ) ) ;
24+ Assert . AreEqual ( 3 , d [ "b" ] . Last ( ) ) ;
25+ }
26+
27+ [ Test ]
28+ public void AddOrCreateList_IList ( )
29+ {
30+ var d = new Dictionary < string , IList < int > > ( ) ;
31+ d . AddOrCreateList ( "a" , 1 ) ;
32+ Assert . AreEqual ( 1 , d [ "a" ] . First ( ) ) ;
33+
34+ d [ "b" ] = new List < int > { 2 } ;
35+ d . AddOrCreateList ( "b" , 3 ) ;
36+ Assert . AreEqual ( 2 , d [ "b" ] . First ( ) ) ;
37+ Assert . AreEqual ( 3 , d [ "b" ] . Last ( ) ) ;
38+ }
39+
40+ [ Test ]
41+ public void AddOrCreateList_List ( )
42+ {
43+ var d = new Dictionary < string , List < int > > ( ) ;
44+ d . AddOrCreateList ( "a" , 1 ) ;
45+ Assert . AreEqual ( 1 , d [ "a" ] . First ( ) ) ;
46+
47+ d [ "b" ] = new List < int > { 2 } ;
48+ d . AddOrCreateList ( "b" , 3 ) ;
49+ Assert . AreEqual ( 2 , d [ "b" ] . First ( ) ) ;
50+ Assert . AreEqual ( 3 , d [ "b" ] . Last ( ) ) ;
51+ }
52+
53+ #endregion
54+
55+ #region HashSet
56+
57+ [ Test ]
58+ public void AddOrCreateHashSet_ICollection ( )
59+ {
60+ var d = new Dictionary < string , ICollection < int > > ( ) ;
61+ d . AddOrCreateHashSet ( "a" , 1 ) ;
62+ Assert . AreEqual ( 1 , d [ "a" ] . First ( ) ) ;
63+
64+ d [ "b" ] = new HashSet < int > { 2 } ;
65+ d . AddOrCreateHashSet ( "b" , 3 ) ;
66+ Assert . AreEqual ( 2 , d [ "b" ] . First ( ) ) ;
67+ Assert . AreEqual ( 3 , d [ "b" ] . Last ( ) ) ;
68+ }
69+
70+ #if ! LESSTHAN_NET40
71+ [ Test ]
72+ public void AddOrCreateHashSet_ISet ( )
73+ {
74+ var d = new Dictionary < string , ISet < int > > ( ) ;
75+ d . AddOrCreateHashSet ( "a" , 1 ) ;
76+ Assert . AreEqual ( 1 , d [ "a" ] . First ( ) ) ;
77+
78+ d [ "b" ] = new HashSet < int > { 2 } ;
79+ d . AddOrCreateHashSet ( "b" , 3 ) ;
80+ Assert . AreEqual ( 2 , d [ "b" ] . First ( ) ) ;
81+ Assert . AreEqual ( 3 , d [ "b" ] . Last ( ) ) ;
82+ }
83+ #endif
84+
85+ [ Test ]
86+ public void AddOrCreateHashSet_HashSet ( )
87+ {
88+ var d = new Dictionary < string , HashSet < int > > ( ) ;
89+ d . AddOrCreateHashSet ( "a" , 1 ) ;
90+ Assert . AreEqual ( 1 , d [ "a" ] . First ( ) ) ;
91+
92+ d [ "b" ] = new HashSet < int > { 2 } ;
93+ d . AddOrCreateHashSet ( "b" , 3 ) ;
94+ Assert . AreEqual ( 2 , d [ "b" ] . First ( ) ) ;
95+ Assert . AreEqual ( 3 , d [ "b" ] . Last ( ) ) ;
96+ }
97+
98+ #endregion
99+
100+ #region Collection
101+
102+ [ Test ]
103+ public void AddOrCreateCollection_ICollection ( )
104+ {
105+ var d = new Dictionary < string , ICollection < int > > ( ) ;
106+ d . AddOrCreateCollection ( "a" , 1 , ( ) => new List < int > ( ) ) ;
107+ Assert . AreEqual ( 1 , d [ "a" ] . First ( ) ) ;
108+
109+ d [ "b" ] = new HashSet < int > { 2 } ;
110+ d . AddOrCreateCollection ( "b" , 3 , ( ) => new List < int > ( ) ) ;
111+ Assert . AreEqual ( 2 , d [ "b" ] . First ( ) ) ;
112+ Assert . AreEqual ( 3 , d [ "b" ] . Last ( ) ) ;
113+ }
114+
115+ [ Test ]
116+ public void AddOrCreateCollection_IList ( )
117+ {
118+ var d = new Dictionary < string , IList < int > > ( ) ;
119+ d . AddOrCreateCollection ( "a" , 1 , ( ) => new List < int > ( ) ) ;
120+ Assert . AreEqual ( 1 , d [ "a" ] . First ( ) ) ;
121+
122+ d [ "b" ] = new List < int > { 2 } ;
123+ d . AddOrCreateCollection ( "b" , 3 , ( ) => new List < int > ( ) ) ;
124+ Assert . AreEqual ( 2 , d [ "b" ] . First ( ) ) ;
125+ Assert . AreEqual ( 3 , d [ "b" ] . Last ( ) ) ;
126+ }
127+
128+ #if ! LESSTHAN_NET40
129+ [ Test ]
130+ public void AddOrCreateCollection_ISet ( )
131+ {
132+ var d = new Dictionary < string , ISet < int > > ( ) ;
133+ d . AddOrCreateCollection ( "a" , 1 , ( ) => new HashSet < int > ( ) ) ;
134+ Assert . AreEqual ( 1 , d [ "a" ] . First ( ) ) ;
135+
136+ d [ "b" ] = new HashSet < int > { 2 } ;
137+ d . AddOrCreateCollection ( "b" , 3 , ( ) => new HashSet < int > ( ) ) ;
138+ Assert . AreEqual ( 2 , d [ "b" ] . First ( ) ) ;
139+ Assert . AreEqual ( 3 , d [ "b" ] . Last ( ) ) ;
140+ }
141+ #endif
142+
143+ [ Test ]
144+ public void AddOrCreateCollection_List ( )
145+ {
146+ var d = new Dictionary < string , List < int > > ( ) ;
147+ d . AddOrCreateCollection ( "a" , 1 , ( ) => new List < int > ( ) ) ;
148+ Assert . AreEqual ( 1 , d [ "a" ] . First ( ) ) ;
149+
150+ d [ "b" ] = new List < int > { 2 } ;
151+ d . AddOrCreateCollection ( "b" , 3 , ( ) => new List < int > ( ) ) ;
152+ Assert . AreEqual ( 2 , d [ "b" ] . First ( ) ) ;
153+ Assert . AreEqual ( 3 , d [ "b" ] . Last ( ) ) ;
154+ }
155+
156+ [ Test ]
157+ public void AddOrCreateCollection_HashSet ( )
158+ {
159+ var d = new Dictionary < string , HashSet < int > > ( ) ;
160+ d . AddOrCreateCollection ( "a" , 1 , ( ) => new HashSet < int > ( ) ) ;
161+ Assert . AreEqual ( 1 , d [ "a" ] . First ( ) ) ;
162+
163+ d [ "b" ] = new HashSet < int > { 2 } ;
164+ d . AddOrCreateCollection ( "b" , 3 , ( ) => new HashSet < int > ( ) ) ;
165+ Assert . AreEqual ( 2 , d [ "b" ] . First ( ) ) ;
166+ Assert . AreEqual ( 3 , d [ "b" ] . Last ( ) ) ;
167+ }
168+
169+ #endregion
170+ }
171+ }
0 commit comments