来自 cmd 的 D 程序中的单元测试错误

Unit testing error in a D program from cmd

我有一个名为 test.d 的文本文件,其中包含以下代码片段:

import std.array;

bool binarySearch(T)(T[] input, T value)
{
    while(!input.empty)
    {
        auto i = input.length /2;
        auto mod = input[i];
        if(mid > value) input = input[0 .. i];
        else if (mid < value) input = input[i+1 .. $];
        else return true;
    }
    return false;
}

unittest 
{
    assert(binarySearch([1, 3, 6, 7, 9, 15], 6) == true);
    assert(binarySearch([1, 3, 6, 7, 9, 15], 5) == false);
} 

调用rdmd后,如下:

C:\D\dmd2\windows\bin>rdmd.exe F:\Test\test.d

一个window会打开说:

An app on your PC needs the following feature:

NTVDM

而且,会抛出一个奇怪的错误:

std.process.ProcessException@std\process.d(568): Failed to spawn new process (%1 is a 16-bit application. You do not have permissions to execute 16-bit applications. Check your permissions with your system administrator.)
----------------
0x0043D878
0x0042E14C
0x004042AC
0x00404333
0x00437457
0x00437358
0x0042A064
0x74AADEA4 in BaseThreadInitThunk
0x7700055E in RtlInitializeCriticalSectionAndSpinCount
0x7700052D in RtlInitializeCriticalSectionAndSpinCount

所以,我刚刚安装了那个 NVDTM,但是当我 运行 代码片段时,弹出窗口只是断言 NVDTM 已停止工作...

怎么了?...

您的文件没有主要功能,因此 OS 不知道它应该对可执行文件做什么。 Rdmd 可以为您添加存根。此外,默认情况下它不会 运行 单元测试。

使用此命令 运行 它:

rdmd -unittest --force --main test.d

-unittest 启用单元测试

--force 强制重新编译,所以它不使用旧的可执行文件

--main 添加存根 main