std::string SpatialPoolerNode::getParameterString(const std::string& paramName, Int64 index) { if (!poolersAllocated_) NTA_THROW << "Invalid operation -- SpatialPoolerNode must be initialized by initializing the network"; // per-node parameter UInt poolerIndex = clonedNodes_ ? 0 : (UInt) index; if (paramName == "sparsify") { return poolers_[poolerIndex]->getSparsificationModeStr(); } else if (paramName == "spatialPoolerAlgorithm") { return poolers_[poolerIndex]->getInferenceModeStr(); } else if (paramName == "nta_patchMasks") { OMemStream buf; poolers_[poolerIndex]->getInputMasks().saveState(buf); return buf.str(); } else if (paramName == "coincidenceMatrixString") { OMemStream buf; poolers_[0]->getCoincidenceMatrix(buf); return buf.str(); } else { NTA_THROW << "Unknown string parameter: " << paramName; } }
// ------------------------------------------------------- // Test output stream // ------------------------------------------------------- TEST(MemStreamTest, OutputStream) { OMemStream ms; std::stringstream ss; for (int i = 0; i < 500; i++) { ms << i << " "; ss << i << " "; } const char *dataP = ms.str(); size_t size = ms.pcount(); std::string msStr(dataP, size); std::string ssStr = ss.str(); ASSERT_EQ(msStr, ssStr) << "out data"; ASSERT_EQ(ms.eof(), ss.eof()) << "out eof"; ASSERT_EQ(ms.fail(), ss.fail()) << "out fail"; }
void RandomTest::RunTests() { UInt32 r1, r2, r3; // make sure the global instance is seeded from time() // in the test situation, we can be sure we were seeded less than 100000 seconds ago // make sure random number system is initialized by creating a random // object. Use the object to make sure the compiler doesn't complain about // an unused variable Random r; UInt64 x = r.getUInt64(); TEST(x != 0); mysrandom(148); Random rWithSeed(148); for (auto & elem : expected) { r2 = rWithSeed.getUInt32(); r3 = myrandom(); // Uncomment to generate expected values // NTA_DEBUG << "expected[" << i << "] = " << r1 << ";"; if ((r2 != r3) || (r3 != elem)) { // only create a test result if we get a failure. Otherwise we // end up creating and saving a lot of unnecessary test results. TESTEQUAL(r2, r3); break; } } if (r2 == r3) { // create one positive test result if everything is good TEST(true); } TESTEQUAL(148U, rWithSeed.getSeed()); { // same test, different seed mysrandom(98765); Random r(98765); for (int i = 0; i < 1000; i++) { r1 = r.getUInt32(); r2 = myrandom(); if (r1 != r2) { TESTEQUAL(r1, r2); break; } } if (r1 == r2) { // create one positive test result if everything is good TEST(true); } TESTEQUAL(98765U, r.getSeed()); } { // test copy constructor. Random r1(289436); int i; for (i = 0; i < 100; i++) r1.getUInt32(); Random r2(r1); UInt32 v1, v2; for (i = 0; i < 100; i++) { v1 = r1.getUInt32(); v2 = r2.getUInt32(); if (v1 != v2) break; } TEST2("copy constructor", v1 == v2); } { // test operator= Random r1(289436); int i; for (i = 0; i < 100; i++) r1.getUInt32(); Random r2(86726008); for (i = 0; i < 100; i++) r2.getUInt32(); r2 = r1; UInt32 v1, v2; for (i = 0; i < 100; i++) { v1 = r1.getUInt32(); v2 = r2.getUInt32(); if (v1 != v2) break; } TEST2("operator=", v1 == v2); } { // test serialization/deserialization Random r1(862973); int i; for (i = 0; i < 100; i++) r1.getUInt32(); // serialize OMemStream ostream; ostream << r1; // print out serialization for debugging std::string x(ostream.str(), ostream.pcount()); NTA_INFO << "random serialize string: '" << x << "'"; // Serialization should be deterministic and platform independent std::string expectedString = "random-v1 862973 randomimpl-v1 31 1624753037 2009419031 633377166 -1574892086 1144529851 1406716263 1553314465 1423305391 27869257 -777179591 -476654188 -1158534456 1569565720 -1530107687 -689150702 2101555921 1535380948 1896399958 -452440042 1943129361 -1905643199 -1174375166 2019813130 -1490833398 1624596014 -694914945 517970867 1155092552 -244858627 -823365838 -938489650 7 10 endrandom-v1"; TESTEQUAL(expectedString, x); // deserialize into r2 std::string s(ostream.str(), ostream.pcount()); std::stringstream ss(s); Random r2; ss >> r2; // r1 and r2 should be identical TESTEQUAL(r1.getSeed(), r2.getSeed()); UInt32 v1, v2; for (i = 0; i < 100; i++) { v1 = r1.getUInt32(); v2 = r2.getUInt32(); NTA_CHECK(v1 == v2); } TESTEQUAL2("serialization", v1, v2); } { // make sure that we are returning values in the correct range // @todo perform statistical tests Random r; UInt32 seed = r.getSeed(); TEST2("seed not zero", seed != 0); int i; UInt32 max32 = 10000000; UInt64 max64 = (UInt64)max32 * (UInt64)max32; for (i = 0; i < 200; i++) { UInt32 i32 = r.getUInt32(max32); TEST2("UInt32", i32 < max32); UInt64 i64 = r.getUInt64(max64); TEST2("UInt64", i64 < max64); Real64 r64 = r.getReal64(); TEST2("Real64", r64 >= 0.0 && r64 < 1.0); } } { // tests for sampling UInt32 population[] = {1, 2, 3, 4}; Random r(42); { // choose 0 elements UInt32 choices[0]; r.sample(population, 4, choices, 0); } { // choose some elements UInt32 choices[2]; r.sample(population, 4, choices, 2); TESTEQUAL2("check element 0", 2, choices[0]); TESTEQUAL2("check element 1", 4, choices[1]); } { // choose all elements UInt32 choices[4]; r.sample(population, 4, choices, 4); TESTEQUAL2("check element 0", 1, choices[0]); TESTEQUAL2("check element 1", 2, choices[1]); TESTEQUAL2("check element 2", 3, choices[2]); TESTEQUAL2("check element 3", 4, choices[3]); } { // nChoices > nPopulation UInt32 choices[5]; bool caught = false; try { r.sample(population, 4, choices, 5); } catch (LoggingException& exc) { caught = true; } TEST2("checking for exception from population too small", caught); } } { // tests for shuffling Random r(42); UInt32 arr[] = {1, 2, 3, 4}; UInt32* start = arr; UInt32* end = start + 4; r.shuffle(start, end); TESTEQUAL2("check element 0", 3, arr[0]); TESTEQUAL2("check element 1", 2, arr[1]); TESTEQUAL2("check element 2", 4, arr[2]); TESTEQUAL2("check element 3", 1, arr[3]); } { // tests for Cap'n Proto serialization Random r1, r2; UInt32 v1, v2; const char* outputPath = "RandomTest1.temp"; { std::ofstream out(outputPath); r1.write(out); out.close(); } { std::ifstream in(outputPath); r2.read(in); in.close(); } v1 = r1.getUInt32(); v2 = r2.getUInt32(); TESTEQUAL2("check serialization for unused Random object", v1, v2); { std::ofstream out(outputPath); r1.write(out); out.close(); } { std::ifstream in(outputPath); r2.read(in); in.close(); } v1 = r1.getUInt32(); v2 = r2.getUInt32(); TESTEQUAL2("check serialization for used Random object", v1, v2); // clean up remove(outputPath); } }
void MemStreamTest::RunTests() { // ------------------------------------------------------- // Test input stream // ------------------------------------------------------- { std::string test("hi there"); IMemStream ms((char*)(test.data()), test.size()); std::stringstream ss(test); for (int i=0; i<5; i++) { std::string s1, s2; ms >> s1; ss >> s2; TESTEQUAL2("in", s2, s1); TESTEQUAL2("in fail", ss.fail(), ms.fail()); TESTEQUAL2("in eof", ss.eof(), ms.eof()); } // Test changing the buffer std::string test2("bye now"); ms.str((char*)(test2.data()), test2.size()); ms.seekg(0); ms.clear(); std::stringstream ss2(test2); for (int i=0; i<5; i++) { std::string s1, s2; ms >> s1; ss2 >> s2; TESTEQUAL2("in2", s2, s1); TESTEQUAL2("in2 fail", ss2.fail(), ms.fail()); TESTEQUAL2("in2 eof", ss2.eof(), ms.eof()); } } // ------------------------------------------------------- // Test setting the buffer on a default input stream // ------------------------------------------------------- { std::string test("third test"); IMemStream ms; ms.str((char*)(test.data()), test.size()); std::stringstream ss(test); for (int i=0; i<5; i++) { std::string s1, s2; ms >> s1; ss >> s2; TESTEQUAL2("in2", s2, s1); TESTEQUAL2("in2 fail", ss.fail(), ms.fail()); TESTEQUAL2("in2 eof", ss.eof(), ms.eof()); } } // ------------------------------------------------------- // Test output stream // ------------------------------------------------------- { OMemStream ms; std::stringstream ss; for (int i=0; i<500; i++) { ms << i << " "; ss << i << " "; } const char* dataP = ms.str(); size_t size = ms.pcount(); std::string msStr(dataP, size); std::string ssStr = ss.str(); TESTEQUAL2("out data", msStr, ssStr); TESTEQUAL2("out eof", ms.eof(), ss.eof()); TESTEQUAL2("out fail", ms.fail(), ss.fail()); } // ------------------------------------------------------- // Test memory limits // ------------------------------------------------------- // Set max at 0x10000000 for day to day testing so that test doesn't take too long. // To determine the actual memory limits, change this max to something very large and // see where we break. size_t max = 0x10000000L; size_t sizeLimit = memLimitsTest(max); TESTEQUAL2("maximum stream size", sizeLimit >= max, true); }