94 lines
3.6 KiB
C++
94 lines
3.6 KiB
C++
|
|
// Step 1953: PolyglotFunctionMap
|
||
|
|
//
|
||
|
|
// t1: registerFunction and functionCountForLanguage
|
||
|
|
// t2: lookupTag returns correct tag; throws on missing
|
||
|
|
// t3: hasFunction correct
|
||
|
|
// t4: languagesForFunction across multiple languages
|
||
|
|
// t5: registeredLanguages and allFunctions
|
||
|
|
|
||
|
|
#include "PolyglotFunctionMap.h"
|
||
|
|
#include <algorithm>
|
||
|
|
#include <iostream>
|
||
|
|
|
||
|
|
namespace ws = whetstone;
|
||
|
|
static int p=0,f=0;
|
||
|
|
#define T(n) { std::cout<<" "<<#n<<"... "; }
|
||
|
|
#define P() { std::cout<<"PASS\n"; ++p; }
|
||
|
|
#define F(m) { std::cout<<"FAIL: "<<m<<"\n"; ++f; }
|
||
|
|
#define C(c,m) if(!(c)){F(m);return;}
|
||
|
|
|
||
|
|
void t1(){
|
||
|
|
T(registerFunction_and_functionCount);
|
||
|
|
ws::PolyglotFunctionMap m;
|
||
|
|
C(m.functionCountForLanguage("Python") == 0, "zero initially");
|
||
|
|
m.registerFunction("Python", "compute", "py::compute_v1");
|
||
|
|
m.registerFunction("Python", "format", "py::format_v1");
|
||
|
|
m.registerFunction("Rust", "compute", "rs::compute_v1");
|
||
|
|
C(m.functionCountForLanguage("Python") == 2, "two Python functions");
|
||
|
|
C(m.functionCountForLanguage("Rust") == 1, "one Rust function");
|
||
|
|
C(m.functionCountForLanguage("Go") == 0, "zero Go functions");
|
||
|
|
P();
|
||
|
|
}
|
||
|
|
|
||
|
|
void t2(){
|
||
|
|
T(lookupTag_correct_and_throws);
|
||
|
|
ws::PolyglotFunctionMap m;
|
||
|
|
m.registerFunction("Go", "sort", "go::sort_v1");
|
||
|
|
C(m.lookupTag("Go","sort") == "go::sort_v1", "correct tag");
|
||
|
|
bool threw1 = false, threw2 = false;
|
||
|
|
try { m.lookupTag("Go","missing"); } catch (const std::out_of_range&) { threw1 = true; }
|
||
|
|
try { m.lookupTag("Java","sort"); } catch (const std::out_of_range&) { threw2 = true; }
|
||
|
|
C(threw1, "throws on missing function");
|
||
|
|
C(threw2, "throws on missing language");
|
||
|
|
P();
|
||
|
|
}
|
||
|
|
|
||
|
|
void t3(){
|
||
|
|
T(hasFunction_correct);
|
||
|
|
ws::PolyglotFunctionMap m;
|
||
|
|
m.registerFunction("TypeScript","render","ts::render_v1");
|
||
|
|
C( m.hasFunction("TypeScript","render"), "registered present");
|
||
|
|
C(!m.hasFunction("TypeScript","missing"), "missing function absent");
|
||
|
|
C(!m.hasFunction("Java","render"), "missing language absent");
|
||
|
|
P();
|
||
|
|
}
|
||
|
|
|
||
|
|
void t4(){
|
||
|
|
T(languagesForFunction_across_languages);
|
||
|
|
ws::PolyglotFunctionMap m;
|
||
|
|
m.registerFunction("Python", "compute", "py::compute");
|
||
|
|
m.registerFunction("Rust", "compute", "rs::compute");
|
||
|
|
m.registerFunction("Haskell", "compute", "hs::compute");
|
||
|
|
m.registerFunction("Go", "sort", "go::sort");
|
||
|
|
auto langs = m.languagesForFunction("compute");
|
||
|
|
C(langs.size() == 3, "three languages for compute");
|
||
|
|
bool hasPy = std::find(langs.begin(),langs.end(),"Python") != langs.end();
|
||
|
|
bool hasRs = std::find(langs.begin(),langs.end(),"Rust") != langs.end();
|
||
|
|
bool hasHs = std::find(langs.begin(),langs.end(),"Haskell") != langs.end();
|
||
|
|
C(hasPy && hasRs && hasHs, "Python, Rust, Haskell present");
|
||
|
|
auto sortLangs = m.languagesForFunction("sort");
|
||
|
|
C(sortLangs.size() == 1 && sortLangs[0] == "Go", "only Go for sort");
|
||
|
|
C(m.languagesForFunction("unknown").empty(), "empty for unknown function");
|
||
|
|
P();
|
||
|
|
}
|
||
|
|
|
||
|
|
void t5(){
|
||
|
|
T(registeredLanguages_and_allFunctions);
|
||
|
|
ws::PolyglotFunctionMap m;
|
||
|
|
m.registerFunction("Python", "compute", "py::compute");
|
||
|
|
m.registerFunction("Rust", "compute", "rs::compute");
|
||
|
|
m.registerFunction("Go", "sort", "go::sort");
|
||
|
|
auto langs = m.registeredLanguages();
|
||
|
|
C(langs.size() == 3, "three languages");
|
||
|
|
auto all = m.allFunctions();
|
||
|
|
C(all.size() == 3, "three (lang,fn) pairs");
|
||
|
|
P();
|
||
|
|
}
|
||
|
|
|
||
|
|
int main(){
|
||
|
|
std::cout << "Step 1953: PolyglotFunctionMap\n";
|
||
|
|
t1(); t2(); t3(); t4(); t5();
|
||
|
|
std::cout << "\n" << p << "/" << (p+f) << " passed\n";
|
||
|
|
return f > 0 ? 1 : 0;
|
||
|
|
}
|