parent_process.cpp 2.64 KB
Newer Older
Tijana Vukovic's avatar
Tijana Vukovic committed
1
2
3
4
5
6
7
8
9
#include <Windows.h>
#include <iostream>
#include <string>

#include "test.hpp"
#include <migraphx/msgpack.hpp>
#include <migraphx/process.hpp>
#include <migraphx/filesystem.hpp>
#include <migraphx/errors.hpp>
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <tchar.h>

#define BUFSIZE MAX_PATH
    
std::string get_cwd()
{
    char Buffer[BUFSIZE];
    DWORD dwRet;
    dwRet = GetCurrentDirectory(BUFSIZE, Buffer);
    if(dwRet == 0)
        MIGRAPHX_THROW("GetCurrentDirectory failed (" + std::to_string(GetLastError()) + ")");
    return std::string(Buffer);
}
Tijana Vukovic's avatar
Tijana Vukovic committed
23
24
25

TEST_CASE(string_data)
{
26
27
    std::string cwd = get_cwd();
    auto child_path = migraphx::fs::path{cwd};
Tijana Vukovic's avatar
Tijana Vukovic committed
28

29
    std::string string_data = "Parent string";
Tijana Vukovic's avatar
Tijana Vukovic committed
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

    // write string data to child process
    migraphx::process{"test_child.exe"}.cwd(child_path).write([&](auto writer) {
        migraphx::to_msgpack(string_data, writer);
    });

    //// parent process read from child stdout
    // std::vector<char> result;
    // HANDLE std_in = GetStdHandle(STD_INPUT_HANDLE);
    // if(std_in == INVALID_HANDLE_VALUE)
    //     MIGRAPHX_THROW("STDIN invalid handle (" + std::to_string(GetLastError()) + ")");
    // constexpr std::size_t BUFFER_SIZE = 4096;
    // DWORD bytes_read;
    // TCHAR buffer[BUFFER_SIZE];
    // for(;;)
45
    // {
Tijana Vukovic's avatar
Tijana Vukovic committed
46
47
48
49
50
    //     BOOL status = ReadFile(std_in, buffer, BUFFER_SIZE, &bytes_read, nullptr);
    //     if(status == FALSE or bytes_read == 0)
    //         break;

    //    result.insert(result.end(), buffer, buffer + bytes_read);
51
    // }
Tijana Vukovic's avatar
Tijana Vukovic committed
52
53
54
55
56
57
58
59
60

    // EXPECT(result.data() == string_data);
}

TEST_CASE(binary_data)
{

    // binary data
    std::vector<char> binary_data = {'B', 'i', 'n', 'a', 'r', 'y'};
61
62
    std::string cwd               = get_cwd();
    auto child_path               = migraphx::fs::path{cwd};
Tijana Vukovic's avatar
Tijana Vukovic committed
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89

    // write string data to child process
    migraphx::process{"test_child.exe"}.cwd(child_path).write([&](auto writer) {
        migraphx::to_msgpack(binary_data, writer);
    });

    //// parent process read from child stdout
    // std::vector<char> result;
    // HANDLE std_in = GetStdHandle(STD_INPUT_HANDLE);
    // if(std_in == INVALID_HANDLE_VALUE)
    //     MIGRAPHX_THROW("STDIN invalid handle (" + std::to_string(GetLastError()) + ")");
    // constexpr std::size_t BUFFER_SIZE = 4096;
    // DWORD bytes_read;
    // TCHAR buffer[BUFFER_SIZE];
    // for(;;)
    //{
    //     BOOL status = ReadFile(std_in, buffer, BUFFER_SIZE, &bytes_read, nullptr);
    //     if(status == FALSE or bytes_read == 0)
    //         break;

    //    result.insert(result.end(), buffer, buffer + bytes_read);
    //}

    // EXPECT(result.data() == string_data);
}

int main(int argc, const char* argv[]) { test::run(argc, argv); }