Files
rocm-systems/hipamd/tests/src/printf/printf_common.h
T
Sameer Sahasrabuddhe 01d4117789 SWDEV-227201: Introduce tests for printf on hostcall
Tests that check POSIX specifiers with a single thread:
 - hipPrintfSpecifiers.cpp     : all conversion specifiers
 - hipPrintfFlags.cpp          : common flags that modify conversions
 - hipPrintfAltForms.cpp       : alternate forms ('#')
 - hipPrintfStar.cpp           : additional arguments ('*')
 - hipPrintfWidthPrecision.cpp : floating point details

Tests that check functionality on top of hostcall
 - hipPrintfBasic.cpp       : divergent calls, series of calls, return value, etc
 - hipPrintfManyWaves.cpp   : many waves printing together
 - hipPrintfManyDevices.cpp : many waves on many devices

Change-Id: I35e069f4c542f896999239996dc89eda0faad7b8
2020-04-06 00:49:34 -04:00

95 lines
2.1 KiB
C++

#ifndef COMMON_H
#define COMMON_H
#include <errno.h>
#include <error.h>
#include <fstream>
#include <iostream>
#include <map>
#include <stdlib.h>
#include <string>
#include <unistd.h>
struct CaptureStream {
int saved_fd;
int orig_fd;
int temp_fd;
char tempname[13] = "mytestXXXXXX";
CaptureStream(FILE *original) {
orig_fd = fileno(original);
saved_fd = dup(orig_fd);
temp_fd = mkstemp(tempname);
if (errno) {
error(0, errno, "Error");
assert(false);
}
fflush(nullptr);
dup2(temp_fd, orig_fd);
if (errno) {
error(0, errno, "Error");
assert(false);
}
close(temp_fd);
if (errno) {
error(0, errno, "Error");
assert(false);
}
}
void restoreStream() {
if (saved_fd == -1)
return;
fflush(nullptr);
dup2(saved_fd, orig_fd);
if (errno) {
error(0, errno, "Error");
assert(false);
}
close(saved_fd);
if (errno) {
error(0, errno, "Error");
assert(false);
}
saved_fd = -1;
}
std::ifstream getCapturedData() {
restoreStream();
std::ifstream temp(tempname);
return temp;
}
~CaptureStream() {
restoreStream();
remove(tempname);
if (errno) {
error(0, errno, "Error");
assert(false);
}
}
};
static std::string gulp(std::ifstream &input) {
std::string retval;
input.seekg(0, std::ios_base::end);
retval.resize(input.tellg());
input.seekg(0, std::ios_base::beg);
input.read(&retval[0], retval.size());
input.close();
return retval;
}
#define DECLARE_DATA() \
const char *msg_short = "Carpe diem."; \
const char *msg_long1 = "Lorem ipsum dolor sit amet, consectetur nullam. " \
"In mollis imperdiet nibh nec ullamcorper."; \
const char *msg_long2 = "Curabitur nec metus sit amet augue vehicula " \
"ultrices ut id leo. Lorem ipsum dolor sit amet, " \
"consectetur adipiscing elit amet.";
#endif