Reputation: 83
I have this Zip method:
public static List<List<T>> Zip<T>(this List<List<T>> s)
{
if (s is null || s.Count == 0)
{
throw new ArgumentNullException(nameof(s));
}
foreach (var x in s)
if (x == null)
throw new ArgumentNullException();
var count = s[0].Count;
if (!s.All(x => x.Count == count))
throw new ArgumentException("Not all sub lists have the same count.");
var output = new List<List<T>>();
for (int i = 0; i < count; i++) //guardo la prima lista in s
{
var current_merged_list = new List<T>();
foreach (var sub_list in s) //per ogni lista in s
{
current_merged_list.Add(sub_list[i]);
}
output.Add(current_merged_list);
}
return output;
}
I wrote this test:
[TestCase(6)]
public void Test3(int approx)
{
var abc = new List<string>() {"a", "b", "c"};
var bca = new List<string>() { "b", "c", "a" };
var cab = new List<string>() { "c", "a", "b" };
var abcInf = AbcInf(abc); //for some reason abcInf is null
var bcaInf = BcaInf(bca); //I guess this is null too
var cabInf = CabInf(cab); //I guess this is null too
var dataList = new List<List<string>>();
dataList.Add((abcInf as List<string>));
dataList.Add((bcaInf as List<string>));
dataList.Add((cabInf as List<string>));
var actual = Check_Res_Approx(dataList.Zip(),approx);
var expected = ReturnResult(approx);
Assert.AreEqual(actual,expected);
}
public IEnumerable<string> AbcInf(List<string> abc)
{
int i = 0;
while (true)
{
var res = i;
abc.Add("a");
abc.Add("b");
abc.Add("c");
i++;
yield return abc[res];
}
}
public IEnumerable<string> BcaInf(List<string> bca)
{
int i = 0;
while (true)
{
var res = i;
bca.Add("b");
bca.Add("c");
bca.Add("a");
i++;
yield return bca[res];
}
}
public IEnumerable<string> CabInf(List<string> cab)
{
int i = 0;
while (true)
{
var res = i;
cab.Add("c");
cab.Add("a");
cab.Add("b");
i++;
yield return cab[res];
}
}
public IEnumerable<IEnumerable<string>> Check_Res_Approx(List<List<string>> dataList, int approx)
{
int i = 0;
while (i<approx)
{
yield return dataList[i];
}
}
public IEnumerable<IEnumerable<string>> ReturnResult(int approx)
{
var abc = new List<string>()
{
"a",
"b",
"c"
};
var bca = new List<string>()
{
"b",
"c",
"a"
};
var cab = new List<string>()
{
"c",
"a",
"b"
};
var actual = new List<List<string>>();
int i = 0;
while (true)
{
actual.Add(abc);
actual.Add(bca);
actual.Add(cab);
if (i < approx)
yield return actual[i];
yield break;
}
}
I know it's messy but my question is not about the test itself. My question is why does it say that the List abcInf is null? I filled it using method AbcInf(abc) so I thought it should contain an infinite sequence of [ a , b ,c , a , b , c , a , b , c , ...] but apparently it doesn't.
Upvotes: 0
Views: 67
Reputation: 17038
abcInf
isn't null
and it isn't a List
. It's an IEnumerable<string>
. The problem is that you're trying to convert it to a List<string>
, which is a different type altogether. Since this conversion isn't possible, the result of abcInf as List<string>
is null
, as described in the C# documentation.
If you want to construct a List<T>
from an IEnumerable<T>
, you have to call a constructor, like this: new List<string>(abcInf)
. However, since abcInf
is infinite in this case, the constructor would never return.
Upvotes: 1