Reputation: 143
I need to create test cases based on the results of 2 enums. For example:
public enum Test_ID
{
// for ram test
test1 = 1,
test2 ,
test3 ,
// for video test
test4 ,
test5 ,
// many more...
}
public enum Test_Result
{
NOT_FAIL = 0,
FAIL
}
public struct ResultStruct
{
public Test_ID id;
public Test_Result res;
}
And some other class result's were dependent on these 2 enums.
public class foo
{
public ResultStruct[] ResStructArr = new ResultStruct[MAX_NUM_OF_TESTS];
public void updateTestsResults()
{
getResult();
for (int i = 0; i <= MAX_NUM_OF_TESTS; i++)
{
if(ResStructArr[i].id == 1 && ResStructArr[i].res == FAIL ||
ResStructArr[i].id == 2 && ResStructArr[i].res == FAIL ||
ResStructArr[i].id == 3 && ResStructArr[i].res == FAIL )
{
ramtest.result = FAIL;
}
else
{
ramtest.result = NOT_FAIL;
}
// Update other tests results here
}
}
public void getResult()
{
// get Test_ID and Test_Result and put it in struct array
}
// Perform tests..(Ram Test, Video tests, etc)
}
However for my test cases, I have to test all combinations of the 2 enums. Like:
For Ram: test case 1:
_testId = 1, _testRes = NOT_FAIL
_testId = 2, _testRes = NOT_FAIL
_testId = 3, _testRes = NOT_FAIL
test case 2:
_testId = 1, _testRes = NOT_FAIL
_testId = 2, _testRes = FAIL
_testId = 3, _testRes = NOT_FAIL
test case 3:
_testId = 1, _testRes = NOT_FAIL
_testId = 2, _testRes = FAIL
_testId = 3, _testRes = FAIL
test case 4:
_testId = 1, _testRes = FAIL
_testId = 2, _testRes = NOT_FAIL
_testId = 3, _testRes = FAIL
and so on...
For Video: test case 1:
_testId = 4, _testRes = FAIL
_testId = 5, _testRes = FAIL
test case 2:
_testId = 4, _testRes = PASS
_testId = 5, _testRes = FAIL
test case 3:
_testId = 4, _testRes = FAIL
_testId = 5, _testRes = PASS
and so on...
I read here that I can get all the permutations of the 2 enums. But not what I wanted.
Is there any way to do this or do I have to manually write the test cases one by one?
EDIT:
I have edited my question so that it would be much clearer what I want to do.
I am trying to create the test cases as what I have described as above. With the help of William Custode, I am able to get all the permutations of the enums. But I'm stuck at creating the test cases.
Upvotes: 1
Views: 237
Reputation: 3509
As I know now what you need, here's my answer for you:
public enum Test_ID
{
Type_MASK = 100,
RAM = 100,
RAM_01 = 101,
RAM_02 = 102,
RAM_03 = 103,
VIDEO = 200,
VIDEO_01 = 201,
VIDEO_02 = 202,
}
public enum Test_Result
{
nothing = 0,
OK = 1,
FAIL = 99,
}
First, you should not use 0 as one of the result values, as 0 is the default value for int variables. You might forget to assign a value and get an invalid test result. Therefore don't use 0. Has helped me a lot to prevent mistakes.
Secondly, by using qualified names you may differentiate between test cases using the name of the enum (see static function of the Enum
class, i.e. Enum.GetNames()
). The easier way is using the values of the enum and separate the values into groups using division and modulo:
(This is C++/CLI, copied from my own code and variables renamed. You can easily convert it to C#)
public ref class CTestCase abstract
{
public:
static Test_ID Type (Test_ID i_enTest)
{
return i_enTest- static_cast<Test>(Number(i_enTest));
}
static Test_ID Type (int i_iTest)
{
return static_cast<Test_ID>(i_iTest- Number(i_iTest));
}
static int Number (Test_ID i_enTest)
{
int iNumber = static_cast<int>(i_enTest) % static_cast<int>(Test_ID::Type_MASK);
return iNumber;
}
static int Number (int i_iTest)
{
int iNumber = i_iTest% static_cast<int>(Test_ID::Type_MASK);
return iNumber;
}
};
This is not the final solution, but I think you will get the rest. ;-)
Upvotes: 0
Reputation: 4594
A simple nested loop statement will produce every variation of the two sets of values:
var tests = Enum.GetValues(typeof(Test_ID)).Cast<Test_ID>();
var results = Enum.GetValues(typeof(Test_Result)).Cast<Test_Result>();
var pairs = new List<Pair>();
foreach(var test in tests)
{
foreach(var result in results)
{
pairs.Add(new Pair
{
Test = test,
Result = result
});
}
}
What you want to do with that information isn't clear from your question, so the rest of this is my inference and suggestion.
It looks like your updateRamTest
method just checks if any test failed, then the ramTest.result
is set to false
. So why not just omit the check on _testId
and just say ramTest.result = _testRes
?
Upvotes: 1