44#include < processthreadsapi.h>
55#include < winnt.h>
66#include < Winbase.h>
7+ #include < filesystem>
78#include < fstream>
89#include < algorithm>
910#include < vector>
@@ -35,6 +36,7 @@ namespace WinMLRunnerTest
3536 static const std::wstring EXE_PATH = CURRENT_PATH + L" WinMLRunner.exe" ;
3637 static const std::wstring INPUT_FOLDER_PATH = CURRENT_PATH + L" test_folder_input" ;
3738 static const std::wstring OUTPUT_PATH = CURRENT_PATH + L" test_output.csv" ;
39+ static const std::wstring TENSOR_DATA_PATH = CURRENT_PATH + L" \\ TestResult" ;
3840
3941 static std::wstring BuildCommand (std::initializer_list<std::wstring>&& arguments)
4042 {
@@ -71,50 +73,48 @@ namespace WinMLRunnerTest
7173 }
7274 }
7375
74- bool CompareTensors (std::wstring trueTensor)
75- {
76- auto time = std::time (nullptr );
77- struct tm localTime;
78- localtime_s (&localTime, &time);
79- std::ostringstream oss;
80- oss << std::put_time (&localTime, " %Y-%m-%d_%H.%M.%S" );
81-
82- std::wstring_convert<std::codecvt_utf8_utf16<wchar_t >> converter;
83- std::string folderName = " PerIterationRun[" + oss.str () + " ]\\ softmaxout1GpuIteration1.csv" ;
84- std::wstring csvTensor = converter.from_bytes (folderName);
85- bool check = true ;
86-
87- if (trueTensor.length () > 0 && csvTensor.length () > 0 )
88- {
89- std::ifstream trueFin;
90- std::ifstream fin;
91- trueFin.open (trueTensor, std::ios_base::app);
92- fin.open (csvTensor);
93-
94- std::string value;
95- std::string trueValue;
96- int i = 0 ;
97- while (trueFin.good () && fin.good ())
98- {
99- std::getline (trueFin, trueValue, ' ,' );
100- std::getline (trueFin, trueValue, ' \n ' );
101- std::getline (fin, value, ' ,' );
102- std::getline (fin, value, ' \n ' );
103- i++;
104- if (i == 1 || i == 1002 ) continue ;
105- if (abs (std::stof (trueValue) - std::stof (value)) > 0.01 )
106- {
107- check = false ;
108- }
109- if (!check) break ;
110- }
111- }
112- return check;
113- }
114-
115- TEST_CLASS (GarbageInputTest)
116- {
117- public:
76+ bool CompareTensors (const std::wstring &trueTensor, const std::wstring &outputTensor)
77+ {
78+ std::wstring csvTensor = outputTensor;
79+ bool check = true ;
80+
81+ if (trueTensor.length () > 0 && csvTensor.length () > 0 )
82+ {
83+ std::ifstream trueFin;
84+ std::ifstream fin;
85+ trueFin.open (trueTensor, std::ios_base::app);
86+ fin.open (csvTensor);
87+
88+ std::string value;
89+ std::string trueValue;
90+ int i = 0 ;
91+
92+ if (trueFin.fail () || fin.fail ())
93+ {
94+ return false ;
95+ }
96+
97+ while (!(trueFin.eof () || fin.eof ()))
98+ {
99+ std::getline (trueFin, trueValue, ' ,' );
100+ std::getline (trueFin, trueValue, ' \n ' );
101+ std::getline (fin, value, ' ,' );
102+ std::getline (fin, value, ' \n ' );
103+ i++;
104+ if (i == 1 || i == 1002 ) continue ;
105+ if (abs (std::stof (trueValue) - std::stof (value)) > 0.01 )
106+ {
107+ check = false ;
108+ break ;
109+ }
110+ }
111+ }
112+ return check;
113+ }
114+
115+ TEST_CLASS (GarbageInputTest)
116+ {
117+ public:
118118 TEST_CLASS_INITIALIZE (SetupClass)
119119 {
120120 // Make test_folder_input folder before starting the tests
@@ -144,18 +144,23 @@ namespace WinMLRunnerTest
144144 TEST_METHOD_CLEANUP (CleanupMethod)
145145 {
146146 // Remove output.csv after each test
147+ // TODO: this will not work if each test runs in parallel. Use different file path for each test, and clean up at class setup
147148 std::remove (std::string (OUTPUT_PATH.begin (), OUTPUT_PATH.end ()).c_str ());
149+ try {
150+ std::filesystem::remove_all (std::string (TENSOR_DATA_PATH.begin (), TENSOR_DATA_PATH.end ()).c_str ());
151+ }
152+ catch (const std::filesystem::filesystem_error &) {}
148153 }
149154
150- TEST_METHOD (GarbageInputCpuAndGpu)
151- {
155+ TEST_METHOD (GarbageInputCpuAndGpu)
156+ {
152157 const std::wstring modelPath = CURRENT_PATH + L" SqueezeNet.onnx" ;
153158 const std::wstring command = BuildCommand ({ EXE_PATH, L" -model" , modelPath, L" -PerfOutput" , OUTPUT_PATH, L" -perf" });
154159 Assert::AreEqual (S_OK, RunProc ((wchar_t *)command.c_str ()));
155160
156161 // We need to expect one more line because of the header
157162 Assert::AreEqual (static_cast <size_t >(3 ), GetOutputCSVLineCount ());
158- }
163+ }
159164
160165 TEST_METHOD (GarbageInputOnlyCpu)
161166 {
@@ -476,14 +481,14 @@ namespace WinMLRunnerTest
476481 // We need to expect one more line because of the header
477482 Assert::AreEqual (static_cast <size_t >(49 ), GetOutputCSVLineCount ());
478483 }
479- TEST_METHOD (GarbageInputOnlyGpuSaveTensor)
480- {
481- const std::wstring modelPath = CURRENT_PATH + L" SqueezeNet.onnx" ;
482- const std::wstring command = BuildCommand ({ EXE_PATH, L" -model" , modelPath, L" -output " , OUTPUT_PATH,L" -SaveTensorData" , L" First" , L" -GPU" , L" -RGB" });
483- Assert::AreEqual (S_OK, RunProc ((wchar_t *)command.c_str ()));
484- Assert::AreEqual (true , CompareTensors (L" softmaxout1GpuIteration1Garbage.csv" ));
485- }
486- };
484+ TEST_METHOD (GarbageInputOnlyGpuSaveTensor)
485+ {
486+ const std::wstring modelPath = CURRENT_PATH + L" SqueezeNet.onnx" ;
487+ const std::wstring command = BuildCommand ({ EXE_PATH, L" -model" , modelPath, L" -PerfOutput " , OUTPUT_PATH, L" -SaveTensorData" , L" First" , TENSOR_DATA_PATH , L" -GPU" , L" -RGB" });
488+ Assert::AreEqual (S_OK, RunProc ((wchar_t *)command.c_str ()));
489+ Assert::AreEqual (true , CompareTensors (L" softmaxout1GpuIteration1Garbage.csv " , TENSOR_DATA_PATH + L" \\ softmaxout1GpuIteration1 .csv" ));
490+ }
491+ };
487492
488493 TEST_CLASS (ImageInputTest)
489494 {
@@ -519,22 +524,25 @@ namespace WinMLRunnerTest
519524 const std::wstring command = BuildCommand ({ EXE_PATH, L" -model " , modelPath, L" -input" , inputPath, L" -autoScale" , L" Cubic" });
520525 Assert::AreEqual (S_OK, RunProc ((wchar_t *)command.c_str ()));
521526 }
522- TEST_METHOD (ProvidedImageInputOnlyGpuSaveTensor)
523- {
524- const std::wstring modelPath = CURRENT_PATH + L" SqueezeNet.onnx" ;
525- const std::wstring inputPath = CURRENT_PATH + L" fish.png" ;
526- const std::wstring command = BuildCommand ({ EXE_PATH, L" -model " , modelPath, L" -input" , inputPath, L" -SaveTensorData" , L" First" , L" -GPU" });
527- Assert::AreEqual (S_OK, RunProc ((wchar_t *)command.c_str ()));
528- Assert::AreEqual (true , CompareTensors (L" softmaxout1GpuIteration1.csv" ));
529- }
530- TEST_METHOD (ProvidedImageInputOnlyCpuSaveTensor)
531- {
532- const std::wstring modelPath = CURRENT_PATH + L" SqueezeNet.onnx" ;
533- const std::wstring inputPath = CURRENT_PATH + L" fish.png" ;
534- const std::wstring command = BuildCommand ({ EXE_PATH, L" -model " , modelPath, L" -input" , inputPath, L" -SaveTensorData" , L" First" , L" -CPU" });
535- Assert::AreEqual (S_OK, RunProc ((wchar_t *)command.c_str ()));
536- Assert::AreEqual (true , CompareTensors (L" softmaxout1CpuIteration1.csv" ));
537- }
527+ TEST_METHOD (ProvidedImageInputOnlyGpuSaveTensor)
528+ {
529+ const std::wstring modelPath = CURRENT_PATH + L" SqueezeNet.onnx" ;
530+ const std::wstring inputPath = CURRENT_PATH + L" fish.png" ;
531+ const std::wstring command = BuildCommand ({ EXE_PATH, L" -model " , modelPath, L" -input" , inputPath,
532+ L" -SaveTensorData" , L" First" , TENSOR_DATA_PATH, L" -GPU" });
533+ Assert::AreEqual (S_OK, RunProc ((wchar_t *)command.c_str ()));
534+ Assert::AreEqual (true , CompareTensors (L" softmaxout1GpuIteration1.csv" , TENSOR_DATA_PATH + L" \\ softmaxout1GpuIteration1.csv" ));
535+ }
536+ TEST_METHOD (ProvidedImageInputOnlyCpuSaveTensor)
537+ {
538+ const std::wstring modelPath = CURRENT_PATH + L" SqueezeNet.onnx" ;
539+ const std::wstring inputPath = CURRENT_PATH + L" fish.png" ;
540+ const std::wstring tensorDataPath = CURRENT_PATH + L" TestResult\\ ProvidedImageInputOnlyCpuSaveTensor.csv" ;
541+ const std::wstring command = BuildCommand ({ EXE_PATH, L" -model " , modelPath, L" -input" , inputPath,
542+ L" -SaveTensorData" , L" First" , TENSOR_DATA_PATH, L" -CPU" });
543+ Assert::AreEqual (S_OK, RunProc ((wchar_t *)command.c_str ()));
544+ Assert::AreEqual (true , CompareTensors (L" softmaxout1CpuIteration1.csv" , TENSOR_DATA_PATH + L" \\ softmaxout1CpuIteration1.csv" ));
545+ }
538546 };
539547
540548 TEST_CLASS (CsvInputTest)
@@ -555,14 +563,15 @@ namespace WinMLRunnerTest
555563 const std::wstring command = BuildCommand ({ EXE_PATH, L" -model" , modelPath, L" -input" , inputPath });
556564 Assert::AreEqual (HRESULT_FROM_WIN32 (ERROR_INVALID_PARAMETER), RunProc ((wchar_t *)command.c_str ()));
557565 }
558- TEST_METHOD (ProvidedCSVInputSaveTensor)
559- {
560- const std::wstring modelPath = CURRENT_PATH + L" SqueezeNet.onnx" ;
561- const std::wstring inputPath = CURRENT_PATH + L" kitten_224.csv" ;
562- const std::wstring command = BuildCommand ({ EXE_PATH, L" -model" , modelPath, L" -input" , inputPath, L" -SaveTensorData" , L" First" , L" -GPU" });
563- Assert::AreEqual (S_OK, RunProc ((wchar_t *)command.c_str ()));
564- Assert::AreEqual (true , CompareTensors (L" softmaxout1GpuIteration1Csv.csv" ));
565- }
566+ TEST_METHOD (ProvidedCSVInputSaveTensor)
567+ {
568+ const std::wstring modelPath = CURRENT_PATH + L" SqueezeNet.onnx" ;
569+ const std::wstring inputPath = CURRENT_PATH + L" fish.csv" ;
570+ const std::wstring command = BuildCommand ({ EXE_PATH, L" -model" , modelPath, L" -input" , inputPath, L" -SaveTensorData" ,
571+ L" First" , TENSOR_DATA_PATH, L" -GPU" });
572+ Assert::AreEqual (S_OK, RunProc ((wchar_t *)command.c_str ()));
573+ Assert::AreEqual (true , CompareTensors (L" softmaxout1GpuIteration1.csv" , TENSOR_DATA_PATH + L" \\ softmaxout1GpuIteration1.csv" ));
574+ }
566575 };
567576
568577 TEST_CLASS (OtherTests)
0 commit comments