1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035
|
/*
* Copyright (C) 2018-2025 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/
#include "shared/source/helpers/aligned_memory.h"
#include "shared/source/helpers/string.h"
#include "shared/source/program/print_formatter.h"
#include "shared/test/common/helpers/stream_capture.h"
#include "shared/test/common/mocks/mock_graphics_allocation.h"
#include "shared/test/common/mocks/mock_kernel_info.h"
#include "gtest/gtest.h"
#include <cmath>
using namespace NEO;
using namespace iOpenCL;
// -------------------- Base Fixture ------------------------
class PrintFormatterTest : public testing::Test {
public:
std::unique_ptr<PrintFormatter> printFormatter;
static const size_t maxPrintfOutputLength = 4096;
static const size_t printfBufferSize = 1024;
std::string format;
uint8_t buffer;
MockGraphicsAllocation *data;
std::unique_ptr<MockKernelInfo> kernelInfo;
uint8_t underlyingBuffer[maxPrintfOutputLength];
uint32_t offset;
int maxStringIndex;
protected:
void SetUp() override {
offset = 4;
maxStringIndex = 0;
data = new MockGraphicsAllocation(underlyingBuffer, maxPrintfOutputLength);
kernelInfo = std::make_unique<MockKernelInfo>();
printFormatter = std::unique_ptr<PrintFormatter>(new PrintFormatter(static_cast<uint8_t *>(data->getUnderlyingBuffer()), printfBufferSize, is32bit, &kernelInfo->kernelDescriptor.kernelMetadata.printfStringsMap));
underlyingBuffer[0] = 0;
underlyingBuffer[1] = 0;
underlyingBuffer[2] = 0;
underlyingBuffer[3] = 0;
}
void TearDown() override {
delete data;
}
PrintfDataType getPrintfDataType(char value) { return PrintfDataType::byteType; };
PrintfDataType getPrintfDataType(int8_t value) { return PrintfDataType::byteType; };
PrintfDataType getPrintfDataType(uint8_t value) { return PrintfDataType::byteType; };
PrintfDataType getPrintfDataType(int16_t value) { return PrintfDataType::shortType; };
PrintfDataType getPrintfDataType(uint16_t value) { return PrintfDataType::shortType; };
PrintfDataType getPrintfDataType(int32_t value) { return PrintfDataType::intType; };
PrintfDataType getPrintfDataType(uint32_t value) { return PrintfDataType::intType; };
PrintfDataType getPrintfDataType(int64_t value) { return PrintfDataType::longType; };
PrintfDataType getPrintfDataType(uint64_t value) { return PrintfDataType::longType; };
PrintfDataType getPrintfDataType(float value) { return PrintfDataType::floatType; };
PrintfDataType getPrintfDataType(double value) { return PrintfDataType::doubleType; };
PrintfDataType getPrintfDataType(char *value) { return PrintfDataType::stringType; };
template <class T>
void injectValue(T value) {
auto dataType = getPrintfDataType(value);
storeData(dataType);
if (dataType == PrintfDataType::byteType ||
dataType == PrintfDataType::shortType) {
storeData(static_cast<int>(value));
} else {
storeData(value);
}
}
void injectStringValue(int value) {
storeData(PrintfDataType::stringType);
storeData(value);
}
template <class T>
void storeData(T value) {
T *valuePointer = reinterpret_cast<T *>(underlyingBuffer + offset);
if (isAligned(valuePointer)) {
*valuePointer = value;
} else {
memcpy_s(valuePointer, sizeof(underlyingBuffer) - offset, &value, sizeof(T));
}
offset += sizeof(T);
// first four bytes always store the size
uint32_t *pointer = reinterpret_cast<uint32_t *>(underlyingBuffer);
*pointer = offset;
}
int injectFormatString(std::string str) {
auto index = maxStringIndex++;
kernelInfo->addToPrintfStringsMap(index, str);
return index;
}
};
// for tests printing a single value
template <class T>
struct SingleValueTestParam {
std::string format;
T value;
};
template <class T>
struct SingleValueTestParamWithHostFormat {
std::string format;
std::string hostFormat;
T value;
};
typedef SingleValueTestParam<int8_t> Int8Params;
typedef SingleValueTestParam<uint8_t> Uint8Params;
typedef SingleValueTestParam<int16_t> Int16Params;
typedef SingleValueTestParam<uint16_t> Uint16Params;
typedef SingleValueTestParam<int32_t> Int32Params;
typedef SingleValueTestParam<uint32_t> Uint32Params;
typedef SingleValueTestParamWithHostFormat<int64_t> Int64Params;
typedef SingleValueTestParamWithHostFormat<uint64_t> Uint64Params;
typedef SingleValueTestParam<float> FloatParams;
typedef SingleValueTestParam<double> DoubleParams;
typedef SingleValueTestParam<std::string> StringParams;
Int8Params byteValues[] = {
{"%c", 'a'},
};
class PrintfInt8Test : public PrintFormatterTest,
public ::testing::WithParamInterface<Int8Params> {};
TEST_P(PrintfInt8Test, GivenFormatContainingIntWhenPrintingThenValueIsInserted) {
auto input = GetParam();
auto stringIndex = injectFormatString(input.format);
storeData(stringIndex);
injectValue(input.value);
char referenceOutput[maxPrintfOutputLength];
char actualOutput[maxPrintfOutputLength];
printFormatter->printKernelOutput([&actualOutput](char *str) { strncpy_s(actualOutput, maxPrintfOutputLength, str, maxPrintfOutputLength - 1); });
snprintf(referenceOutput, sizeof(referenceOutput), input.format.c_str(), input.value);
EXPECT_STREQ(referenceOutput, actualOutput);
}
INSTANTIATE_TEST_SUITE_P(PrintfInt8Test,
PrintfInt8Test,
::testing::ValuesIn(byteValues));
Int32Params intValues[] = {
{"%d", 0},
{"%d", 1},
{"%d", -1},
{"%d", INT32_MAX},
{"%d", INT32_MIN},
{"%5d", 10},
{"%-5d", 10},
{"%05d", 10},
{"%+5d", 10},
{"%-+5d", 10},
{"%.5i", 100},
{"%6.5i", 100},
{"%-06i", 100},
{"%06.5i", 100}};
class PrintfInt32Test : public PrintFormatterTest,
public ::testing::WithParamInterface<Int32Params> {};
TEST_P(PrintfInt32Test, GivenFormatContainingIntWhenPrintingThenValueIsInserted) {
auto input = GetParam();
auto stringIndex = injectFormatString(input.format);
storeData(stringIndex);
injectValue(input.value);
char referenceOutput[maxPrintfOutputLength];
char actualOutput[maxPrintfOutputLength];
printFormatter->printKernelOutput([&actualOutput](char *str) { strncpy_s(actualOutput, maxPrintfOutputLength, str, maxPrintfOutputLength - 1); });
snprintf(referenceOutput, sizeof(referenceOutput), input.format.c_str(), input.value);
EXPECT_STREQ(referenceOutput, actualOutput);
}
INSTANTIATE_TEST_SUITE_P(PrintfInt32Test,
PrintfInt32Test,
::testing::ValuesIn(intValues));
Uint32Params uintValues[] = {
{"%u", 0},
{"%u", 1},
{"%u", UINT32_MAX},
{"%.0u", 0},
// octal
{"%o", 10},
{"%.5o", 10},
{"%#o", 100000000},
{"%04.5o", 10},
// hexadecimal
{"%#x", 0xABCDEF},
{"%#X", 0xABCDEF},
{"%#X", 0},
{"%8x", 399},
{"%04x", 399}};
class PrintfUint32Test : public PrintFormatterTest,
public ::testing::WithParamInterface<Uint32Params> {};
TEST_P(PrintfUint32Test, GivenFormatContainingUintWhenPrintingThenValueIsInserted) {
auto input = GetParam();
auto stringIndex = injectFormatString(input.format);
storeData(stringIndex);
injectValue(input.value);
char referenceOutput[maxPrintfOutputLength];
char actualOutput[maxPrintfOutputLength];
printFormatter->printKernelOutput([&actualOutput](char *str) { strncpy_s(actualOutput, maxPrintfOutputLength, str, maxPrintfOutputLength - 1); });
snprintf(referenceOutput, sizeof(referenceOutput), input.format.c_str(), input.value);
EXPECT_STREQ(referenceOutput, actualOutput);
}
TEST_P(PrintfUint32Test, GivenBufferSizeGreaterThanPrintBufferWhenPrintingThenBufferIsTrimmed) {
auto input = GetParam();
printFormatter = std::unique_ptr<PrintFormatter>(new PrintFormatter(static_cast<uint8_t *>(data->getUnderlyingBuffer()), 0, is32bit, &kernelInfo->kernelDescriptor.kernelMetadata.printfStringsMap));
auto stringIndex = injectFormatString(input.format);
storeData(stringIndex);
injectValue(input.value);
char referenceOutput[maxPrintfOutputLength] = "";
char actualOutput[maxPrintfOutputLength] = "";
printFormatter->printKernelOutput([&actualOutput](char *str) { strncpy_s(actualOutput, maxPrintfOutputLength, str, maxPrintfOutputLength - 1); });
EXPECT_STREQ(referenceOutput, actualOutput);
}
INSTANTIATE_TEST_SUITE_P(PrintfUint32Test,
PrintfUint32Test,
::testing::ValuesIn(uintValues));
Int64Params longValues[] = {
{"%lld", "%lld", INT64_MAX},
{"%ld", "%lld", INT64_MAX},
{"%ld", "%lld", INT64_MIN},
{"%lld", "%lld", INT64_MIN},
{"%llx", "%llx", INT64_MAX},
{"%lx", "%llx", INT64_MAX},
{"%lx", "%llx", INT64_MIN},
{"%llx", "%llx", INT64_MIN},
{"%d", "%d", INT32_MAX},
{"%x", "%x", INT32_MAX}};
class PrintfInt64Test : public PrintFormatterTest,
public ::testing::WithParamInterface<Int64Params> {};
TEST_P(PrintfInt64Test, GivenFormatContainingLongWhenPrintingThenValueIsInserted) {
auto input = GetParam();
auto stringIndex = injectFormatString(input.format);
storeData(stringIndex);
injectValue(input.value);
char referenceOutput[maxPrintfOutputLength];
char actualOutput[maxPrintfOutputLength];
printFormatter->printKernelOutput([&actualOutput](char *str) { strncpy_s(actualOutput, maxPrintfOutputLength, str, maxPrintfOutputLength - 1); });
snprintf(referenceOutput, sizeof(referenceOutput), input.hostFormat.c_str(), input.value);
EXPECT_STREQ(referenceOutput, actualOutput);
}
INSTANTIATE_TEST_SUITE_P(PrintfInt64Test,
PrintfInt64Test,
::testing::ValuesIn(longValues));
Uint64Params ulongValues[] = {
{"%llu", "%llu", UINT64_MAX},
{"%lu", "%llu", UINT64_MAX},
{"%llux", "%llux", UINT64_MAX},
{"%lux", "%llux", UINT64_MAX}};
class PrintfUint64Test : public PrintFormatterTest,
public ::testing::WithParamInterface<Uint64Params> {};
TEST_P(PrintfUint64Test, GivenFormatContainingUlongWhenPrintingThenValueIsInserted) {
auto input = GetParam();
auto stringIndex = injectFormatString(input.format);
storeData(stringIndex);
injectValue(input.value);
char referenceOutput[maxPrintfOutputLength];
char actualOutput[maxPrintfOutputLength];
printFormatter->printKernelOutput([&actualOutput](char *str) { strncpy_s(actualOutput, maxPrintfOutputLength, str, maxPrintfOutputLength - 1); });
snprintf(referenceOutput, sizeof(referenceOutput), input.hostFormat.c_str(), input.value);
EXPECT_STREQ(referenceOutput, actualOutput);
}
INSTANTIATE_TEST_SUITE_P(PrintfUint64Test,
PrintfUint64Test,
::testing::ValuesIn(ulongValues));
FloatParams floatValues[] = {
{"%f", 10.3456f},
{"%.1f", 10.3456f},
{"%.2f", 10.3456f},
{"%8.3f", 10.3456f},
{"%08.2f", 10.3456f},
{"%-8.2f", 10.3456f},
{"%+8.2f", -10.3456f},
{"%.0f", 0.1f},
{"%.0f", 0.6f},
{"%0f", 0.6f},
};
class PrintfFloatTest : public PrintFormatterTest,
public ::testing::WithParamInterface<FloatParams> {};
TEST_P(PrintfFloatTest, GivenFormatContainingFloatWhenPrintingThenValueIsInserted) {
auto input = GetParam();
auto stringIndex = injectFormatString(input.format);
storeData(stringIndex);
injectValue(input.value);
char referenceOutput[maxPrintfOutputLength];
char actualOutput[maxPrintfOutputLength];
printFormatter->printKernelOutput([&actualOutput](char *str) { strncpy_s(actualOutput, maxPrintfOutputLength, str, maxPrintfOutputLength - 1); });
snprintf(referenceOutput, sizeof(referenceOutput), input.format.c_str(), input.value);
EXPECT_STREQ(referenceOutput, actualOutput);
}
INSTANTIATE_TEST_SUITE_P(PrintfFloatTest,
PrintfFloatTest,
::testing::ValuesIn(floatValues));
class PrintfDoubleToFloatTest : public PrintFormatterTest,
public ::testing::WithParamInterface<DoubleParams> {};
DoubleParams doubleToFloatValues[] = {
{"%f", 10.3456},
{"%.1f", 10.3456},
{"%.2f", 10.3456},
{"%8.3f", 10.3456},
{"%08.2f", 10.3456},
{"%-8.2f", 10.3456},
{"%+8.2f", -10.3456},
{"%.0f", 0.1},
{"%0f", 0.6},
{"%4g", 12345.6789},
{"%4.2g", 12345.6789},
{"%4G", 0.0000023},
{"%4G", 0.023},
{"%-#20.15e", 19456120.0},
{"%+#21.15E", 19456120.0},
{"%.6a", 0.1},
{"%10.2a", 9990.235}};
TEST_P(PrintfDoubleToFloatTest, GivenFormatContainingFloatAndDoubleWhenPrintingThenValueIsInserted) {
auto input = GetParam();
auto stringIndex = injectFormatString(input.format);
storeData(stringIndex);
injectValue(input.value);
char referenceOutput[maxPrintfOutputLength];
char actualOutput[maxPrintfOutputLength];
printFormatter->printKernelOutput([&actualOutput](char *str) { strncpy_s(actualOutput, maxPrintfOutputLength, str, maxPrintfOutputLength - 1); });
snprintf(referenceOutput, sizeof(referenceOutput), input.format.c_str(), input.value);
EXPECT_STREQ(referenceOutput, actualOutput);
}
INSTANTIATE_TEST_SUITE_P(PrintfDoubleToFloatTest,
PrintfDoubleToFloatTest,
::testing::ValuesIn(doubleToFloatValues));
DoubleParams doubleValues[] = {
{"%f", 302230.12156260},
{"%+f", 2937289102.1562},
{"% #F", (double)-1254},
{"%6.2f", 0.1562},
{"%06.2f", -0.1562},
{"%e", 0.1562},
{"%E", -1254.0001001},
{"%+.10e", 0.1562000102241},
{"% E", (double)1254},
{"%10.2e", 100230.1562},
{"%g", 74010.00001562},
{"%G", -1254.0001001},
{"%+g", 325001.00001562},
{"%+#G", -1254.0001001},
{"%8.2g", 19.844},
{"%1.5G", -1.1},
{"%.13a", 1890.00001562},
{"%.13A", -1254.0001001},
};
class PrintfDoubleTest : public PrintFormatterTest,
public ::testing::WithParamInterface<DoubleParams> {};
TEST_P(PrintfDoubleTest, GivenFormatContainingDoubleWhenPrintingThenValueIsInserted) {
auto input = GetParam();
auto stringIndex = injectFormatString(input.format);
storeData(stringIndex);
injectValue(input.value);
char referenceOutput[maxPrintfOutputLength];
char actualOutput[maxPrintfOutputLength];
printFormatter->printKernelOutput([&actualOutput](char *str) { strncpy_s(actualOutput, maxPrintfOutputLength, str, maxPrintfOutputLength - 1); });
if (input.format[input.format.length() - 1] == 'F') {
input.format[input.format.length() - 1] = 'f';
}
snprintf(referenceOutput, sizeof(referenceOutput), input.format.c_str(), input.value);
EXPECT_STREQ(referenceOutput, actualOutput);
}
INSTANTIATE_TEST_SUITE_P(PrintfDoubleTest,
PrintfDoubleTest,
::testing::ValuesIn(doubleValues));
std::pair<std::string, std::string> specialValues[] = {
{"%%", "%"},
{"%% ", "% "},
{"nothing%", "nothing"},
};
class PrintfSpecialTest : public PrintFormatterTest,
public ::testing::WithParamInterface<std::pair<std::string, std::string>> {};
TEST_P(PrintfSpecialTest, GivenFormatContainingDoublePercentageWhenPrintingThenValueIsInsertedCorrectly) {
auto input = GetParam();
auto stringIndex = injectFormatString(input.first);
storeData(stringIndex);
char actualOutput[maxPrintfOutputLength];
printFormatter->printKernelOutput([&actualOutput](char *str) { strncpy_s(actualOutput, maxPrintfOutputLength, str, maxPrintfOutputLength - 1); });
EXPECT_STREQ(input.second.c_str(), actualOutput);
}
INSTANTIATE_TEST_SUITE_P(PrintfSpecialTest,
PrintfSpecialTest,
::testing::ValuesIn(specialValues));
// ------------------------- Testing Strings only with no Formatting ------------------------
class PrintfNoArgumentsTest : public PrintFormatterTest,
public ::testing::WithParamInterface<std::pair<std::string, std::string>> {};
// escape/non-escaped strings are specified manually to avoid converting them in code
// automatic code would have to do the same thing it is testing and therefore would be prone to mistakes
// this is needed because compiler doesn't escape the format strings and provides them exactly as they were typed in kernel source
std::pair<std::string, std::string> stringValues[] = {
{R"(test)", R"(test)"},
{R"(test\n)", R"(test\n)"},
{R"(test\\n)", R"(test\\n)"},
{R"(test\\\n)", R"(test\\\n)"},
{R"(test\t)", R"(test\t)"},
{R"(test\\t)", R"(test\\t)"},
{R"(test\\\t)", R"(test\\\t)"},
};
TEST_P(PrintfNoArgumentsTest, GivenNoArgumentsWhenPrintingThenCharsAreEscaped) {
auto input = GetParam();
auto stringIndex = injectFormatString(input.first);
storeData(stringIndex);
char actualOutput[maxPrintfOutputLength];
printFormatter->printKernelOutput([&actualOutput](char *str) { strncpy_s(actualOutput, maxPrintfOutputLength, str, maxPrintfOutputLength - 1); });
EXPECT_STREQ(input.second.c_str(), actualOutput);
}
INSTANTIATE_TEST_SUITE_P(PrintfNoArgumentsTest,
PrintfNoArgumentsTest,
::testing::ValuesIn(stringValues));
StringParams stringValues2[] = {
{"%s", "foo"},
};
class PrintfStringTest : public PrintFormatterTest,
public ::testing::WithParamInterface<StringParams> {};
TEST_P(PrintfStringTest, GivenFormatContainingStringWhenPrintingThenValueIsInserted) {
auto input = GetParam();
auto stringIndex = injectFormatString(input.format);
storeData(stringIndex);
auto inputIndex = injectFormatString(input.value);
injectStringValue(inputIndex);
char referenceOutput[maxPrintfOutputLength];
char actualOutput[maxPrintfOutputLength];
printFormatter->printKernelOutput([&actualOutput](char *str) { strncpy_s(actualOutput, maxPrintfOutputLength, str, maxPrintfOutputLength - 1); });
snprintf(referenceOutput, sizeof(referenceOutput), input.format.c_str(), input.value.c_str());
EXPECT_STREQ(input.value.c_str(), actualOutput);
}
INSTANTIATE_TEST_SUITE_P(PrintfStringTest,
PrintfStringTest,
::testing::ValuesIn(stringValues2));
TEST_F(PrintFormatterTest, GivenLongStringValueWhenPrintedThenFullStringIsPrinted) {
char testedLongString[maxPrintfOutputLength];
memset(testedLongString, 'a', maxPrintfOutputLength - 1);
testedLongString[maxPrintfOutputLength - 1] = '\0';
auto stringIndex = injectFormatString(testedLongString);
storeData(stringIndex);
char actualOutput[maxPrintfOutputLength];
printFormatter->printKernelOutput([&actualOutput](char *str) { strncpy_s(actualOutput, maxPrintfOutputLength, str, maxPrintfOutputLength - 1); });
EXPECT_STREQ(testedLongString, actualOutput);
}
TEST_F(PrintFormatterTest, GivenStringSpecifierWhenLongStringIsPassedAsValueThenFullStringIsPrinted) {
char testedLongString[maxPrintfOutputLength];
memset(testedLongString, 'a', maxPrintfOutputLength - 5);
testedLongString[maxPrintfOutputLength - 5] = '\0';
auto stringIndex = injectFormatString("%s");
storeData(stringIndex);
auto inputIndex = injectFormatString(testedLongString);
injectStringValue(inputIndex);
char actualOutput[maxPrintfOutputLength];
printFormatter->printKernelOutput([&actualOutput](char *str) { strncpy_s(actualOutput, maxPrintfOutputLength, str, maxPrintfOutputLength - 1); });
EXPECT_STREQ(testedLongString, actualOutput);
}
TEST_F(PrintFormatterTest, GivenTooLongStringWhenPrintedThenOutputIsTruncated) {
std::unique_ptr<char[]> testedLongString(new char[PrintFormatter::maxSinglePrintStringLength + 1024]);
memset(testedLongString.get(), 'a', PrintFormatter::maxSinglePrintStringLength + 1024 - 1);
testedLongString[PrintFormatter::maxSinglePrintStringLength + 1024 - 1] = '\0';
auto stringIndex = injectFormatString(testedLongString.get());
storeData(stringIndex);
std::unique_ptr<char[]> actualOutput(new char[PrintFormatter::maxSinglePrintStringLength + 1024]);
printFormatter->printKernelOutput([&actualOutput](char *str) {
size_t length = strnlen_s(str, PrintFormatter::maxSinglePrintStringLength + 1023);
strncpy_s(actualOutput.get(), PrintFormatter::maxSinglePrintStringLength + 1024, str, length); });
auto testedLength = strnlen_s(testedLongString.get(), PrintFormatter::maxSinglePrintStringLength + 1024);
auto actualLength = strnlen_s(actualOutput.get(), PrintFormatter::maxSinglePrintStringLength + 1024);
EXPECT_GT(testedLength, actualLength);
}
TEST_F(PrintFormatterTest, GivenNullTokenWhenPrintingThenNullIsInserted) {
auto stringIndex = injectFormatString("%s");
storeData(stringIndex);
storeData(PrintfDataType::vectorIntType);
storeData(0);
char actualOutput[maxPrintfOutputLength];
printFormatter->printKernelOutput([&actualOutput](char *str) { strncpy_s(actualOutput, maxPrintfOutputLength, str, maxPrintfOutputLength - 1); });
EXPECT_STREQ("(null)", actualOutput);
}
// ----------------------- Vector channel count ---------------------------------
TEST_F(PrintFormatterTest, GivenVector2WhenPrintingThenAllValuesAreInserted) {
int channelCount = 2;
auto stringIndex = injectFormatString("%v2d");
storeData(stringIndex);
storeData(PrintfDataType::vectorIntType);
// channel count
storeData(channelCount);
// channel values
for (int i = 0; i < channelCount; i++) {
storeData(i + 1);
}
char actualOutput[maxPrintfOutputLength];
printFormatter->printKernelOutput([&actualOutput](char *str) { strncpy_s(actualOutput, maxPrintfOutputLength, str, maxPrintfOutputLength - 1); });
EXPECT_STREQ("1,2", actualOutput);
}
TEST_F(PrintFormatterTest, GivenVector4WhenPrintingThenAllValuesAreInserted) {
int channelCount = 4;
auto stringIndex = injectFormatString("%v4d");
storeData(stringIndex);
storeData(PrintfDataType::vectorIntType);
// channel count
storeData(channelCount);
// channel values
for (int i = 0; i < channelCount; i++) {
storeData(i + 1);
}
char actualOutput[maxPrintfOutputLength];
printFormatter->printKernelOutput([&actualOutput](char *str) { strncpy_s(actualOutput, maxPrintfOutputLength, str, maxPrintfOutputLength - 1); });
EXPECT_STREQ("1,2,3,4", actualOutput);
}
TEST_F(PrintFormatterTest, GivenVector8WhenPrintingThenAllValuesAreInserted) {
int channelCount = 8;
auto stringIndex = injectFormatString("%v8d");
storeData(stringIndex);
storeData(PrintfDataType::vectorIntType);
// channel count
storeData(channelCount);
// channel values
for (int i = 0; i < channelCount; i++) {
storeData(i + 1);
}
char actualOutput[maxPrintfOutputLength];
printFormatter->printKernelOutput([&actualOutput](char *str) { strncpy_s(actualOutput, maxPrintfOutputLength, str, maxPrintfOutputLength - 1); });
EXPECT_STREQ("1,2,3,4,5,6,7,8", actualOutput);
}
TEST_F(PrintFormatterTest, GivenVector16WhenPrintingThenAllValuesAreInserted) {
int channelCount = 16;
auto stringIndex = injectFormatString("%v16d");
storeData(stringIndex);
storeData(PrintfDataType::vectorIntType);
// channel count
storeData(channelCount);
// channel values
for (int i = 0; i < channelCount; i++) {
storeData(i + 1);
}
char actualOutput[maxPrintfOutputLength];
printFormatter->printKernelOutput([&actualOutput](char *str) { strncpy_s(actualOutput, maxPrintfOutputLength, str, maxPrintfOutputLength - 1); });
EXPECT_STREQ("1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16", actualOutput);
}
// ------------------- vector types ----------------------------
TEST_F(PrintFormatterTest, GivenVectorOfBytesWhenPrintingThenAllValuesAreInserted) {
int channelCount = 2;
auto stringIndex = injectFormatString("%v2hhd");
storeData(stringIndex);
storeData(PrintfDataType::vectorByteType);
// channel count
storeData(channelCount);
storeData<int8_t>(1);
storeData<int8_t>(2);
char actualOutput[maxPrintfOutputLength];
printFormatter->printKernelOutput([&actualOutput](char *str) { strncpy_s(actualOutput, maxPrintfOutputLength, str, maxPrintfOutputLength - 1); });
EXPECT_STREQ("1,2", actualOutput);
}
TEST_F(PrintFormatterTest, GivenVectorOfShortsWhenPrintingThenAllValuesAreInserted) {
int channelCount = 2;
auto stringIndex = injectFormatString("%v2hd");
storeData(stringIndex);
storeData(PrintfDataType::vectorShortType);
// channel count
storeData(channelCount);
storeData<int16_t>(1);
storeData<int16_t>(2);
char actualOutput[maxPrintfOutputLength];
printFormatter->printKernelOutput([&actualOutput](char *str) { strncpy_s(actualOutput, maxPrintfOutputLength, str, maxPrintfOutputLength - 1); });
EXPECT_STREQ("1,2", actualOutput);
}
TEST_F(PrintFormatterTest, GivenVectorOfIntsWhenPrintingThenAllValuesAreInserted) {
int channelCount = 2;
auto stringIndex = injectFormatString("%v2d");
storeData(stringIndex);
storeData(PrintfDataType::vectorIntType);
// channel count
storeData(channelCount);
storeData<int32_t>(1);
storeData<int32_t>(2);
char actualOutput[maxPrintfOutputLength];
printFormatter->printKernelOutput([&actualOutput](char *str) { strncpy_s(actualOutput, maxPrintfOutputLength, str, maxPrintfOutputLength - 1); });
EXPECT_STREQ("1,2", actualOutput);
}
TEST_F(PrintFormatterTest, GivenSpecialVectorWhenPrintingThenAllValuesAreInserted) {
int channelCount = 2;
auto stringIndex = injectFormatString("%v2hld");
storeData(stringIndex);
storeData(PrintfDataType::vectorIntType);
// channel count
storeData(channelCount);
storeData<int32_t>(1);
storeData<int32_t>(2);
char actualOutput[maxPrintfOutputLength];
printFormatter->printKernelOutput([&actualOutput](char *str) { strncpy_s(actualOutput, maxPrintfOutputLength, str, maxPrintfOutputLength - 1); });
EXPECT_STREQ("1,2", actualOutput);
}
TEST_F(PrintFormatterTest, GivenVectorOfLongsWhenPrintingThenAllValuesAreInserted) {
int channelCount = 2;
auto stringIndex = injectFormatString("%v2ld");
storeData(stringIndex);
storeData(PrintfDataType::vectorLongType);
// channel count
storeData(channelCount);
storeData<int64_t>(100000000000000);
storeData<int64_t>(200000000000000);
char actualOutput[maxPrintfOutputLength];
printFormatter->printKernelOutput([&actualOutput](char *str) { strncpy_s(actualOutput, maxPrintfOutputLength, str, maxPrintfOutputLength - 1); });
EXPECT_STREQ("100000000000000,200000000000000", actualOutput);
}
TEST_F(PrintFormatterTest, GivenVectorOfFloatsWhenPrintingThenAllValuesAreInserted) {
int channelCount = 2;
auto stringIndex = injectFormatString("%v2f");
storeData(stringIndex);
storeData(PrintfDataType::vectorFloatType);
// channel count
storeData(channelCount);
storeData<float>(1.0);
storeData<float>(2.0);
char actualOutput[maxPrintfOutputLength];
printFormatter->printKernelOutput([&actualOutput](char *str) { strncpy_s(actualOutput, maxPrintfOutputLength, str, maxPrintfOutputLength - 1); });
EXPECT_STREQ("1.000000,2.000000", actualOutput);
}
TEST_F(PrintFormatterTest, GivenVectorOfDoublesWhenPrintingThenAllValuesAreInserted) {
int channelCount = 2;
auto stringIndex = injectFormatString("%v2f");
storeData(stringIndex);
storeData(PrintfDataType::vectorDoubleType);
// channel count
storeData(channelCount);
storeData<double>(1.0);
storeData<double>(2.0);
char actualOutput[maxPrintfOutputLength];
printFormatter->printKernelOutput([&actualOutput](char *str) { strncpy_s(actualOutput, maxPrintfOutputLength, str, maxPrintfOutputLength - 1); });
EXPECT_STREQ("1.000000,2.000000", actualOutput);
}
TEST_F(PrintFormatterTest, GivenPointerWhenPrintingThenValueIsInserted) {
auto stringIndex = injectFormatString("%p");
storeData(stringIndex);
int temp;
storeData(PrintfDataType::pointerType);
// channel count
storeData(reinterpret_cast<void *>(&temp));
// on 32bit configurations add extra 4 bytes when storing pointers, IGC always stores pointers on 8 bytes
if (is32bit) {
uint32_t padding = 0;
storeData(padding);
}
char actualOutput[maxPrintfOutputLength];
char referenceOutput[maxPrintfOutputLength];
snprintf(referenceOutput, sizeof(referenceOutput), "%p", reinterpret_cast<void *>(&temp));
printFormatter->printKernelOutput([&actualOutput](char *str) { strncpy_s(actualOutput, maxPrintfOutputLength, str, maxPrintfOutputLength - 1); });
EXPECT_STREQ(referenceOutput, actualOutput);
}
TEST_F(PrintFormatterTest, GivenPointerWith32BitKernelWhenPrintingThen32BitPointerIsPrinted) {
printFormatter.reset(new PrintFormatter(static_cast<uint8_t *>(data->getUnderlyingBuffer()), printfBufferSize, true, &kernelInfo->kernelDescriptor.kernelMetadata.printfStringsMap));
auto stringIndex = injectFormatString("%p");
storeData(stringIndex);
kernelInfo->kernelDescriptor.kernelAttributes.gpuPointerSize = 4;
storeData(PrintfDataType::pointerType);
// store pointer
uint32_t addressValue = 0;
storeData(addressValue);
void *pointer = nullptr;
// store non zero padding
uint32_t padding = 0xdeadbeef;
storeData(padding);
char actualOutput[maxPrintfOutputLength];
char referenceOutput[maxPrintfOutputLength];
snprintf(referenceOutput, sizeof(referenceOutput), "%p", pointer);
printFormatter->printKernelOutput([&actualOutput](char *str) { strncpy_s(actualOutput, maxPrintfOutputLength, str, maxPrintfOutputLength - 1); });
EXPECT_STREQ(referenceOutput, actualOutput);
}
TEST_F(PrintFormatterTest, Given2ByteVectorsWhenPrintingThenDataBufferParsedProperly) {
int channelCount = 4;
auto stringIndex = injectFormatString("%v4hhd %v4hhd");
storeData(stringIndex);
storeData(PrintfDataType::vectorByteType);
// channel count
storeData(channelCount);
// channel values
for (int i = 0; i < channelCount; i++) {
storeData(static_cast<int8_t>(i + 1));
}
// filler, should not be printed
for (int i = 0; i < 12; i++) {
storeData(static_cast<int8_t>(0));
}
storeData(PrintfDataType::vectorByteType);
// channel count
storeData(channelCount);
// channel values
for (int i = 0; i < channelCount; i++) {
storeData(static_cast<int8_t>(i + 1));
}
// filler, should not be printed
for (int i = 0; i < 12; i++) {
storeData(static_cast<int8_t>(0));
}
char actualOutput[maxPrintfOutputLength];
printFormatter->printKernelOutput([&actualOutput](char *str) { strncpy_s(actualOutput, maxPrintfOutputLength, str, maxPrintfOutputLength - 1); });
EXPECT_STREQ("1,2,3,4 1,2,3,4", actualOutput);
}
TEST_F(PrintFormatterTest, GivenEmptyBufferWhenPrintingThenFailSafely) {
char actualOutput[maxPrintfOutputLength];
actualOutput[0] = 0;
printFormatter->printKernelOutput([&actualOutput](char *str) { strncpy_s(actualOutput, maxPrintfOutputLength, str, maxPrintfOutputLength - 1); });
EXPECT_STREQ("", actualOutput);
}
TEST_F(PrintFormatterTest, GivenNoStringMapAndBufferWithFormatStringThenItIsPrintedProperly) {
printFormatter.reset(new PrintFormatter(static_cast<uint8_t *>(data->getUnderlyingBuffer()), printfBufferSize, true));
const char *formatString = "test string";
storeData(formatString);
char output[maxPrintfOutputLength];
printFormatter->printKernelOutput([&output](char *str) { strncpy_s(output, maxPrintfOutputLength, str, maxPrintfOutputLength - 1); });
EXPECT_STREQ(formatString, output);
}
TEST_F(PrintFormatterTest, GivenNoStringMapAndBufferWithEmptyFormatThenNothingIsPrinted) {
printFormatter.reset(new PrintFormatter(static_cast<uint8_t *>(data->getUnderlyingBuffer()), printfBufferSize, true));
const char *formatString = nullptr;
storeData(formatString);
char output[maxPrintfOutputLength]{};
printFormatter->printKernelOutput([&output](char *str) { strncpy_s(output, maxPrintfOutputLength, str, maxPrintfOutputLength - 1); });
EXPECT_STREQ("", output);
}
TEST_F(PrintFormatterTest, GivenNoStringMapAndBufferWithFormatStringAnd2StringsThenDataIsParsedAndPrintedProperly) {
printFormatter.reset(new PrintFormatter(static_cast<uint8_t *>(data->getUnderlyingBuffer()), printfBufferSize, true));
const char *formatString = "%s %s";
storeData(formatString);
const char *string1 = "str1";
storeData(PrintfDataType::pointerType);
storeData(string1);
const char *string2 = "str2";
storeData(PrintfDataType::pointerType);
storeData(string2);
const char *expectedOutput = "str1 str2";
char output[maxPrintfOutputLength];
printFormatter->printKernelOutput([&output](char *str) { strncpy_s(output, maxPrintfOutputLength, str, maxPrintfOutputLength - 1); });
EXPECT_STREQ(expectedOutput, output);
}
TEST_F(PrintFormatterTest, GivenNoStringMapAndInvalidStringIndexStoredWhenPrintingOutputThenBufferParsingStopsAfterInvalidIndex) {
printFormatter.reset(new PrintFormatter(static_cast<uint8_t *>(data->getUnderlyingBuffer()), printfBufferSize, true));
const char *string1 = "str1";
storeData(string1);
const int invalidIndex = -1;
const int zero = 0;
storeData(invalidIndex);
storeData(zero);
const char *string2 = "str2";
storeData(string2);
const char *expectedOutput = "str1";
char output[maxPrintfOutputLength];
printFormatter->printKernelOutput([&output](char *str) { strncpy_s(output, maxPrintfOutputLength, str, maxPrintfOutputLength - 1); });
EXPECT_STREQ(expectedOutput, output);
}
TEST_F(PrintFormatterTest, GivenTypeSmallerThan4BThenItIsReadAs4BValue) {
printFormatter.reset(new PrintFormatter(static_cast<uint8_t *>(data->getUnderlyingBuffer()), printfBufferSize, true));
const char *formatString = "%c %hd %d";
storeData(formatString);
char byteValue = 'a';
injectValue(byteValue);
short shortValue = 123;
injectValue(shortValue);
int intValue = 456;
injectValue(intValue);
const char *expectedOutput = "a 123 456";
char output[maxPrintfOutputLength];
printFormatter->printKernelOutput([&output](char *str) { strncpy_s(output, maxPrintfOutputLength, str, maxPrintfOutputLength - 1); });
EXPECT_STREQ(expectedOutput, output);
}
TEST(printToStdoutTest, GivenStringWhenPrintingToStdoutThenOutputOccurs) {
StreamCapture capture;
capture.captureStdout();
printToStdout("test");
std::string output = capture.getCapturedStdout();
EXPECT_STREQ("test", output.c_str());
}
TEST(simpleSprintf, GivenEmptyFormatStringWhenSimpleSprintfIsCalledThenBailOutWith0) {
char out[1024] = {7, 0};
auto ret = simpleSprintf<float>(out, sizeof(out), "", 3.0f);
EXPECT_EQ(0U, ret);
EXPECT_EQ(0, out[0]);
EXPECT_EQ(0, out[1]);
}
|