コード例 #1
0
ファイル: ArrayTest.cpp プロジェクト: AndreCAndersen/nupic
void nta::ArrayTest::testArrayTyping()
{
  TestCaseIterator testCase;

  for(testCase = testCases_.begin(); testCase != testCases_.end(); testCase++)
  {
    //testArrayCreation() already validates that ArrayBase objects can't be created
    //using invalid NTA_BasicType parameters, so we skip those test cases here
    if(testCase->second.testUsesInvalidParameters)
    {
      continue;
    }
    
    ArrayBase a(testCase->second.dataType);

    TESTEQUAL2("Test case: " +
                testCase->first +
                " - the type of a created ArrayBase should match the requested "
                "type",
              testCase->second.dataType, a.getType());

    std::string name(BasicType::getName(a.getType()));
    TESTEQUAL2("Test case: " +
                testCase->first +
                " - the string representation of a type contained in a "
                "created ArrayBase should match the expected string", 
              testCase->second.dataTypeText, 
              name);
  }    
}
コード例 #2
0
ファイル: TesterTest.cpp プロジェクト: jaredweiss/nupic.core
 void TesterTest::RunTestsShouldFail()
 {
   // These are probably the only tests in our test suite that should fail.
   TESTEQUAL2("Integer test, should fail",1,0);
   TESTEQUAL2("Double test, should fail",23.42,23.421);
   TESTEQUAL2_STR("String test, should fail","Numenta","Numenta ");
   TESTEQUAL(1,0);
   TESTEQUAL(23.42,23.421);
   EXPECT_STREQ("Numenta","Numenta ");
   TEST(false);
   TEST2("TEST2(false)", false);    
 }
コード例 #3
0
  void FastCLAClassifierTest::testBasic()
  {
    vector<UInt> steps;
    steps.push_back(1);
    FastCLAClassifier c = FastCLAClassifier(steps, 0.1, 0.1, 0);

    // Create a vector of input bit indices
    vector<UInt> input1;
    input1.push_back(1);
    input1.push_back(5);
    input1.push_back(9);
    ClassifierResult result1;
    c.fastCompute(0, input1, 4, 34.7, false, true, true, &result1);

    // Create a vector of input bit indices
    vector<UInt> input2;
    input2.push_back(1);
    input2.push_back(5);
    input2.push_back(9);
    ClassifierResult result2;
    c.fastCompute(1, input2, 4, 34.7, false, true, true, &result2);

    {
      bool foundMinus1 = false;
      bool found1 = false;
      for (auto it = result2.begin();
           it != result2.end(); ++it)
      {
        if (it->first == -1)
        {
          // The -1 key is used for the actual values
          TESTEQUAL2("already found key -1 in classifier result",
                     false, foundMinus1);
          foundMinus1 = true;
          TESTEQUAL2("Expected five buckets since it has only seen bucket 4 "
                     "(so it has buckets 0-4).", (long unsigned int)5, it->second->size());
          TEST2("Incorrect actual value for bucket 4",
                fabs(it->second->at(4) - 34.7) < 0.000001);
        } else if (it->first == 1) {
          // Check the one-step prediction
          TESTEQUAL2("already found key 1 in classifier result", false, found1);
          found1 = true;
          TESTEQUAL2("expected five bucket predictions", (long unsigned int)5, it->second->size());
          TEST2("incorrect prediction for bucket 0",
                fabs(it->second->at(0) - 0.2) < 0.000001);
          TEST2("incorrect prediction for bucket 1",
                fabs(it->second->at(1) - 0.2) < 0.000001);
          TEST2("incorrect prediction for bucket 2",
                fabs(it->second->at(2) - 0.2) < 0.000001);
          TEST2("incorrect prediction for bucket 3",
                fabs(it->second->at(3) - 0.2) < 0.000001);
          TEST2("incorrect prediction for bucket 4",
                fabs(it->second->at(4) - 0.2) < 0.000001);
        }
      }
      TESTEQUAL2("key -1 not found in classifier result", true, foundMinus1);
      TESTEQUAL2("key 1 not found in classifier result", true, found1);
    }
  }
コード例 #4
0
ファイル: ArrayTest.cpp プロジェクト: AndreCAndersen/nupic
void nta::ArrayTest::testBufferAssignment()
{
  TestCaseIterator testCase;
  
  for(testCase = testCases_.begin(); testCase != testCases_.end(); testCase++)
  {
    boost::scoped_array<char> buf(new char[testCase->second.dataTypeSize *
                                           testCase->second.allocationSize]);
        
    ArrayBase a(testCase->second.dataType);
    a.setBuffer(buf.get(), testCase->second.allocationSize);
    
    TESTEQUAL2("Test case: " +
                testCase->first +
                " - setBuffer() should used the assigned buffer",
              buf.get(),
              a.getBuffer());

    boost::scoped_array<char> buf2(new char[testCase->second.dataTypeSize *
                                            testCase->second.allocationSize]);

    bool caughtException = false;
    
    try
    {
      a.setBuffer(buf2.get(), testCase->second.allocationSize);
    }
    catch(nta::Exception)
    {
      caughtException = true;
    }
    
    TEST2("Test case: " +
            testCase->first +
            " - setting a buffer when one is already set should raise an "
            "exception",
          caughtException);
  }    
}
コード例 #5
0
ファイル: TesterTest.cpp プロジェクト: jaredweiss/nupic.core
  // Run all appropriate tests
  void TesterTest::RunTests()
  {

    TESTEQUAL2("Integer test, should succeed",1,1);
    TESTEQUAL2("Double test, should succeed",23.42,23.42);
    TESTEQUAL2_STR("String test, should succeed","Numenta","Numenta");

    // Repeat the above for TESTEQUAL
    TESTEQUAL(1,1);
    TESTEQUAL(23.42,23.42);
    EXPECT_STREQ("Numenta","Numenta");
    
    // Test functions in Common
    TESTEQUAL2("Max test", 23.3, Max(23.2, 23.3));
    TESTEQUAL2("Min test", 23.2, Min(23.2, 23.3));
    TESTEQUAL2("Max test", 'b', Max('a', 'b'));
    TESTEQUAL2("Min test", 'a', Min('a', 'b'));

    // Repeat the above for TESTEQUAL
    TESTEQUAL(23.3, Max(23.2, 23.3));
    TESTEQUAL(23.2, Min(23.2, 23.3));
    TESTEQUAL('b', Max('a', 'b'));
    TESTEQUAL('a', Min('a', 'b'));

    // TESTEQUAL_FLOAT allows error less than 0.000001
    TESTEQUAL_FLOAT(23.42,23.4200009);

    // Tests for TEST
    TEST(true);  
    TEST2("TEST2(true)", true);  

    // Tests for throws  
    nupic::LoggingException e(__FILE__, __LINE__);
    e << "This exception should be thrown.";

    SHOULDFAIL(throw e);  
    SHOULDFAIL_WITH_MESSAGE(
      throw e,
      "This exception should be thrown.");
  }
コード例 #6
0
  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);  
  }
コード例 #7
0
ファイル: ArrayTest.cpp プロジェクト: AndreCAndersen/nupic
void nta::ArrayTest::testBufferAllocation()
{
  bool caughtException;

  TestCaseIterator testCase;
  
  for(testCase = testCases_.begin(); testCase != testCases_.end(); testCase++)
  {
    caughtException = false;
    ArrayBase a(testCase->second.dataType);

    try
    {
      a.allocateBuffer((size_t) (testCase->second.allocationSize));
    }
    catch(std::exception& )
    {
      caughtException = true;
    }
      
    if(testCase->second.testUsesInvalidParameters)
    {
      TESTEQUAL2("Test case: " +
                  testCase->first +
                  " - allocation of an ArrayBase of invalid size should raise an "
                  "exception",
                caughtException,
                true);
    }
    else
    {
      TESTEQUAL2("Test case: " +
                  testCase->first +
                  " - Allocation of an ArrayBase of valid size should return a "
                  "valid pointer",
                caughtException,
                false);
            
      caughtException = false;
      
      try
      {
        a.allocateBuffer(10);
      }
      catch(nta::Exception)
      {
        caughtException = true;
      }
      
      TEST2("Test case: " +
              testCase->first +
              " - allocating a buffer when one is already allocated should "
              "raise an exception",
            caughtException);
      
      TESTEQUAL2("Test case: " +
                  testCase->first +
                  " - Size of allocated ArrayBase should match requested size",
                (size_t) testCase->second.allocationSize,
                a.getCount());
    }
  }
}
コード例 #8
0
ファイル: ArrayTest.cpp プロジェクト: AndreCAndersen/nupic
void nta::ArrayTest::testArrayCreation()
{
  boost::scoped_ptr<ArrayBase> arrayP;

  TestCaseIterator testCase;
  
  for(testCase = testCases_.begin(); testCase != testCases_.end(); testCase++)
  {
    char *buf = (char *) -1;
    
    if(testCase->second.testUsesInvalidParameters)
    {
      bool caughtException = false;

      try
      {
        arrayP.reset(new ArrayBase(testCase->second.dataType));
      }
      catch(nta::Exception)
      {
        caughtException = true;
      }

      TEST2("Test case: " +
              testCase->first +
              " - Should throw an exception on trying to create an invalid "
              "ArrayBase",
            caughtException);
    }
    else
    {
      arrayP.reset(new ArrayBase(testCase->second.dataType));
      buf = (char *) arrayP->getBuffer();
      TEST2("Test case: " +
              testCase->first +
              " - When not passed a size, a newly created ArrayBase should "
              "have a NULL buffer",
            buf == NULL);
      TESTEQUAL2("Test case: " +
                  testCase->first +
                  " - When not passed a size, a newly created ArrayBase should "
                  "have a count equal to zero",
                (size_t) 0, 
                arrayP->getCount());

      boost::scoped_array<char> buf2(new char[testCase->second.dataTypeSize *
                                              testCase->second.allocationSize]);
                                              
      arrayP.reset(new ArrayBase(testCase->second.dataType,
                             buf2.get(),
                             testCase->second.allocationSize));
      
      buf = (char *) arrayP->getBuffer();
      TEST2("Test case: " +
              testCase->first +
              " - Preallocating a buffer for a newly created ArrayBase should "
              "use the provided buffer",
            buf == buf2.get());
      TESTEQUAL2("Test case: " +
                  testCase->first +
                  " - Preallocating a buffer should have a count equal to our "
                  "allocation size",
                (size_t) testCase->second.allocationSize,
                arrayP->getCount());
    }    
  }
}