Пример #1
0
void testSecondTimeLeak()
{
  Network n;
  Network::registerPyRegion("nupic.regions.TestNode", "TestNode");
  n.addRegion("r1", "py.TestNode", "");
  n.addRegion("r2", "py.TestNode", "");
}
Пример #2
0
void testYAML()
{
  const char *params = "{int32Param: 1234, real64Param: 23.1}";
  //  badparams contains a non-existent parameter
  const char *badparams = "{int32Param: 1234, real64Param: 23.1, badParam: 4}";


  Network net = Network();
  Region* level1;
  SHOULDFAIL(level1 = net.addRegion("level1", "TestNode", badparams););
Пример #3
0
TEST(InputTest, BasicNetworkConstruction)
{
  Network net;
  Region * r1 = net.addRegion("r1", "TestNode", "");
  Region * r2 = net.addRegion("r2", "TestNode", "");

  //Test constructor
  Input x(*r1, NTA_BasicType_Int32, true);
  Input y(*r2, NTA_BasicType_Byte, false);
  EXPECT_THROW(Input i(*r1, (NTA_BasicType)(NTA_BasicType_Last + 1), true),
               std::exception);

  //test getRegion()
  ASSERT_EQ(r1, &(x.getRegion()));
  ASSERT_EQ(r2, &(y.getRegion()));

  //test isRegionLevel()
  ASSERT_TRUE(x.isRegionLevel());
  ASSERT_TRUE(! y.isRegionLevel());

  //test isInitialized()
  ASSERT_TRUE(! x.isInitialized());
  ASSERT_TRUE(! y.isInitialized());

  //test one case of initialize()
  EXPECT_THROW(x.initialize(), std::exception);
  EXPECT_THROW(y.initialize(), std::exception);

  Dimensions d1;
  d1.push_back(8);
  d1.push_back(4);
  r1->setDimensions(d1);
  Dimensions d2;
  d2.push_back(4);
  d2.push_back(2);
  r2->setDimensions(d2);
  net.link("r1", "r2", "TestFanIn2", "");

  x.initialize();
  y.initialize();

  //test evaluateLinks()
  //should return 0 because x is initialized
  ASSERT_EQ(0u, x.evaluateLinks());
  //should return 0 because there are no links
  ASSERT_EQ(0u, y.evaluateLinks());

  //test getData()
  const ArrayBase * pa = &(y.getData());
  ASSERT_EQ(0u, pa->getCount());
  Real64* buf = (Real64*)(pa->getBuffer());
  ASSERT_TRUE(buf != nullptr);
}
Пример #4
0
void testUnregisterRegion()
{
  Network n;
  n.addRegion("test", "py.TestNode", "");

  Network::unregisterPyRegion("TestNode");

  bool caughtException = false;
  try
  {
    n.addRegion("test", "py.TestNode", "");
  } catch (std::exception& e) {
    NTA_DEBUG << "Caught exception as expected: '" << e.what() << "'";
    caughtException = true;
  }
  if (caughtException)
  {
    NTA_DEBUG << "testUnregisterRegion passed";
  } else {
    NTA_THROW << "testUnregisterRegion did not throw an exception as expected";
  }

}
Пример #5
0
void testExceptionBug()
{
  Network n;
  Region *l1 = n.addRegion("l1", "py.TestNode", "");
  //Dimensions d(1);
  Dimensions d(1);
  l1->setDimensions(d);
  bool caughtException = false;
  try
  {
    n.run(1);
  } catch (std::exception& e) {
    NTA_DEBUG << "Caught exception as expected: '" << e.what() << "'";
    caughtException = true;
  }
  if (caughtException)
  {
    NTA_DEBUG << "testExceptionBug passed";
  } else {
    NTA_THROW << "testExceptionBug did not throw an exception as expected";
  }
    
}
Пример #6
0
void testPynodeLinking()
{
  Network net = Network();

  Region * region1 = net.addRegion("region1", "TestNode", "");
  Region * region2 = net.addRegion("region2", "py.TestNode", "");
  std::cout << "Linking region 1 to region 2" << std::endl;
  net.link("region1", "region2", "TestFanIn2", "");

  std::cout << "Setting region1 dims to (6,4)" << std::endl;
  Dimensions r1dims;
  r1dims.push_back(6);
  r1dims.push_back(4);
  region1->setDimensions(r1dims);

  std::cout << "Initializing network..." << std::endl;
  net.initialize();

  const Dimensions& r2dims = region2->getDimensions();
  NTA_CHECK(r2dims.size() == 2) << " actual dims: " << r2dims.toString();
  NTA_CHECK(r2dims[0] == 3) << " actual dims: " << r2dims.toString();
  NTA_CHECK(r2dims[1] == 2) << " actual dims: " << r2dims.toString();
  
  ArrayRef r1OutputArray = region1->getOutputData("bottomUpOut");

  region1->compute();

  std::cout << "Checking region1 output after first iteration..." << std::endl;
  Real64 *buffer = (Real64*) r1OutputArray.getBuffer();

  for (size_t i = 0; i < r1OutputArray.getCount(); i++)
  {
    if (verbose)
      std::cout << "  " << i << "    " << buffer[i] << "" << std::endl;
    if (i%2 == 0)
      NTA_CHECK(buffer[i] == 0);
    else
      NTA_CHECK(buffer[i] == (i-1)/2);
  }

  region2->prepareInputs();
  ArrayRef r2InputArray = region2->getInputData("bottomUpIn");
  std::cout << "Region 2 input after first iteration:" << std::endl;
  Real64 *buffer2 = (Real64*) r2InputArray.getBuffer();
  NTA_CHECK(buffer != buffer2);

  for (size_t i = 0; i < r2InputArray.getCount(); i++)
  {
    if (verbose)
      std::cout << "  " << i << "    " << buffer2[i] << "" << std::endl;

    if (i%2 == 0)
      NTA_CHECK(buffer[i] == 0);
    else
      NTA_CHECK(buffer[i] == (i-1)/2);
  }

  std::cout << "Region 2 input by node" << std::endl;
  std::vector<Real64> r2NodeInput;

  for (size_t node = 0; node < 6; node++)
  {
    region2->getInput("bottomUpIn")->getInputForNode(node, r2NodeInput);
    if (verbose)
    {
      std::cout << "Node " << node << ": ";
      for (size_t i = 0; i < r2NodeInput.size(); i++)
      {
        std::cout << r2NodeInput[i] << " ";
      }
      std::cout << "" << std::endl;
    }
    // 4 nodes in r1 fan in to 1 node in r2
    int row = node/3;
    int col = node - (row * 3);
    NTA_CHECK(r2NodeInput.size() == 8);
    NTA_CHECK(r2NodeInput[0] == 0);
    NTA_CHECK(r2NodeInput[2] == 0);
    NTA_CHECK(r2NodeInput[4] == 0);
    NTA_CHECK(r2NodeInput[6] == 0);
    // these values are specific to the fanin2 link policy
    NTA_CHECK(r2NodeInput[1] == row * 12    + col * 2) 
      << "row: " << row << " col: " << col << " val: " << r2NodeInput[1];
    NTA_CHECK(r2NodeInput[3] == row * 12    + col * 2 + 1)
      << "row: " << row << " col: " << col << " val: " << r2NodeInput[3];
    NTA_CHECK(r2NodeInput[5] == row * 12 + 6 + col * 2)
      << "row: " << row << " col: " << col << " val: " << r2NodeInput[5];
    NTA_CHECK(r2NodeInput[7] == row * 12 + 6 + col * 2 + 1)
      << "row: " << row << " col: " << col << " val: " << r2NodeInput[7];
  }

  region2->compute();
}
Пример #7
0
TEST(WatcherTest, SampleNetwork) {
  // NOTE:  This test generates files for the subsequent two tests.
  // generate sample network   [level1] -> [level2] -> [level3]
  Network n;
  n.addRegion("level1", "TestNode", "{dim: [4,2]}");
  n.addRegion("level2", "TestNode", "");
  n.addRegion("level3", "TestNode", "");
  n.link("level1", "level2");
  n.link("level2", "level3");
  n.initialize();


  // erase any previous contents of testfile
  Directory::removeTree("TestOutputDir");
  Directory::create("TestOutputDir");

  // test creation
  Watcher w("TestOutputDir/testfile");

  // test uint32Params
  unsigned int id1 = w.watchParam("level1", "uint32Param");
  ASSERT_EQ(id1, (unsigned int)1);
  // test uint64Params
  unsigned int id2 = w.watchParam("level1", "uint64Param");
  ASSERT_EQ(id2, (unsigned int)2);
  // test int32Params
  w.watchParam("level1", "int32Param");
  // test int64Params
  w.watchParam("level1", "int64Param");
  // test real32Params
  w.watchParam("level1", "real32Param");
  // test real64Params
  w.watchParam("level1", "real64Param");
  // test stringParams
  w.watchParam("level1", "stringParam");
  // test unclonedParams
  w.watchParam("level1", "unclonedParam", 0);
  w.watchParam("level1", "unclonedParam", 1);

  // test attachToNetwork()
  w.attachToNetwork(n);

  //test two simultaneous Watchers on the same network with different files
  Watcher* w2 = new Watcher("TestOutputDir/testfile2");

  // test int64ArrayParam
  w2->watchParam("level1", "int64ArrayParam");
  // test real32ArrayParam
  w2->watchParam("level1", "real32ArrayParam");
  // test output
  w2->watchOutput("level1", "bottomUpOut");
  // test int64ArrayParam, sparse = false
  w2->watchParam("level1", "int64ArrayParam", -1, false);

  w2->attachToNetwork(n);

  // set one of the uncloned parameters to 1 instead of 0
  // n.getRegions().getByName("level1")->getNodeAtIndex(1).setParameterUInt32("unclonedParam",(UInt32)1);
  //n.run(3);
  // see if Watcher notices change in parameter values  after 3 iterations
  n.getRegions().getByName("level1")->setParameterUInt64("uint64Param", (UInt64)66);
  n.run(3);

  // test flushFile() - this should produce output
  w2->flushFile();

  // test closeFile()
  w2->closeFile();

  // test to make sure data is flushed when Watcher is deleted
  delete w2;

  // The two generated files are used in next test.
}
Пример #8
0
void InputTest::RunTests()
{
  {
    Network net;
    Region * r1 = net.addRegion("r1", "TestNode", "");
    Region * r2 = net.addRegion("r2", "TestNode", "");

    //Test constructor
    Input x(*r1, NTA_BasicType_Int32, true);
    Input y(*r2, NTA_BasicType_Byte, false);
    SHOULDFAIL(Input i(*r1, (NTA_BasicType)(NTA_BasicType_Last + 1), true));

    //test getRegion()
    TESTEQUAL(r1, &(x.getRegion()));
    TESTEQUAL(r2, &(y.getRegion()));
    
    //test isRegionLevel()
    TEST(x.isRegionLevel());
    TEST(! y.isRegionLevel());

    //test isInitialized()
    TEST(! x.isInitialized());
    TEST(! y.isInitialized());

    //test one case of initialize()
    SHOULDFAIL(x.initialize());
    SHOULDFAIL(y.initialize());

    Dimensions d1;
    d1.push_back(8);
    d1.push_back(4);
    r1->setDimensions(d1);
    Dimensions d2;
    d2.push_back(4);
    d2.push_back(2);
    r2->setDimensions(d2);
    net.link("r1", "r2", "TestFanIn2", "");

    x.initialize();
    y.initialize();

    //test evaluateLinks()
    //should return 0 because x is initialized
    TESTEQUAL(0u, x.evaluateLinks());
    //should return 0 because there are no links
    TESTEQUAL(0u, y.evaluateLinks());

    //test getData()
    const ArrayBase * pa = &(y.getData());
    TESTEQUAL(0u, pa->getCount());
    Real64* buf = (Real64*)(pa->getBuffer());
    TEST(buf != nullptr);
  }

  {
    Network net;
    Region * region1 = net.addRegion("region1", "TestNode", "");
    Region * region2 = net.addRegion("region2", "TestNode", "");

    Dimensions d1;
    d1.push_back(8);
    d1.push_back(4);
    region1->setDimensions(d1);

    net.link("region1", "region2", "TestFanIn2", "");

    //test initialize(), which is called by net.initialize()
    //also test evaluateLinks() which is called here
    net.initialize();
    net.run(1);

    //test that region has correct induced dimensions
    Dimensions d2 = region2->getDimensions();
    TESTEQUAL(2u, d2.size());
    TESTEQUAL(4u, d2[0]);
    TESTEQUAL(2u, d2[1]);

    //test getName() and setName()
    Input * in1 = region1->getInput("bottomUpIn");
    Input * in2 = region2->getInput("bottomUpIn");

    TESTEQUAL("bottomUpIn", in1->getName());
    TESTEQUAL("bottomUpIn", in2->getName());
    in1->setName("uselessName");
    TESTEQUAL("uselessName", in1->getName());
    in1->setName("bottomUpIn");

    //test isInitialized()
    TEST(in1->isInitialized());
    TEST(in2->isInitialized());

    //test getLinks()
    std::vector<Link*> links = in2->getLinks();
    TESTEQUAL(1u, links.size());
    for(auto & link : links) {
      //do something to make sure l[i] is a valid Link*
      TEST(link != nullptr);
      //should fail because regions are initialized
      SHOULDFAIL(in2->removeLink(link));
    }

    //test findLink()
    Link * l1 = in1->findLink("region1", "bottomUpOut");
    TEST(l1 == nullptr);
    Link * l2 = in2->findLink("region1", "bottomUpOut");
    TEST(l2 != nullptr);


    //test removeLink(), uninitialize()
    //uninitialize() is called internally from removeLink()
    {
      //can't remove link b/c region1 initialized
      SHOULDFAIL(in2->removeLink(l2)); 
      //can't remove region b/c region1 has links
      SHOULDFAIL(net.removeRegion("region1")); 
      region1->uninitialize();
      region2->uninitialize();
      SHOULDFAIL(in1->removeLink(l2));
      in2->removeLink(l2);
      SHOULDFAIL(in2->removeLink(l2));
      //l1 == NULL
      SHOULDFAIL(in1->removeLink(l1));
    }
  }

  {
    Network net;
    Region * region1 = net.addRegion("region1", "TestNode", "");
    Region * region2 = net.addRegion("region2", "TestNode", "");

    Dimensions d1;
    d1.push_back(8);
    d1.push_back(4);
    region1->setDimensions(d1);

    //test addLink() indirectly - it is called by Network::link()
    net.link("region1", "region2", "TestFanIn2", "");

    //test initialize(), which is called by net.initialize()
    net.initialize();

    Dimensions d2 = region2->getDimensions();
    Input * in1 = region1->getInput("bottomUpIn");
    Input * in2 = region2->getInput("bottomUpIn");
    Output * out1 = region1->getOutput("bottomUpOut");
    
    //test isInitialized()
    TEST(in1->isInitialized());
    TEST(in2->isInitialized());

    //test evaluateLinks(), in1 already initialized
    TESTEQUAL(0u, in1->evaluateLinks());
    TESTEQUAL(0u, in2->evaluateLinks());

    //test prepare
    {    
      //set in2 to all zeroes
      const ArrayBase * ai2 = &(in2->getData());
      Real64* idata = (Real64*)(ai2->getBuffer());
      for (UInt i = 0; i < 64; i++)
        idata[i] = 0;

      //set out1 to all 10's
      const ArrayBase * ao1 = &(out1->getData());
      idata = (Real64*)(ao1->getBuffer());
      for (UInt i = 0; i < 64; i++)
        idata[i] = 10;

      //confirm that in2 is still all zeroes
      ai2 = &(in2->getData());
      idata = (Real64*)(ai2->getBuffer());
      //only test 4 instead of 64 to cut down on number of tests
      for (UInt i = 0; i < 4; i++)
        TESTEQUAL(0, idata[i]);

      in2->prepare();

      //confirm that in2 is now all 10's
      ai2 = &(in2->getData());
      idata = (Real64*)(ai2->getBuffer());
      //only test 4 instead of 64 to cut down on number of tests
      for (UInt i = 0; i < 4; i++)
        TESTEQUAL(10, idata[i]);
    }

    net.run(2);

    //test getSplitterMap()
    std::vector< std::vector<size_t> > sm;
    sm = in2->getSplitterMap();
    TESTEQUAL(8u, sm.size());
    TESTEQUAL(8u, sm[0].size());
    TESTEQUAL(16u, sm[0][4]);
    TESTEQUAL(12u, sm[3][0]);
    TESTEQUAL(31u, sm[3][7]);
   
    //test getInputForNode()
    std::vector<Real64> input;
    in2->getInputForNode(0, input);
    TESTEQUAL(1, input[0]);
    TESTEQUAL(0, input[1]);
    TESTEQUAL(8, input[5]);
    TESTEQUAL(9, input[7]);
    in2->getInputForNode(3, input);
    TESTEQUAL(1, input[0]);
    TESTEQUAL(6, input[1]);
    TESTEQUAL(15, input[7]);
    
    //test getData()
    const ArrayBase * pa = &(in2->getData());
    TESTEQUAL(64u, pa->getCount());
    Real64* data = (Real64*)(pa->getBuffer());
    TESTEQUAL(1, data[0]);
    TESTEQUAL(0, data[1]);
    TESTEQUAL(1, data[30]);
    TESTEQUAL(15, data[31]);
    TESTEQUAL(31, data[63]);
  }

  //test with two regions linking into the same input
  {
    Network net;
    Region * region1 = net.addRegion("region1", "TestNode", "");
    Region * region2 = net.addRegion("region2", "TestNode", "");
    Region * region3 = net.addRegion("region3", "TestNode", "");

    Dimensions d1;
    d1.push_back(8);
    d1.push_back(4);
    region1->setDimensions(d1);
    region2->setDimensions(d1);

    net.link("region1", "region3", "TestFanIn2", "");
    net.link("region2", "region3", "TestFanIn2", "");

    net.initialize();
    
    Dimensions d3 = region3->getDimensions();
    Input * in3 = region3->getInput("bottomUpIn");
    
    TESTEQUAL(2u, d3.size());
    TESTEQUAL(4u, d3[0]);
    TESTEQUAL(2u, d3[1]);

    net.run(2);
    
    //test getSplitterMap()
    std::vector< std::vector<size_t> > sm;
    sm = in3->getSplitterMap();
    TESTEQUAL(8u, sm.size());
    TESTEQUAL(16u, sm[0].size());
    TESTEQUAL(16u, sm[0][4]);
    TESTEQUAL(12u, sm[3][0]);
    TESTEQUAL(31u, sm[3][7]);
    
    //test getInputForNode()
    std::vector<Real64> input;
    in3->getInputForNode(0, input);
    TESTEQUAL(1, input[0]);
    TESTEQUAL(0, input[1]);
    TESTEQUAL(8, input[5]);
    TESTEQUAL(9, input[7]);
    in3->getInputForNode(3, input);
    TESTEQUAL(1, input[0]);
    TESTEQUAL(6, input[1]);
    TESTEQUAL(15, input[7]);
    
    //test getData()
    const ArrayBase * pa = &(in3->getData());
    TESTEQUAL(128u, pa->getCount());
    Real64* data = (Real64*)(pa->getBuffer());
    TESTEQUAL(1, data[0]);
    TESTEQUAL(0, data[1]);
    TESTEQUAL(1, data[30]);
    TESTEQUAL(15, data[31]);
    TESTEQUAL(31, data[63]);
    TESTEQUAL(1, data[64]);
    TESTEQUAL(0, data[65]);
    TESTEQUAL(1, data[94]);
    TESTEQUAL(15, data[95]);
    TESTEQUAL(31, data[127]);
    
  }

}
Пример #9
0
int main(int argc, const char * argv[])
{
    // Create network
    Network net = Network();

    // Add VectorFileSensor region to network
    Region* region = net.addRegion("region", "VectorFileSensor", "{activeOutputCount: 1}");

    // Set region dimensions
    Dimensions dims;
    dims.push_back(1);

    std::cout << "Setting region dimensions" << dims.toString() << std::endl;

    region->setDimensions(dims);

    // Load data
    std::string path = Path::makeAbsolute("../../src/examples/regions/Data.csv");

    std::cout << "Loading data from " << path << std::endl;

    std::vector<std::string> loadFileArgs;
    loadFileArgs.push_back("loadFile");
    loadFileArgs.push_back(path);
    loadFileArgs.push_back("2");

    region->executeCommand(loadFileArgs);

    // Initialize network
    std::cout << "Initializing network" << std::endl;

    net.initialize();

    ArrayRef outputArray = region->getOutputData("dataOut");

    // Compute
    std::cout << "Compute" << std::endl;

    region->compute();

    // Get output
    Real64 *buffer = (Real64*) outputArray.getBuffer();

    for (size_t i = 0; i < outputArray.getCount(); i++)
    {
        std::cout << "  " << i << "    " << buffer[i] << "" << std::endl;
    }

    // Serialize
    Network net2;
    {
      std::stringstream ss;
      net.write(ss);
      net2.read(ss);
    }
    net2.initialize();

    Region* region2 = net2.getRegions().getByName("region");
    region2->executeCommand(loadFileArgs);
    ArrayRef outputArray2 = region2->getOutputData("dataOut");
    Real64 *buffer2 = (Real64*)outputArray2.getBuffer();

    net.run(1);
    net2.run(1);

    NTA_ASSERT(outputArray2.getCount() == outputArray.getCount());
    for (size_t i = 0; i < outputArray.getCount(); i++)
    {
        std::cout << "  " << i << "    " << buffer[i] << "   " << buffer2[i]
                  << std::endl;
    }

    return 0;
}
Пример #10
0
int realmain(bool leakTest)
{
  // verbose == true turns on extra output that is useful for
  // debugging the test (e.g. when the TestNode compute() 
  // algorithm changes)


  std::cout << "Creating network..." << std::endl;
  Network n;
  
  std::cout << "Region count is " << n.getRegions().getCount() << "" << std::endl;

  std::cout << "Adding a PyNode region..." << std::endl;
  Network::registerPyRegion("nupic.regions.TestNode", "TestNode");
  Region* level2 = n.addRegion("level2", "py.TestNode", "{int32Param: 444}");

  std::cout << "Region count is " << n.getRegions().getCount() << "" << std::endl;
  std::cout << "Node type: " << level2->getType() << "" << std::endl;
  std::cout << "Nodespec is:\n"  << level2->getSpec()->toString() << "" << std::endl;
  
  Real64 rval;
  std::string int64Param("int64Param");
  std::string real64Param("real64Param");

  // get the value of intArrayParam after the setParameter call.

  // --- Test getParameterReal64 of a PyNode
  rval = level2->getParameterReal64("real64Param");
  NTA_CHECK(rval == 64.1); 
  std::cout << "level2 getParameterReal64() returned: " << rval << std::endl;

  // --- Test setParameterReal64 of a PyNode
  level2->setParameterReal64("real64Param", 77.7);
  rval = level2->getParameterReal64("real64Param");
  NTA_CHECK(rval == 77.7); 

  // should fail because network has not been initialized
  SHOULDFAIL(n.run(1));

  // should fail because network can't be initialized
  SHOULDFAIL (n.initialize() );

  std::cout << "Setting dimensions of level1..." << std::endl;
  Dimensions d;
  d.push_back(4);
  d.push_back(4);


  std::cout << "Setting dimensions of level2..." << std::endl;
  level2->setDimensions(d);

  std::cout << "Initializing again..." << std::endl;
  n.initialize();

  testExceptionBug();
  testPynodeInputOutputAccess(level2);
  testPynodeArrayParameters(level2);
  testPynodeLinking();
  if (!leakTest)
  {
    //testNuPIC1x();
    //testPynode1xLinking();
  }

  std::cout << "Done -- all tests passed" << std::endl;

  return 0;
}
Пример #11
0
void testSecondTimeLeak()
{
  Network n;
  n.addRegion("r1", "py.TestNode", "");
  n.addRegion("r2", "py.TestNode", "");
}
Пример #12
0
TEST(InputTest, Links)
{
  Network net;
  Region * region1 = net.addRegion("region1", "TestNode", "");
  Region * region2 = net.addRegion("region2", "TestNode", "");

  Dimensions d1;
  d1.push_back(8);
  d1.push_back(4);
  region1->setDimensions(d1);

  net.link("region1", "region2", "TestFanIn2", "");

  //test initialize(), which is called by net.initialize()
  //also test evaluateLinks() which is called here
  net.initialize();
  net.run(1);

  //test that region has correct induced dimensions
  Dimensions d2 = region2->getDimensions();
  ASSERT_EQ(2u, d2.size());
  ASSERT_EQ(4u, d2[0]);
  ASSERT_EQ(2u, d2[1]);

  //test getName() and setName()
  Input * in1 = region1->getInput("bottomUpIn");
  Input * in2 = region2->getInput("bottomUpIn");

  EXPECT_STREQ("bottomUpIn", in1->getName().c_str());
  EXPECT_STREQ("bottomUpIn", in2->getName().c_str());
  in1->setName("uselessName");
  EXPECT_STREQ("uselessName", in1->getName().c_str());
  in1->setName("bottomUpIn");

  //test isInitialized()
  ASSERT_TRUE(in1->isInitialized());
  ASSERT_TRUE(in2->isInitialized());

  //test getLinks()
  std::vector<Link*> links = in2->getLinks();
  ASSERT_EQ(1u, links.size());
  for(auto & link : links) {
    //do something to make sure l[i] is a valid Link*
    ASSERT_TRUE(link != nullptr);
    //should fail because regions are initialized
    EXPECT_THROW(in2->removeLink(link), std::exception);
  }

  //test findLink()
  Link * l1 = in1->findLink("region1", "bottomUpOut");
  ASSERT_TRUE(l1 == nullptr);
  Link * l2 = in2->findLink("region1", "bottomUpOut");
  ASSERT_TRUE(l2 != nullptr);


  //test removeLink(), uninitialize()
  //uninitialize() is called internally from removeLink()
  {
    //can't remove link b/c region1 initialized
    EXPECT_THROW(in2->removeLink(l2), std::exception); 
    //can't remove region b/c region1 has links
    EXPECT_THROW(net.removeRegion("region1"), std::exception); 
    region1->uninitialize();
    region2->uninitialize();
    EXPECT_THROW(in1->removeLink(l2), std::exception);
    in2->removeLink(l2);
    EXPECT_THROW(in2->removeLink(l2), std::exception);
    //l1 == NULL
    EXPECT_THROW(in1->removeLink(l1), std::exception);
  }
}
Пример #13
0
TEST(InputTest, LinkTwoRegionsOneInput)
{
  Network net;
  Region * region1 = net.addRegion("region1", "TestNode", "");
  Region * region2 = net.addRegion("region2", "TestNode", "");
  Region * region3 = net.addRegion("region3", "TestNode", "");

  Dimensions d1;
  d1.push_back(8);
  d1.push_back(4);
  region1->setDimensions(d1);
  region2->setDimensions(d1);

  net.link("region1", "region3", "TestFanIn2", "");
  net.link("region2", "region3", "TestFanIn2", "");

  net.initialize();

  Dimensions d3 = region3->getDimensions();
  Input * in3 = region3->getInput("bottomUpIn");

  ASSERT_EQ(2u, d3.size());
  ASSERT_EQ(4u, d3[0]);
  ASSERT_EQ(2u, d3[1]);

  net.run(2);

  //test getSplitterMap()
  std::vector< std::vector<size_t> > sm;
  sm = in3->getSplitterMap();
  ASSERT_EQ(8u, sm.size());
  ASSERT_EQ(16u, sm[0].size());
  ASSERT_EQ(16u, sm[0][4]);
  ASSERT_EQ(12u, sm[3][0]);
  ASSERT_EQ(31u, sm[3][7]);

  //test getInputForNode()
  std::vector<Real64> input;
  in3->getInputForNode(0, input);
  ASSERT_EQ(1, input[0]);
  ASSERT_EQ(0, input[1]);
  ASSERT_EQ(8, input[5]);
  ASSERT_EQ(9, input[7]);
  in3->getInputForNode(3, input);
  ASSERT_EQ(1, input[0]);
  ASSERT_EQ(6, input[1]);
  ASSERT_EQ(15, input[7]);

  //test getData()
  const ArrayBase * pa = &(in3->getData());
  ASSERT_EQ(128u, pa->getCount());
  Real64* data = (Real64*)(pa->getBuffer());
  ASSERT_EQ(1, data[0]);
  ASSERT_EQ(0, data[1]);
  ASSERT_EQ(1, data[30]);
  ASSERT_EQ(15, data[31]);
  ASSERT_EQ(31, data[63]);
  ASSERT_EQ(1, data[64]);
  ASSERT_EQ(0, data[65]);
  ASSERT_EQ(1, data[94]);
  ASSERT_EQ(15, data[95]);
  ASSERT_EQ(31, data[127]);

}
Пример #14
0
TEST(InputTest, SplitterMap)
{
  Network net;
  Region * region1 = net.addRegion("region1", "TestNode", "");
  Region * region2 = net.addRegion("region2", "TestNode", "");

  Dimensions d1;
  d1.push_back(8);
  d1.push_back(4);
  region1->setDimensions(d1);

  //test addLink() indirectly - it is called by Network::link()
  net.link("region1", "region2", "TestFanIn2", "");

  //test initialize(), which is called by net.initialize()
  net.initialize();

  Dimensions d2 = region2->getDimensions();
  Input * in1 = region1->getInput("bottomUpIn");
  Input * in2 = region2->getInput("bottomUpIn");
  Output * out1 = region1->getOutput("bottomUpOut");

  //test isInitialized()
  ASSERT_TRUE(in1->isInitialized());
  ASSERT_TRUE(in2->isInitialized());

  //test evaluateLinks(), in1 already initialized
  ASSERT_EQ(0u, in1->evaluateLinks());
  ASSERT_EQ(0u, in2->evaluateLinks());

  //test prepare
  {    
    //set in2 to all zeroes
    const ArrayBase * ai2 = &(in2->getData());
    Real64* idata = (Real64*)(ai2->getBuffer());
    for (UInt i = 0; i < 64; i++)
      idata[i] = 0;

    //set out1 to all 10's
    const ArrayBase * ao1 = &(out1->getData());
    idata = (Real64*)(ao1->getBuffer());
    for (UInt i = 0; i < 64; i++)
      idata[i] = 10;

    //confirm that in2 is still all zeroes
    ai2 = &(in2->getData());
    idata = (Real64*)(ai2->getBuffer());
    //only test 4 instead of 64 to cut down on number of tests
    for (UInt i = 0; i < 4; i++)
      ASSERT_EQ(0, idata[i]);

    in2->prepare();

    //confirm that in2 is now all 10's
    ai2 = &(in2->getData());
    idata = (Real64*)(ai2->getBuffer());
    //only test 4 instead of 64 to cut down on number of tests
    for (UInt i = 0; i < 4; i++)
      ASSERT_EQ(10, idata[i]);
  }

  net.run(2);

  //test getSplitterMap()
  std::vector< std::vector<size_t> > sm;
  sm = in2->getSplitterMap();
  ASSERT_EQ(8u, sm.size());
  ASSERT_EQ(8u, sm[0].size());
  ASSERT_EQ(16u, sm[0][4]);
  ASSERT_EQ(12u, sm[3][0]);
  ASSERT_EQ(31u, sm[3][7]);

  //test getInputForNode()
  std::vector<Real64> input;
  in2->getInputForNode(0, input);
  ASSERT_EQ(1, input[0]);
  ASSERT_EQ(0, input[1]);
  ASSERT_EQ(8, input[5]);
  ASSERT_EQ(9, input[7]);
  in2->getInputForNode(3, input);
  ASSERT_EQ(1, input[0]);
  ASSERT_EQ(6, input[1]);
  ASSERT_EQ(15, input[7]);

  //test getData()
  const ArrayBase * pa = &(in2->getData());
  ASSERT_EQ(64u, pa->getCount());
  Real64* data = (Real64*)(pa->getBuffer());
  ASSERT_EQ(1, data[0]);
  ASSERT_EQ(0, data[1]);
  ASSERT_EQ(1, data[30]);
  ASSERT_EQ(15, data[31]);
  ASSERT_EQ(31, data[63]);
}
Пример #15
0
TEST(WatcherTest, SampleNetwork)
{
  //generate sample network
  Network n;
  n.addRegion("level1", "TestNode", "");
  n.addRegion("level2", "TestNode", "");
  n.addRegion("level3", "TestNode", "");
  Dimensions d;
  d.push_back(8);
  d.push_back(4);
  n.getRegions().getByName("level1")->setDimensions(d);
  n.link("level1", "level2", "TestFanIn2", "");
  n.link("level2", "level3", "TestFanIn2", "");
  n.initialize();

  //erase any previous contents of testfile
  OFStream o("testfile");
  o.close();
  
  //test creation
  Watcher w("testfile");

  //test uint32Params
  unsigned int id1 = w.watchParam("level1", "uint32Param");
  ASSERT_EQ(id1, (unsigned int)1);
  //test uint64Params
  unsigned int id2 = w.watchParam("level1", "uint64Param");
  ASSERT_EQ(id2, (unsigned int)2);
  //test int32Params
  w.watchParam("level1", "int32Param");
  //test int64Params
  w.watchParam("level1", "int64Param");
  //test real32Params
  w.watchParam("level1", "real32Param");
  //test real64Params
  w.watchParam("level1", "real64Param");
  //test stringParams
  w.watchParam("level1", "stringParam");
  //test unclonedParams
  w.watchParam("level1", "unclonedParam", 0);
  w.watchParam("level1", "unclonedParam", 1);
  
  //test attachToNetwork()
  w.attachToNetwork(n);

  //test two simultaneous Watchers on the same network with different files
  Watcher* w2 = new Watcher("testfile2");

  //test int64ArrayParam
  w2->watchParam("level1", "int64ArrayParam");
  //test real32ArrayParam
  w2->watchParam("level1", "real32ArrayParam");
  //test output
  w2->watchOutput("level1", "bottomUpOut");
  //test int64ArrayParam, sparse = false
  w2->watchParam("level1", "int64ArrayParam", -1, false);

  w2->attachToNetwork(n);

  //set one of the uncloned parameters to 1 instead of 0
  //n.getRegions().getByName("level1")->getNodeAtIndex(1).setParameterUInt32("unclonedParam", (UInt32)1);
  //n.run(3);
  //see if Watcher notices change in parameter values after 3 iterations
  n.getRegions().getByName("level1")->setParameterUInt64("uint64Param", (UInt64)66);
  n.run(3);

  //test flushFile() - this should produce output
  w.flushFile();

  //test closeFile()
  w.closeFile();

  //test to make sure data is flushed when Watcher is deleted
  delete w2;
}