@@ -18,6 +18,14 @@ Make sure each method works on:
1818* Null array (null)
1919*)
2020
21+ type ArrayWindowedTestInput < 't > =
22+ {
23+ InputArray: 't []
24+ WindowSize: int
25+ ExpectedArray: 't [][]
26+ Exception: Type option
27+ }
28+
2129[<TestFixture>]
2230type ArrayModule2 () =
2331
@@ -1001,6 +1009,96 @@ type ArrayModule2() =
10011009
10021010()
10031011
1012+ [<Test>]
1013+ member this.Windowed () =
1014+ let testWindowed config =
1015+ try
1016+ config.InputArray
1017+ |> Array.windowed config.WindowSize
1018+ |> ( fun actual -> Assert.AreEqual( config.ExpectedArray, actual))
1019+ with
1020+ | _ when Option.isNone config.Exception-> Assert.Fail()
1021+ | ewhen e.GetType() = ( Option.get config.Exception) -> ()
1022+ | _ -> Assert.Fail()
1023+
1024+ {
1025+ InputArray= [| 1 .. 10 |]
1026+ WindowSize= 1
1027+ ExpectedArray= [| for iin 1 .. 10 do yield [| i|] |]
1028+ Exception= None
1029+ } |> testWindowed
1030+ {
1031+ InputArray= [| 1 .. 10 |]
1032+ WindowSize= 5
1033+ ExpectedArray= [| for iin 1 .. 6 do yield [| i; i+ 1 ; i+ 2 ; i+ 3 ; i+ 4 |] |]
1034+ Exception= None
1035+ } |> testWindowed
1036+ {
1037+ InputArray= [| 1 .. 10 |]
1038+ WindowSize= 10
1039+ ExpectedArray= [| yield [| 1 .. 10 |] |]
1040+ Exception= None
1041+ } |> testWindowed
1042+ {
1043+ InputArray= [| 1 .. 10 |]
1044+ WindowSize= 25
1045+ ExpectedArray= [| |]
1046+ Exception= None
1047+ } |> testWindowed
1048+ {
1049+ InputArray= [| " str1" ; " str2" ; " str3" ; " str4" |]
1050+ WindowSize= 2
1051+ ExpectedArray= [| [| " str1" ; " str2" |]; [| " str2" ; " str3" |]; [| " str3" ; " str4" |] |]
1052+ Exception= None
1053+ } |> testWindowed
1054+ {
1055+ InputArray= [| |]
1056+ WindowSize= 2
1057+ ExpectedArray= [| |]
1058+ Exception= None
1059+ } |> testWindowed
1060+ {
1061+ InputArray= null
1062+ WindowSize= 2
1063+ ExpectedArray= [| |]
1064+ Exception= Some typeof< ArgumentNullException>
1065+ } |> testWindowed
1066+ {
1067+ InputArray= [| 1 .. 10 |]
1068+ WindowSize= 0
1069+ ExpectedArray= [| |]
1070+ Exception= Some typeof< ArgumentException>
1071+ } |> testWindowed
1072+
1073+ // expectedArrays indexed by arraySize,windowSize
1074+ let expectedArrays = Array2D.zeroCreate6 6
1075+ expectedArrays.[ 1 , 1 ] <- [| [| 1 |] |]
1076+ expectedArrays.[ 2 , 1 ] <- [| [| 1 |]; [| 2 |] |]
1077+ expectedArrays.[ 2 , 2 ] <- [| [| 1 ; 2 |] |]
1078+ expectedArrays.[ 3 , 1 ] <- [| [| 1 |]; [| 2 |]; [| 3 |] |]
1079+ expectedArrays.[ 3 , 2 ] <- [| [| 1 ; 2 |]; [| 2 ; 3 |] |]
1080+ expectedArrays.[ 3 , 3 ] <- [| [| 1 ; 2 ; 3 |] |]
1081+ expectedArrays.[ 4 , 1 ] <- [| [| 1 |]; [| 2 |]; [| 3 |]; [| 4 |] |]
1082+ expectedArrays.[ 4 , 2 ] <- [| [| 1 ; 2 |]; [| 2 ; 3 |]; [| 3 ; 4 |] |]
1083+ expectedArrays.[ 4 , 3 ] <- [| [| 1 ; 2 ; 3 |]; [| 2 ; 3 ; 4 |] |]
1084+ expectedArrays.[ 4 , 4 ] <- [| [| 1 ; 2 ; 3 ; 4 |] |]
1085+ expectedArrays.[ 5 , 1 ] <- [| [| 1 |]; [| 2 |]; [| 3 |]; [| 4 |]; [| 5 |] |]
1086+ expectedArrays.[ 5 , 2 ] <- [| [| 1 ; 2 |]; [| 2 ; 3 |]; [| 3 ; 4 |]; [| 4 ; 5 |] |]
1087+ expectedArrays.[ 5 , 3 ] <- [| [| 1 ; 2 ; 3 |]; [| 2 ; 3 ; 4 |]; [| 3 ; 4 ; 5 |] |]
1088+ expectedArrays.[ 5 , 4 ] <- [| [| 1 ; 2 ; 3 ; 4 |]; [| 2 ; 3 ; 4 ; 5 |] |]
1089+ expectedArrays.[ 5 , 5 ] <- [| [| 1 ; 2 ; 3 ; 4 ; 5 |] |]
1090+
1091+ for arraySize= 0 to 5 do
1092+ for windowSize= - 1 to 5 do
1093+ if windowSize<= 0 then
1094+ CheckThrowsArgumentException( fun () -> Array.windowed windowSize[| 1 .. arraySize|] |> ignore)
1095+ elif arraySize< windowSizethen
1096+ Assert.AreEqual([| |], Array.windowed windowSize[| 1 .. arraySize|])
1097+ else
1098+ Assert.AreEqual( expectedArrays.[ arraySize, windowSize], Array.windowed windowSize[| 1 .. arraySize|])
1099+
1100+ ()
1101+
10041102[<Test>]
10051103member this.Zero_Create () =
10061104