예제 #1
0
 void CollectionTest::testEmptyCollection()
 {
   Collection<int> c;
   TEST(c.getCount() == 0);
   TEST(c.contains("blah") == false);
   SHOULDFAIL(c.getByIndex(0));
   SHOULDFAIL(c.getByName("blah"));
 }
예제 #2
0
  void CollectionTest::testCollectionWith_1_Item()
  {
    auto p = new Item(5);
    Collection<Item *> c;
    TEST(c.contains("x") == false);
    c.add("x", p);
    TEST(c.contains("x") == true);
    TEST(c.getCount() == 1);
    TEST(c.getByIndex(0).second->x == 5);
    TEST(c.getByName("x")->x == 5);
    
    SHOULDFAIL(c.getByIndex(1));
    SHOULDFAIL(c.getByName("blah"));

    delete p;
  }
예제 #3
0
  void CollectionTest::testCollectionWith_137_Items()
  {
    Collection<int> c;
    for (int i = 0; i < 137; ++i)
    {
      std::stringstream ss;
      ss << i;
      c.add(ss.str(), i);
    }

    TEST(c.getCount() == 137);

    for (int i = 0; i < 137; ++i)
    {
      TEST(c.getByIndex(i).second == i);
    }

    SHOULDFAIL(c.getByIndex(137));
    SHOULDFAIL(c.getByName("blah"));    
  }
예제 #4
0
  void CollectionTest::testCollectionWith_2_Items()
  {
    Collection<Item> c;
    c.add("x1", Item(1));
    c.add("x2", Item(2));
    TEST(c.getCount() == 2);

    Item i1 = c.getByIndex(0).second;
    Item i2 = c.getByIndex(1).second;

    TEST(i1.x == 1 && i2.x == 2);

    TEST(c.contains("no such item") == false);
    TEST(c.contains("x1") == true);
      TEST(c.contains("x2") == true);
    TEST(c.getByName("x1").x == 1);
    TEST(c.getByName("x2").x == 2);
    
    SHOULDFAIL(c.getByIndex(2));
    SHOULDFAIL(c.getByName("blah"));    
  }
예제 #5
0
  // 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 CollectionTest::testCollectionAddRemove()
  {
    Collection<int> c;
    c.add("0", 0);
    c.add("1", 1);
    c.add("2", 2);
    // c is now: 0,1,2
    TEST(c.contains("0"));
    TEST(c.contains("1"));
    TEST(c.contains("2"));
    TEST(!c.contains("3"));
      
    SHOULDFAIL(c.add("0", 0));
    SHOULDFAIL(c.add("1", 1));
    SHOULDFAIL(c.add("2", 2));

    TESTEQUAL(0, c.getByName("0"));
    TESTEQUAL(1, c.getByName("1"));
    TESTEQUAL(2, c.getByName("2"));

    TESTEQUAL(0, c.getByIndex(0).second);
    TESTEQUAL(1, c.getByIndex(1).second);
    TESTEQUAL(2, c.getByIndex(2).second);

    TEST(c.getCount() == 3);

    SHOULDFAIL(c.remove("4"));

    // remove in middle of collection
    c.remove("1");
    // c is now 0, 2
    SHOULDFAIL(c.remove("1"));
    
    TEST(c.getCount() == 2);
    TEST(c.contains("0"));
    TEST(!c.contains("1"));
    TEST(c.contains("2"));

    TESTEQUAL(0, c.getByIndex(0).second);
    // item "2" has shifted into position 1
    TESTEQUAL(2, c.getByIndex(1).second);
    
    // should append to end of collection
    c.add("1", 1);
    // c is now 0, 2, 1
    TEST(c.getCount() == 3);
    TEST(c.contains("1"));
    TESTEQUAL(0, c.getByIndex(0).second);
    TESTEQUAL(2, c.getByIndex(1).second);
    TESTEQUAL(1, c.getByIndex(2).second);

    SHOULDFAIL(c.add("0", 0));
    SHOULDFAIL(c.add("1", 1));
    SHOULDFAIL(c.add("2", 2));

    // remove at end of collection
    c.remove("1");
    // c is now 0, 2
    SHOULDFAIL(c.remove("1"));
    TEST(c.getCount() == 2);
    TESTEQUAL(0, c.getByIndex(0).second);
    TESTEQUAL(2, c.getByIndex(1).second);

    // continue removing until done
    c.remove("0");
    // c is now 2
    SHOULDFAIL(c.remove("0"));
    TEST(c.getCount() == 1);
    // "2" shifts to first position
    TESTEQUAL(2, c.getByIndex(0).second);

    c.remove("2");
    // c is now empty
    TEST(c.getCount() == 0);
    TEST(!c.contains("2"));

  }
예제 #7
0
void PyHelpersTest::RunTests()
{
  // Test py::Ptr construction
  {
    {
      // NULL pointer
      PyObject * p  = NULL;
      SHOULDFAIL(py::Ptr(p, /* allowNULL: */false));

      py::Ptr pp1(p, /* allowNULL: */true);
      TEST((PyObject *)pp1 == NULL);
      TEST(pp1.isNULL());
    }

    // Non-NULL pointer
    {
      PyObject * p = PyTuple_New(1);
      py::Ptr pp2(p);
      TEST(!pp2.isNULL());
      TEST((PyObject *)pp2 == p);
      pp2.release();
      TEST(pp2.isNULL());
      Py_DECREF(p);
    }
    
    // assign
    {
      PyObject * p = PyTuple_New(1);
      TEST(p->ob_refcnt == 1);
      py::Ptr pp(NULL, /* allowNULL */ true);
      TEST(pp.isNULL());
      NTA_DEBUG << "*** Before assign";
      pp.assign(p);
      NTA_DEBUG << "*** After assign";
      TEST(p->ob_refcnt == 2);
      TEST(!(pp.isNULL()));
      Py_DECREF(p);
      TEST(p->ob_refcnt == 1);
    }
  }

  // py::String
  {
    py::String ps1(std::string("123"));
    TEST(PyString_Check(ps1) != 0);

    py::String ps2("123", size_t(3));
    TEST(PyString_Check(ps2) != 0);

    py::String ps3("123");
    TEST(PyString_Check(ps3) != 0);

    std::string s1(PyString_AsString(ps1));
    std::string s2(PyString_AsString(ps2));
    std::string s3(PyString_AsString(ps3));
    std::string expected("123");
    TEST(s1 == expected);
    TEST(s2 == expected);
    TEST(s3 == expected);
  
    TEST(std::string(ps1) == expected);
    TEST(std::string(ps2) == expected);
    TEST(std::string(ps3) == expected);

    PyObject * p = PyString_FromString("777");
    py::String ps4(p);
    TEST(std::string(ps4) == std::string("777"));
  }

  // py::Int
  {
    py::Int n1(-5);
    py::Int n2(-6666);
    py::Int n3(long(0));
    py::Int n4(555);
    py::Int n5(6666);
    
    TEST(n1 == -5);
    int x = n2; 
    int expected = -6666;
    TEST(x == expected);
    TEST(n3 == 0);
    TEST(n4 == 555);
    x = n5;
    expected = 6666;
    TEST(x == expected);
  }

  // py::Long
  {
    py::Long n1(-5);
    py::Long n2(-66666666);
    py::Long n3(long(0));
    py::Long n4(555);
    py::Long n5(66666666);
    
    TEST(n1 == -5);
    long x = n2; 
    long expected = -66666666;
    TEST(x == expected);
    TEST(n3 == 0);
    TEST(n4 == 555);
    x = n5;
    expected = 66666666;
    TEST(x == expected);
  }

  // py::UnsignedLong
  {
    py::UnsignedLong n1((unsigned long)(-5));
    py::UnsignedLong n2((unsigned long)(-66666666));
    py::UnsignedLong n3((unsigned long)(0));
    py::UnsignedLong n4(555);
    py::UnsignedLong n5(66666666);
    
    TEST(n1 == (unsigned long)(-5));
    TEST(n2 == (unsigned long)(-66666666));
    TEST(n3 == 0);
    TEST(n4 == 555);
    TEST(n5 == 66666666);
  }

  // py::Float
  {
    TEST(py::Float::getMax() == std::numeric_limits<double>::max());
    TEST(py::Float::getMin() == std::numeric_limits<double>::min());

    py::Float max(std::numeric_limits<double>::max());
    py::Float min(std::numeric_limits<double>::min());
    py::Float n1(-0.5);
    py::Float n2(double(0));
    py::Float n3(333.555);
    py::Float n4(0.02);
    py::Float n5("0.02");
    
    TEST(max == py::Float::getMax());
    TEST(min == py::Float::getMin());
    TEST(n1 == -0.5);
    TEST(n2 == 0);
    TEST(n3 == 333.555);
    TEST(n4 == 0.02);
    TEST(n5 == 0.02);
  }

  // py::Tuple
  {
    py::String s1("item_1");
    py::String s2("item_2");

    // Empty tuple
    {
      py::Tuple empty;
      TEST(PyTuple_Check(empty) != 0);
      TEST(empty.getCount() == 0);
      
      SHOULDFAIL(empty.setItem(0, s1));
      SHOULDFAIL(empty.getItem(0));
    }

    // One item tuple
    {
      py::Tuple t1(1);
      TEST(PyTuple_Check(t1) != 0);
      TEST(t1.getCount() == 1);

      t1.setItem(0, s1);
      py::String item1(t1.getItem(0));
      TEST(std::string(item1) == std::string(s1));
      
      py::String fastItem1(t1.fastGetItem(0));
      TEST(std::string(fastItem1) == std::string(s1));
      fastItem1.release();
      
      SHOULDFAIL(t1.setItem(1, s2));
      SHOULDFAIL(t1.getItem(1));

      TEST(t1.getCount() == 1);
    }

    // 2 items tuple
    {
      py::Tuple t2(2);
      TEST(PyTuple_Check(t2) != 0);
      TEST(t2.getCount() == 2);

      t2.setItem(0, s1);
      py::String item1(t2.getItem(0));
      TEST(std::string(item1) == std::string(s1));
      py::String fastItem1(t2.fastGetItem(0));
      TEST(std::string(fastItem1) == std::string(s1));
      fastItem1.release();

      t2.setItem(1, s2);
      py::String item2(t2.getItem(1));
      TEST(std::string(item2) == std::string(s2));
      py::String fastItem2(t2.fastGetItem(1));
      TEST(std::string(fastItem2) == std::string(s2));
      fastItem2.release();


      SHOULDFAIL(t2.setItem(2, s2));
      SHOULDFAIL(t2.getItem(2));

      TEST(t2.getCount() == 2);
    }
  }

  // py::List
  {
    py::String s1("item_1");
    py::String s2("item_2");

    // Empty list
    {
      py::List empty;
      TEST(PyList_Check(empty) != 0);
      TEST(empty.getCount() == 0);
      
      SHOULDFAIL(empty.setItem(0, s1));
      SHOULDFAIL(empty.getItem(0));
    }

    // One item list
    {
      py::List t1;
      TEST(PyList_Check(t1) != 0);
      TEST(t1.getCount() == 0);

      t1.append(s1);
      py::String item1(t1.getItem(0));
      TEST(std::string(item1) == std::string(s1));
      py::String fastItem1(t1.fastGetItem(0));
      TEST(std::string(fastItem1) == std::string(s1));
      fastItem1.release();

      TEST(t1.getCount() == 1);
      TEST(std::string(item1) == std::string(s1));
      
      SHOULDFAIL(t1.getItem(1));
    }

    // Two items list
    {
      py::List t2;
      TEST(PyList_Check(t2) != 0);
      TEST(t2.getCount() == 0);

      t2.append(s1);
      py::String item1(t2.getItem(0));
      TEST(std::string(item1) == std::string(s1));
      py::String fastItem1(t2.fastGetItem(0));
      TEST(std::string(fastItem1) == std::string(s1));
      fastItem1.release();

      t2.append(s2);
      TEST(t2.getCount() == 2);
      
      py::String item2(t2.getItem(1));
      TEST(std::string(item2) == std::string(s2));
      py::String fastItem2(t2.fastGetItem(1));
      TEST(std::string(fastItem2) == std::string(s2));
      fastItem2.release();


      SHOULDFAIL(t2.getItem(2));
    }
  }

  // py::Dict
  {
    // Empty dict
    {
      py::Dict d;
      TEST(PyDict_Size(d) == 0);

      TEST(d.getItem("blah") == NULL);
    }

    // Failed External PyObject *
    {
      // NULL object
      SHOULDFAIL(py::Dict(NULL));

      // Wrong type (must be a dictionary)
      py::String s("1234");
      try
      {
        py::Dict d(s.release());
        NTA_THROW << "py::Dict d(s) Should fail!!!";
      }
      catch(...)
      {
      }
      // SHOULDFAIL fails to fail :-)
      //SHOULDFAIL(py::Dict(s));
    }

    // Successful external PyObject *
    {

      PyObject * p = PyDict_New();
      PyDict_SetItem(p, py::String("1234"), py::String("5678"));
      
      py::Dict d(p);

      TEST(PyDict_Contains(d, py::String("1234")) == 1);

      PyDict_SetItem(d, py::String("777"), py::String("999"));

      TEST(PyDict_Contains(d, py::String("777")) == 1);

    }
    
    // getItem with default (exisiting and non-exisitng key)
    {
      py::Dict d;
      d.setItem("A", py::String("AAA"));

      PyObject * defaultItem = (PyObject *)123;
      
      py::String A(d.getItem("A"));             
      TEST(std::string(A) == std::string("AAA"));

      // No "B" in the dict, so expect to get the default item
      PyObject * B = (d.getItem("B", defaultItem));
      TEST(B == defaultItem);

      PyDict_SetItem(d, py::String("777"), py::String("999"));
      TEST(PyDict_Contains(d, py::String("777")) == 1);
    }
    
    
    //NTA_DEBUG << ss << ": " << ss->ob_refcnt;
  }

  // py::Module
  {
    py::Module module("sys");
    TEST(std::string(PyModule_GetName(module)) == std::string("sys"));
  }

  // py::Class
  {
    py::Class c("datetime", "date");
  }

  // py::Instance
  {
    
    py::Tuple args(3);
    args.setItem(0, py::Long(2000));
    args.setItem(1, py::Long(11));
    args.setItem(2, py::Long(5));
    py::Instance date("datetime", "date", args, py::Dict());

    // Test invoke()
    {
      py::String result(date.invoke("__str__", py::Tuple(), py::Dict()));
      std::string res((const char *)result);
      std::string expected("2000-11-05");
      TEST(res == expected);
    }

    // Test hasAttr()
    {
      py::String result(date.invoke("__str__", py::Tuple(), py::Dict()));
      std::string res((const char *)result);
      std::string expected("2000-11-05");
      TEST(!(date.hasAttr("No such attribute")));
      TEST(date.hasAttr("year"));
    }

    // Test getAttr()
    {
      py::Int year(date.getAttr("year"));
      TEST(2000 == long(year));
    }

    // Test toString()
    {
      std::string res((const char *)py::String(date.toString()));
      std::string expected("2000-11-05");
      TEST(res == expected);
    }
  }

  // Test custom exception
  {
    py::Tuple args(1);
    args.setItem(0, py::String("error message!"));
    py::Instance e(PyExc_RuntimeError, args);
    e.setAttr("traceback", py::String("traceback!!!"));

    PyErr_SetObject(PyExc_RuntimeError, e);

    try
    {
      py::checkPyError(0);
    }
    catch (const nta::Exception & e)
    {
      NTA_DEBUG << e.getMessage();
    }
  }
}
예제 #8
0
  void DimensionsTest::RunTests()
  {
    Coordinate zero; // [0];
    zero.push_back(0);

    Coordinate one_two; // [1,2]
    one_two.push_back(1);
    one_two.push_back(2);

    Coordinate three_four; // [3,4]
    three_four.push_back(3);
    three_four.push_back(4);
    

    {
      // empty dimensions (unspecified)
      Dimensions d;
      TEST(d.isUnspecified());
      TEST(d.isValid());
      TEST(!d.isDontcare());
      SHOULDFAIL(d.getCount());
      SHOULDFAIL(d.getDimension(0));
      TESTEQUAL("[unspecified]", d.toString());
      SHOULDFAIL(d.getIndex(one_two));
      SHOULDFAIL(d.getCount());
      SHOULDFAIL(d.getDimension(0));
      TESTEQUAL((unsigned int)0, d.getDimensionCount());
    }

    {
      // dontcare dimensions [0]
      Dimensions d;
      d.push_back(0);
      TEST(!d.isUnspecified());
      TEST(d.isDontcare());
      TEST(d.isValid());
      TESTEQUAL("[dontcare]", d.toString());
      SHOULDFAIL(d.getIndex(zero));
      SHOULDFAIL(d.getCount());
      TESTEQUAL((unsigned int)0, d.getDimension(0));
      TESTEQUAL((unsigned int)1, d.getDimensionCount());
    }


    {
      // invalid dimensions
      Dimensions d;
      d.push_back(1);
      d.push_back(0);
      TEST(!d.isUnspecified());
      TEST(!d.isDontcare());
      TEST(!d.isValid());
      TESTEQUAL("[1 0] (invalid)", d.toString());
      SHOULDFAIL(d.getIndex(one_two));
      SHOULDFAIL(d.getCount());
      TESTEQUAL((unsigned int)1, d.getDimension(0));
      TESTEQUAL((unsigned int)0, d.getDimension(1));
      SHOULDFAIL(d.getDimension(2));
      TESTEQUAL((unsigned int)2, d.getDimensionCount());
    }

    {
      // valid dimensions [2,3]
      // two rows, three columns
      Dimensions d;
      d.push_back(2);
      d.push_back(3);
      TEST(!d.isUnspecified());
      TEST(!d.isDontcare());
      TEST(d.isValid());
      TESTEQUAL("[2 3]", d.toString());
      TESTEQUAL((unsigned int)2, d.getDimension(0));
      TESTEQUAL((unsigned int)3, d.getDimension(1));
      SHOULDFAIL(d.getDimension(2));
      TESTEQUAL((unsigned int)6, d.getCount());
      TESTEQUAL((unsigned int)5, d.getIndex(one_two));
      TESTEQUAL((unsigned int)2, d.getDimensionCount());
    }

    {
      //check a two dimensional matrix for proper x-major ordering
      std::vector<size_t> x;
      x.push_back(4);
      x.push_back(5);
      Dimensions d(x);
      size_t testDim1 = 4;
      size_t testDim2 = 5;
      for(size_t i = 0; i < testDim1; i++)
      {
        for(size_t j = 0; j < testDim2; j++)
        {
          Coordinate testCoordinate;
          testCoordinate.push_back(i);
          testCoordinate.push_back(j);

          TESTEQUAL(i+j*testDim1, d.getIndex(testCoordinate));
          TESTEQUAL(vecToString(testCoordinate),
                    vecToString(d.getCoordinate(i+j*testDim1)));
        }
      }
    }

    {
      //check a three dimensional matrix for proper x-major ordering
      std::vector<size_t> x;
      x.push_back(3);
      x.push_back(4);
      x.push_back(5);
      Dimensions d(x);
      size_t testDim1 = 3;
      size_t testDim2 = 4;
      size_t testDim3 = 5;
      for(size_t i = 0; i < testDim1; i++)
      {
        for(size_t j = 0; j < testDim2; j++)
        {
          for(size_t k = 0; k < testDim3; k++)
          {
            Coordinate testCoordinate;
            testCoordinate.push_back(i);
            testCoordinate.push_back(j);
            testCoordinate.push_back(k);

            TESTEQUAL(i +
                      j*testDim1 +
                      k*testDim1*testDim2, d.getIndex(testCoordinate));

            TESTEQUAL(vecToString(testCoordinate),
                      vecToString(d.getCoordinate(i +
                                                  j*testDim1 +
                                                  k*testDim1*testDim2)));
          }
        }
      }
    }

    { 
      // alternate constructor
      std::vector<size_t> x;
      x.push_back(2);
      x.push_back(5);
      Dimensions d(x);
      TEST(!d.isUnspecified());
      TEST(!d.isDontcare());
      TEST(d.isValid());
      
      TESTEQUAL((unsigned int)2, d.getDimension(0));
      TESTEQUAL((unsigned int)5, d.getDimension(1));
      SHOULDFAIL(d.getDimension(2));
      TESTEQUAL((unsigned int)2, d.getDimensionCount());
    }

  } // RunTests()