基于 2 个枚举的多重排列

Multiple permuations based on 2 Enums

我需要根据 2 个枚举的结果创建测试用例。 例如:

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;
}

其他一些 class 结果依赖于这两个枚举。

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)
}

但是对于我的测试用例,我必须测试 2 个枚举的所有组合。 喜欢:

拉姆: 测试用例 1:

_testId = 1, _testRes = NOT_FAIL
_testId = 2, _testRes = NOT_FAIL
_testId = 3, _testRes = NOT_FAIL

测试用例 2:

_testId = 1, _testRes = NOT_FAIL
_testId = 2, _testRes = FAIL
_testId = 3, _testRes = NOT_FAIL

测试用例 3:

_testId = 1, _testRes = NOT_FAIL
_testId = 2, _testRes = FAIL
_testId = 3, _testRes = FAIL

测试用例 4:

_testId = 1, _testRes = FAIL
_testId = 2, _testRes = NOT_FAIL
_testId = 3, _testRes = FAIL
and so on...

对于视频: 测试用例 1:

_testId = 4, _testRes = FAIL
_testId = 5, _testRes = FAIL

测试用例 2:

_testId = 4, _testRes = PASS
_testId = 5, _testRes = FAIL

测试用例 3:

_testId = 4, _testRes = FAIL
_testId = 5, _testRes = PASS
and so on...

我读到 我可以获得 2 个枚举的所有排列。但不是我想要的。

有什么方法可以做到这一点还是我必须手动一个一个地编写测试用例?

编辑:

我已经编辑了我的问题,这样我想做什么就更清楚了。

我正在尝试创建我上面描述的测试用例。在 William Custode 的帮助下,我能够获得枚举的所有排列。但我坚持创建测试用例。

一个简单的嵌套循环语句将产生两组值的每个变化:

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
            });
    }
}

你的问题并不清楚你想用这些信息做什么,所以剩下的就是我的推断和建议。

看起来您的 updateRamTest 方法只是检查是否有任何测试失败,然后 ramTest.result 设置为 false。那么,为什么不省略 _testId 的检查而只说 ramTest.result = _testRes

我现在知道你需要什么,这是我给你的答案:

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,
}

首先,您不应使用 0 作为结果值之一,因为 0 是 int 变量的默认值。您可能会忘记分配一个值并得到无效的测试结果。因此不要使用 0。对我防止错误有很大帮助。

其次,通过使用限定名称,您可以使用枚举名称区分测试用例(参见 Enum class 的静态函数,即 Enum.GetNames())。更简单的方法是使用枚举的值并使用除法和模数将值分成组:

(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;
  }
};

这不是最终的解决方案,但我想您会得到其余的。 ;-)