Files
whetstone_DSL/editor/tests/step1960_test.cpp

96 lines
3.6 KiB
C++
Raw Normal View History

// Step 1960: LibraryCapabilityLedger
//
// t1: set and get round-trip; has() before and after
// t2: score() returns correct value for known pair
// t3: score() returns 0.0 for unknown (library, domain) pair
// t4: allForLibrary() returns all records for one library
// t5: libraries() and domains() return sorted unique lists
#include "LibraryCapabilityLedger.h"
#include <iostream>
#include <algorithm>
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; }
static ws::LibraryCapabilityLedger makeLedger() {
ws::LibraryCapabilityLedger l;
l.set({"nlohmann_json", "serialization.json", 0.92f});
l.set({"protobuf", "serialization.binary", 0.95f});
l.set({"protobuf", "serialization.json", 0.60f});
l.set({"cublas", "compute.matrix", 0.99f});
l.set({"numpy", "compute.matrix", 0.95f});
l.set({"numpy", "compute.statistics", 0.93f});
return l;
}
void t1() {
T(set_get_and_has);
ws::LibraryCapabilityLedger l;
C(!l.has("nlohmann_json", "serialization.json"), "not present before set");
l.set({"nlohmann_json", "serialization.json", 0.92f});
C(l.has("nlohmann_json", "serialization.json"), "present after set");
auto rec = l.get("nlohmann_json", "serialization.json");
C(rec.has_value(), "get returns value");
C(rec->score == 0.92f, "score 0.92");
C(rec->libraryId == "nlohmann_json", "libraryId");
P();
}
void t2() {
T(score_returns_correct_value);
auto l = makeLedger();
C(l.score("cublas", "compute.matrix") == 0.99f, "cublas/compute.matrix 0.99");
C(l.score("numpy", "compute.matrix") == 0.95f, "numpy/compute.matrix 0.95");
C(l.score("protobuf", "serialization.binary") == 0.95f, "protobuf/serial.binary 0.95");
C(l.score("protobuf", "serialization.json") == 0.60f, "protobuf/serial.json 0.60");
P();
}
void t3() {
T(score_returns_zero_for_unknown_pair);
auto l = makeLedger();
C(l.score("unknown_lib", "compute.matrix") == 0.0f, "unknown lib -> 0.0");
C(l.score("cublas", "unknown_domain") == 0.0f, "unknown domain -> 0.0");
C(l.score("unknown_lib", "unknown_domain") == 0.0f, "both unknown -> 0.0");
P();
}
void t4() {
T(allForLibrary_returns_all_records_for_library);
auto l = makeLedger();
auto protobuf_recs = l.allForLibrary("protobuf");
C(protobuf_recs.size() == 2, "protobuf has 2 records");
auto numpy_recs = l.allForLibrary("numpy");
C(numpy_recs.size() == 2, "numpy has 2 records");
auto none = l.allForLibrary("unknown");
C(none.empty(), "unknown library has no records");
auto json_recs = l.allForDomain("serialization.json");
C(json_recs.size() == 2, "serialization.json has 2 records");
P();
}
void t5() {
T(libraries_and_domains_return_sorted_unique);
auto l = makeLedger();
auto libs = l.libraries();
C(libs.size() == 4, "4 unique libraries");
C(std::is_sorted(libs.begin(), libs.end()), "libraries sorted");
auto doms = l.domains();
C(doms.size() == 4, "4 unique domains");
C(std::is_sorted(doms.begin(), doms.end()), "domains sorted");
C(l.size() == 6, "total 6 records");
P();
}
int main() {
std::cout << "Step 1960: LibraryCapabilityLedger\n";
t1(); t2(); t3(); t4(); t5();
std::cout << "\n" << p << "/" << (p + f) << " passed\n";
return f > 0 ? 1 : 0;
}