You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TestCase_EnumerableHelper.asp 13KB

пре 8 месеци
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. <%
  2. Class EnumerableHelper_Tests
  3. Public Sub Setup : End Sub
  4. Public Sub Teardown : End Sub
  5. Public Function TestCaseNames
  6. TestCaseNames = Array("Test_Enumerable_Function_Returns_EnumerableHelper_Instance", _
  7. "Test_Map_Returns_EnumerableHelper_Instance", _
  8. "Test_Map_Lambda", _
  9. "Test_Map_Proc", _
  10. "Test_Max_Lambda", _
  11. "Test_Max_Proc", _
  12. "Test_Min_Lambda", _
  13. "Test_Min_Proc", _
  14. "Test_Take", _
  15. "Test_TakeWhile_Lambda", _
  16. "Test_TakeWhile_Proc", _
  17. "Test_Sum", _
  18. "Test_All", _
  19. "Test_Any", _
  20. "Test_Where_Lambda", _
  21. "Test_Where_Proc", _
  22. "Test_Count", _
  23. "Test_First", _
  24. "Test_Last", _
  25. "Test_Chained")
  26. End Function
  27. '---------------------------------------------------------------------------------------------------------------------
  28. Public Sub Test_Enumerable_Function_Returns_EnumerableHelper_Instance(T)
  29. dim list : set list = new LinkedList_Class
  30. dim E : set E = Enumerable(list)
  31. T.Assert typename(E) = "EnumerableHelper_Class", "did not return correct instance"
  32. T.Assert typename(E.Data) = "LinkedList_Class", "Data() is of type " & typename(E.Data)
  33. End Sub
  34. '---------------------------------------------------------------------------------------------------------------------
  35. Public Sub Test_Map_Returns_EnumerableHelper_Instance(T)
  36. dim list : set list = new LinkedList_Class
  37. dim E : set E = Enumerable(list).Map("")
  38. T.Assert typename(E) = "EnumerableHelper_Class", "returned type " & typename(E)
  39. End Sub
  40. '---------------------------------------------------------------------------------------------------------------------
  41. Public Sub Test_Map_Lambda(T)
  42. dim list : set list = OrderedList(10)
  43. dim E : set E = Enumerable(list).Map("dim x : x = item_ + 1 : V_ = x")
  44. T.Assert 10 = E.Data.Count, "count: " & E.Data.Count
  45. dim A : A = list.To_Array()
  46. dim B : B = E.Data.To_Array()
  47. T.Assert UBound(A) = UBound(B), "A = " & UBound(A) & ", B = " & UBound(B)
  48. dim i
  49. For i = 0 to UBound(A)
  50. T.Assert B(i) = A(i) + 1, "B(" & i & ") = " & B(i) & ", A(" & i & ") = " & A(i)
  51. Next
  52. End Sub
  53. '---------------------------------------------------------------------------------------------------------------------
  54. Public Sub Test_Map_Proc(T)
  55. dim list : set list = OrderedList(10)
  56. dim E : set E = Enumerable(list).Map(GetRef("AddOne"))
  57. T.Assert 10 = E.Data.Count, "count: " & E.Data.Count
  58. dim A : A = list.To_Array()
  59. dim B : B = E.Data.To_Array()
  60. T.Assert UBound(A) = UBound(B), "A = " & UBound(A) & ", B = " & UBound(B)
  61. dim i
  62. For i = 0 to UBound(A)
  63. T.Assert B(i) = A(i) + 1, "B(" & i & ") = " & B(i) & ", A(" & i & ") = " & A(i)
  64. Next
  65. End Sub
  66. '---------------------------------------------------------------------------------------------------------------------
  67. Public Sub Test_Max_Lambda(T)
  68. dim list : set list = new LinkedList_Class
  69. list.Push "Alice"
  70. list.Push "Bob"
  71. list.Push "Charlie"
  72. list.Push "Doug"
  73. dim val : val = Enumerable(list).Max("len(item_)")
  74. T.Assert 7 = val, "val = " & val
  75. End Sub
  76. '---------------------------------------------------------------------------------------------------------------------
  77. Public Sub Test_Max_Proc(T)
  78. dim list : set list = new LinkedList_Class
  79. list.Push "Alice"
  80. list.Push "Bob"
  81. list.Push "Charlie"
  82. list.Push "Doug"
  83. dim val : val = Enumerable(list).Max(GetRef("GetLength"))
  84. T.Assert 7 = val, "val = " & val
  85. End Sub
  86. '---------------------------------------------------------------------------------------------------------------------
  87. Public Sub Test_Min_Lambda(T)
  88. dim list : set list = new LinkedList_Class
  89. list.Push "Alice"
  90. list.Push "Bob"
  91. list.Push "Charlie"
  92. list.Push "Doug"
  93. dim val : val = Enumerable(list).Min("len(item_)")
  94. T.Assert 3 = val, "val = " & val
  95. End Sub
  96. '---------------------------------------------------------------------------------------------------------------------
  97. Public Sub Test_Min_Proc(T)
  98. dim list : set list = new LinkedList_Class
  99. list.Push "Alice"
  100. list.Push "Bob"
  101. list.Push "Charlie"
  102. list.Push "Doug"
  103. dim val : val = Enumerable(list).Min(GetRef("GetLength"))
  104. T.Assert 3 = val, "val = " & val
  105. End Sub
  106. '---------------------------------------------------------------------------------------------------------------------
  107. Public Sub Test_Take(T)
  108. dim list : set list = OrderedList(3)
  109. dim firstTwo : set firstTwo = Enumerable(list).Take(2)
  110. T.Assert typename(firstTwo) = "EnumerableHelper_Class", "typename = " & typename(firstTwo)
  111. dim L : set L = firstTwo.Data
  112. T.AssertEqual 2, L.Count, "Count = " & L.Count
  113. T.AssertEqual 1, L.Front, "Front = " & L.Front
  114. T.AssertEqual 2, L.Back, "Back = " & L.Back
  115. End Sub
  116. '---------------------------------------------------------------------------------------------------------------------
  117. Public Sub Test_TakeWhile_Lambda(T)
  118. dim list : set list = OrderedList(5)
  119. dim firstTwo : set firstTwo = Enumerable(list).TakeWhile("item_ < 3")
  120. T.Assert typename(firstTwo) = "EnumerableHelper_Class", "typename = " & typename(firstTwo)
  121. dim L : set L = firstTwo.Data
  122. T.AssertEqual 2, L.Count, "Count = " & L.Count
  123. T.AssertEqual 1, L.Front, "Front = " & L.Front
  124. T.AssertEqual 2, L.Back, "Back = " & L.Back
  125. End Sub
  126. '---------------------------------------------------------------------------------------------------------------------
  127. Public Sub Test_TakeWhile_Proc(T)
  128. dim list : set list = OrderedList(5)
  129. dim firstTwo : set firstTwo = Enumerable(list).TakeWhile(GetRef("IsLessThanThree"))
  130. T.Assert typename(firstTwo) = "EnumerableHelper_Class", "typename = " & typename(firstTwo)
  131. dim L : set L = firstTwo.Data
  132. T.AssertEqual 2, L.Count, "Count = " & L.Count
  133. T.AssertEqual 1, L.Front, "Front = " & L.Front
  134. T.AssertEqual 2, L.Back, "Back = " & L.Back
  135. End Sub
  136. '---------------------------------------------------------------------------------------------------------------------
  137. Public Sub Test_Sum(T)
  138. dim list : set list = OrderedList(5)
  139. dim val : val = Enumerable(list).Sum("item_")
  140. T.AssertEqual val, 15, "simple: val = " & val
  141. val = Enumerable(list).Sum("item_ * item_")
  142. T.AssertEqual val, 55, "squares: val = " & val
  143. End Sub
  144. '---------------------------------------------------------------------------------------------------------------------
  145. Public Sub Test_All(T)
  146. dim list : set list = new LinkedList_Class
  147. list.Push "Alice"
  148. list.Push "Bob"
  149. list.Push "Charlie"
  150. list.Push "Doug"
  151. dim val
  152. val = Enumerable(list).All("len(item_) >= 3")
  153. T.Assert val, "Len >= 3: val = " & val
  154. val = Enumerable(list).All("len(item_) = 3")
  155. T.AssertFalse val, "Len = 3: val = " & val
  156. End Sub
  157. '---------------------------------------------------------------------------------------------------------------------
  158. Public Sub Test_Any(T)
  159. dim list : set list = new LinkedList_Class
  160. list.Push "Alice"
  161. list.Push "Bob"
  162. list.Push "Charlie"
  163. list.Push "Doug"
  164. dim val
  165. val = Enumerable(list).Any("len(item_) >= 3")
  166. T.Assert val, "Len >= 3: val = " & val
  167. val = Enumerable(list).Any("len(item_) < 3")
  168. T.AssertFalse val, "Len < 3: val = " & val
  169. End Sub
  170. '---------------------------------------------------------------------------------------------------------------------
  171. Public Sub Test_Where_Lambda(T)
  172. dim list : set list = new LinkedList_Class
  173. list.Push "Alice"
  174. list.Push "Bob"
  175. list.Push "Charlie"
  176. list.Push "Doug"
  177. dim list2
  178. set list2 = Enumerable(list).Where("len(item_) > 4")
  179. T.AssertEqual 2, list2.Data.Count, "list2.Count = " & list2.Data.Count
  180. T.AssertEqual "Alice", list2.Data.Front(), "list2.Data.Front = " & list2.Data.Front()
  181. T.AssertEqual "Charlie", list2.Data.Back(), "list2.Data.Front = " & list2.Data.Back()
  182. End Sub
  183. '---------------------------------------------------------------------------------------------------------------------
  184. Public Sub Test_Where_Proc(T)
  185. dim list : set list = new LinkedList_Class
  186. list.Push "Alice"
  187. list.Push "Bob"
  188. list.Push "Charlie"
  189. list.Push "Doug"
  190. dim E : set E = Enumerable(list).Where(GetRef("IsMoreThanFourChars"))
  191. T.AssertEqual 2, E.Data.Count, "E.Count = " & E.Data.Count
  192. T.AssertEqual "Alice", E.Data.Front(), "E.Data.Front = " & E.Data.Front()
  193. T.AssertEqual "Charlie", E.Data.Back(), "E.Data.Front = " & E.Data.Back()
  194. End Sub
  195. '---------------------------------------------------------------------------------------------------------------------
  196. Public Sub Test_Count(T)
  197. dim list : set list = OrderedList(10)
  198. dim E : set E = Enumerable(list)
  199. T.AssertEqual 10, E.Count(), empty
  200. End Sub
  201. '---------------------------------------------------------------------------------------------------------------------
  202. Public Sub Test_First(T)
  203. dim list : set list = OrderedList(10)
  204. dim E : set E = Enumerable(list)
  205. T.AssertEqual 1, E.First(), empty
  206. End Sub
  207. '---------------------------------------------------------------------------------------------------------------------
  208. Public Sub Test_Last(T)
  209. dim list : set list = OrderedList(10)
  210. dim E : set E = Enumerable(list)
  211. T.AssertEqual 10, E.Last(), empty
  212. End Sub
  213. '---------------------------------------------------------------------------------------------------------------------
  214. Public Sub Test_Chained(T)
  215. dim list : set list = OrderedList(10)
  216. dim it, item
  217. dim A : set A = Enumerable(list).Take(5).Where("item_ mod 2 <> 0")
  218. T.AssertEqual 3, A.Data.Count, "A.Count = " & A.Data.Count
  219. set it = A.Data.Iterator
  220. item = it.GetNext()
  221. T.AssertEqual 1, item, "A 1st item = " & item
  222. item = it.GetNext()
  223. T.AssertEqual 3, item, "A 2nd item = " & item
  224. item = it.GetNext()
  225. T.AssertEqual 5, item, "A 3rd item = " & item
  226. dim B : set B = Enumerable(list).Take(5).Select("V_ = item_ * item_")
  227. T.AssertEqual 5, B.Data.Count, "B.Count = " & B.Data.Count
  228. set it = B.Data.Iterator
  229. item = it.GetNext()
  230. T.AssertEqual 1, item, "B 1st item = " & item
  231. item = it.GetNext()
  232. T.AssertEqual 4, item, "B 2nd item = " & item
  233. item = it.GetNext()
  234. T.AssertEqual 9, item, "B 3rd item = " & item
  235. item = it.GetNext()
  236. T.AssertEqual 16, item, "B 4th item = " & item
  237. item = it.GetNext()
  238. T.AssertEqual 25, item, "B 5th item = " & item
  239. dim list2 : set list2 = new LinkedList_Class
  240. list2.Push "Alice"
  241. list2.Push "Bob"
  242. list2.Push "Charlie"
  243. list2.Push "Doug"
  244. list2.Push "Edward"
  245. list2.Push "Franklin"
  246. list2.Push "George"
  247. list2.Push "Hal"
  248. list2.Push "Isaac"
  249. list2.Push "Jeremy"
  250. dim C : C = Enumerable(list2) _
  251. .Where("len(item_) > 5") _
  252. .Map("set V_ = new ChainedExample_Class : V_.Data = item_ : V_.Length = len(item_)") _
  253. .Max("item_.Length")
  254. T.AssertEqual 8, C, "C"
  255. End Sub
  256. End Class
  257. Class ChainedExample_Class
  258. Public Data
  259. Public Length
  260. End Class
  261. Function OrderedList(maxnum)
  262. dim list : set list = new LinkedList_Class
  263. dim i
  264. For i = 1 to maxnum
  265. list.Push i
  266. Next
  267. set OrderedList = list
  268. End Function
  269. Function AddOne(x)
  270. AddOne = x + 1
  271. End Function
  272. Function GetLength(s)
  273. GetLength = Len(s)
  274. End Function
  275. Function IsLessThanThree(x)
  276. IsLessThanThree = x < 3
  277. End Function
  278. Function IsMoreThanFourChars(s)
  279. IsMoreThanFourChars = Len(s) > 4
  280. End Function
  281. %>

Powered by TurnKey Linux.