source: CLRX/CLRadeonExtender/trunk/tests/amdasm/AssemblerBasics.cpp @ 3893

Last change on this file since 3893 was 3893, checked in by matszpk, 16 months ago

CLRadeonExtender: Add testSuiteId to test messages.

File size: 7.4 KB
Line 
1/*
2 *  CLRadeonExtender - Unofficial OpenCL Radeon Extensions Library
3 *  Copyright (C) 2014-2018 Mateusz Szpakowski
4 *
5 *  This library is free software; you can redistribute it and/or
6 *  modify it under the terms of the GNU Lesser General Public
7 *  License as published by the Free Software Foundation; either
8 *  version 2.1 of the License, or (at your option) any later version.
9 *
10 *  This library is distributed in the hope that it will be useful,
11 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 *  Lesser General Public License for more details.
14 *
15 *  You should have received a copy of the GNU Lesser General Public
16 *  License along with this library; if not, write to the Free Software
17 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18 */
19
20#include <CLRX/Config.h>
21#include <iostream>
22#include <cstdio>
23#include <sstream>
24#include <memory>
25#include <CLRX/utils/Containers.h>
26#include <CLRX/amdasm/Assembler.h>
27#include "../TestUtils.h"
28#include "AsmBasics.h"
29
30using namespace CLRX;
31
32typedef std::pair<CString, const AsmSymbol*> AsmSymbolEntryC;
33
34// recursive function to push symbols from scope adding to name scope prefixes
35static void pushSymbolsFromScopes(const AsmScope& scope,
36            std::vector<AsmSymbolEntryC>& symEntries, const std::string& prefix)
37{
38    // push symbol in this scope adding to its name a prefix
39    for (const AsmSymbolEntry& symEntry: scope.symbolMap)
40    {
41        std::string symName = prefix+symEntry.first.c_str();
42        symEntries.push_back(AsmSymbolEntryC(CString(symName.c_str()), &symEntry.second));
43    }
44    // traverse through scopes
45    for (const auto& scopeEntry: scope.scopeMap)
46    {
47        std::string newPrefix = prefix+scopeEntry.first.c_str()+"::";
48        pushSymbolsFromScopes(*scopeEntry.second, symEntries, newPrefix);
49    }
50}
51
52static void testAssembler(cxuint testSuiteId, cxuint testId, const AsmTestCase& testCase)
53{
54    std::istringstream input(testCase.input);
55    std::ostringstream errorStream;
56    std::ostringstream printStream;
57   
58    // create assembler with testcase input
59    // enable ASM_TESTRUN (needed while testing)
60    Assembler assembler("test.s", input, (ASM_ALL|ASM_TESTRUN)&~ASM_ALTMACRO,
61            BinaryFormat::AMD, GPUDeviceType::CAPE_VERDE, errorStream, printStream);
62    // include include dirs from testcase
63    for (const char* incDir: testCase.includeDirs)
64        assembler.addIncludeDir(incDir);
65    // just assemble
66    bool good = assembler.assemble();
67    /* compare results */
68    char testName[30];
69    snprintf(testName, 30, "Test%u #%u", testSuiteId, testId);
70   
71    // check whether good, format, device, bitness is match
72    assertValue(testName, "good", int(testCase.good), int(good));
73    assertValue(testName, "format", int(testCase.format),
74                int(assembler.getBinaryFormat()));
75    assertValue(testName, "deviceType", int(testCase.deviceType),
76                int(assembler.getDeviceType()));
77    assertValue(testName, "64bit", int(testCase.is64Bit), int(assembler.is64Bit()));
78   
79    // check kernels
80    const std::vector<AsmKernel>& resKernels = assembler.getKernels();
81    assertValue(testName, "kernels.length", testCase.kernelNames.size(), resKernels.size());
82    for (size_t i = 0; i < testCase.kernelNames.size(); i++)
83    {
84        std::ostringstream caseOss;
85        caseOss << "Kernel#" << i << ".";
86        caseOss.flush();
87        std::string caseName(caseOss.str());
88        assertString(testName, caseName+"name", testCase.kernelNames[i],
89                     resKernels[i].name);
90    }
91   
92    // check sections
93    const std::vector<AsmSection>& resSections = assembler.getSections();
94    assertValue(testName, "sections.length", testCase.sections.size(), resSections.size());
95    for (size_t i = 0; i < testCase.sections.size(); i++)
96    {
97        std::ostringstream caseOss;
98        caseOss << "Section#" << i << ".";
99        caseOss.flush();
100        std::string caseName(caseOss.str());
101       
102        const AsmSection& resSection = resSections[i];
103        const Section& expSection = testCase.sections[i];
104        assertString(testName, caseName+"name", expSection.name, resSection.name);
105        assertValue(testName, caseName+"kernelId", expSection.kernelId,
106                    resSection.kernelId);
107        assertValue(testName, caseName+"type", int(expSection.type), int(resSection.type));
108        assertArray<cxbyte>(testName, caseName+"content", expSection.content,
109                    resSection.content);
110    }
111    // check symbols
112    std::vector<AsmSymbolEntryC> symEntries;
113    // push symbols recursive traversing through scopes (begins from global)
114    pushSymbolsFromScopes(assembler.getGlobalScope(), symEntries, "");
115    std::sort(symEntries.begin(), symEntries.end(),
116                [](const AsmSymbolEntryC& s1, const AsmSymbolEntryC& s2)
117                { return s1.first < s2.first; });
118   
119    assertValue(testName, "symbols.length", testCase.symbols.size(), symEntries.size());
120   
121    for (cxuint i = 0; i < testCase.symbols.size(); i++)
122    {
123        std::ostringstream caseOss;
124        char buf[32];
125        snprintf(buf, 32, "Symbol#%u.", i);
126        std::string caseName(buf);
127       
128        // compare expected symbols with result symbols
129        const AsmSymbolEntryC& resSymbol = symEntries[i];
130        const SymEntry& expSymbol = testCase.symbols[i];
131        assertString(testName,caseName+"name", expSymbol.name, resSymbol.first);
132        assertValue(testName,caseName+"value", expSymbol.value, resSymbol.second->value);
133        assertValue(testName,caseName+"sectId", expSymbol.sectionId,
134                     resSymbol.second->sectionId);
135        assertValue(testName,caseName+"size", expSymbol.size, resSymbol.second->size);
136        assertValue(testName,caseName+"isDefined", int(expSymbol.hasValue),
137                    int(resSymbol.second->hasValue));
138        assertValue(testName,caseName+"onceDefined", int(expSymbol.onceDefined),
139                    int(resSymbol.second->onceDefined));
140        assertValue(testName,caseName+"base", int(expSymbol.base),
141                    int(resSymbol.second->base));
142        assertValue(testName,caseName+"info", int(expSymbol.info),
143                    int(resSymbol.second->info));
144        assertValue(testName,caseName+"other", int(expSymbol.other),
145                    int(resSymbol.second->other));
146        assertValue(testName,caseName+"regRange", int(expSymbol.regRange),
147                    int(resSymbol.second->regRange));
148    }
149    errorStream.flush();
150    printStream.flush();
151    const std::string errorMsgs = errorStream.str();
152    const std::string printMsgs = printStream.str();
153    assertString(testName, "errorMessages", testCase.errorMessages, errorMsgs);
154    assertString(testName, "printMessages", testCase.printMessages, printMsgs);
155}
156
157int main(int argc, const char** argv)
158{
159    int retVal = 0;
160    for (size_t i = 0; asmTestCases1Tbl[i].input != nullptr; i++)
161        try
162        { testAssembler(0, i, asmTestCases1Tbl[i]); }
163        catch(const std::exception& ex)
164        {
165            std::cerr << ex.what() << std::endl;
166            retVal = 1;
167        }
168    for (size_t i = 0; asmTestCases2Tbl[i].input != nullptr; i++)
169        try
170        { testAssembler(1, i, asmTestCases2Tbl[i]); }
171        catch(const std::exception& ex)
172        {
173            std::cerr << ex.what() << std::endl;
174            retVal = 1;
175        }
176    return retVal;
177}
Note: See TracBrowser for help on using the repository browser.