int main(int argc, char* argv[]) { Solution s; ListNode n1(1); ListNode n3(3); ListNode n5(5); ListNode n2(2); ListNode n4(4); ListNode n6(6); n1.next = &n3; n3.next = &n5; n2.next = &n4; n4.next = &n6; ListNode *ret1 = s.mergeTwoLists(&n1, &n2); int i = 0; while (ret1 != NULL) { i++; assert(ret1->val == i); ret1 = ret1->next; } i = 2; ret1 = s.mergeTwoLists(&n1, NULL); while (ret1 == NULL) { assert(ret1->val == 2); i += 2; ret1 = ret1->next; } return 0; }
main(int argc, char *argv[]) { node gnd; node n1("n1"); node n2("n2"); node n3("n3"); node n4("n4"); node n5("n5"); node n6("n6"); vdc vcc("vcc", n6, gnd, 5.0); vpulse vin("vin", n1, gnd, 0, 5, 2e-9, 2e-9, 2e-9, 10e-9); r rb1("rb1", n1, n2, 10e3); r rc1("rc1", n6, n3, 1e3); qnd q1("q1", n3, n2, gnd); r rb2("rb2", n3, n4, 10e3); qnd q2("q2", n5, n4, gnd); r rc2("rc2", n6, n5, 1e3); op::monitor = 1; op(); plot::output = *argv; plot::add("n1", "n3", "n5"); tran::tsmin = 1.0e-30; tran::monitor = 1; tran(0.2e-9, 20.0e-9); }
TEST(removeNthFromEnd, caseTest) { Solution s; ListNode n0(0); EXPECT_EQ(NULL, s.removeNthFromEnd(&n0, 1)); ListNode n1(1); ListNode n2(2); n1.next = &n2; ListNode* n3 = s.removeNthFromEnd(&n1, 2); EXPECT_EQ(&n2, n3); EXPECT_EQ(NULL, n3->next); ListNode n4(4); ListNode n5(5); ListNode n6(6); ListNode n7(7); n4.next = &n5; n5.next = &n6; n6.next = &n7; ListNode *n8 = s.removeNthFromEnd(&n4, 4); EXPECT_EQ(5, n8->val); EXPECT_EQ(6, n8->next->val); }
int main(int argc, char const *argv[]) { // 0 // / \ // 1 2 // / \ \ // 3 4 7 // \ / // 5 8 // \ // 6 TreeNode<int> n0(0); TreeNode<int> n1(1); TreeNode<int> n2(2); TreeNode<int> n3(3); TreeNode<int> n4(4); TreeNode<int> n5(5); TreeNode<int> n6(6); TreeNode<int> n7(7); TreeNode<int> n8(8); n0.left = &n1; n0.right = &n2; n1.left = &n3; n1.right = &n4; n4.right = &n5; n5.right = &n6; n2.right = &n7; n7.left = &n8; bool* matrix = solve(&n0); delete[] matrix; return 0; }
main() { P1DIR = 0xFF; while(1) { n1(); delay(65000); n2(); delay(65000); n3(); delay(65000); n4(); delay(65000); n5(); delay(65000); n6(); delay(65000); n7(); delay(65000); n8(); delay(65000); n9(); delay(65000); n0(); delay(65000); } }
void TestSortList() { SortList::ListNode node4(1); SortList::ListNode node3(2); node3.next = &node4; SortList::ListNode node2(3); node2.next = &node3; SortList::ListNode node1(4); node1.next = &node2; SortList::SolutionSortList so; SortList::ListNode *newhead = so.sortList(&node1); node4.next = &node2; node2.next = &node3; node3.next = nullptr; newhead = so.sortList(&node4);//1,3,2 SortList::ListNode n1(1); SortList::ListNode n2(2); n2.next = &n1; SortList::ListNode n3(3); n3.next = &n2; SortList::ListNode n4(4); n4.next = &n3; SortList::ListNode n5(5); n5.next = &n4; newhead = so.sortList(&n5); }
int main() { TreeNode n1(5); TreeNode n2(4); TreeNode n3(8); TreeNode n4(11); TreeNode n5(13); TreeNode n6(4); TreeNode n7(7); TreeNode n8(2); TreeNode n9(1); n1.left = &n2; n1.right = &n3; n2.left = &n4; n4.left = &n7; n4.right = &n8; n3.left = &n5; n3.right = &n6; n6.right = &n9; Solution sln; cout << sln.hasPathSum(&n1, 22) << endl; return 0; }
int main(){ node n1(1); node n2(2); node n3(3); node n4(4); node n5(5); node n6(6); node n7(7); node n8(8); node n9(9); node n10(10);
int main() { // init data TreeNode n6(6); TreeNode n2(2); TreeNode n8(8); TreeNode n0(0); TreeNode n4(4); TreeNode n7(7); TreeNode n9(9); TreeNode n3(3); TreeNode n5(5); n6.left = &n2; n6.right = &n8; n2.left = &n0; n2.right = &n4; n8.left = &n7; n8.right = &n9; n4.left = &n3; n4.right = &n5; // 测试 fillMapParents Solution sol; map<TreeNode*, TreeNode*> mapParents; sol.fillMapParents(&n6, mapParents); sol.printMapParents(mapParents); // 测试 depth sol.printAllNodesDepth(mapParents, &n6); // 测试 traverseUpward TreeNode* tempNode = &n2; sol.traverseUpward(tempNode, 1, mapParents); // 测试 lowestCommonAncestor TreeNode* v = &n2; TreeNode* w = &n4; TreeNode* lca = sol.lowestCommonAncestor(&n6, v, w); cout << endl << v->val << ", " << w->val << " 的 LCA 是 " << lca->val << endl << endl; v = &n2; w = &n8; lca = sol.lowestCommonAncestor(&n6, v, w); cout << endl << v->val << ", " << w->val << " 的 LCA 是 " << lca->val << endl << endl; return 0; }
int main() { typedef DAG::Node<const idpair> INode; // create a set of nodes // the nodes will be kept track of in the Node children and parent collections so // shared_ptr is used. INode n0(idpair(0, 2)); INode n1(idpair(1, 3)); INode n2(idpair(2, 3)); INode n3(idpair(3, 2)); INode n4(idpair(4, 2)); INode n5(idpair(5, 2)); INode n6(idpair(6, 2)); // and now define the polytree // add the directed (parent -> child) branches of the polytree // each link requires an addChild and an addParent n0.addChild(n1); // link between n0 and n1 n1.addChild(n2); // link between n0 and n2 etc n1.addChild(n3); n3.addChild(n6); n3.addChild(n4); n4.addChild(n5); n5.addChild(n6); DAG::BFSRecurseVisitor<INode> bfs; // Start at node 0 for the following examples // Example 1 // BFSVisitor uses an iterative method to traverse // output the Datatype of all children std::cout << std::endl << "TRAVERSE CHILDREN (start Node 0) 1 level" << std::endl; for (auto n : bfs.traverseChildren(n0, 1)) { std::cout << n->value().itype << ":" << n->value().ivalue << std::endl; } std::cout << std::endl << "TRAVERSE UNDIRECTED (start Node 5) 2 levels " << std::endl; for (auto n : bfs.traverseUndirected(n5, 2)) { std::cout << n->value().itype << ":" << n->value().ivalue << std::endl; } std::cout << std::endl << "TRAVERSE CHILDREN (start Node 0) all levels" << std::endl; for (auto n : bfs.traverseChildren(n0)) { std::cout << n->value().itype << ":" << n->value().ivalue << std::endl; } return 0; }
//-------------------------------------------------------------------------- void BasicNodeTests::dummy() { // Check unique identifier is auto incrementing BasicNode n0; BasicNode n1; BasicNode n2; CPPUNIT_ASSERT_EQUAL(0U, n0.id()); CPPUNIT_ASSERT_EQUAL(true, 0U == n0); CPPUNIT_ASSERT_EQUAL(true, n0 == n0); CPPUNIT_ASSERT_EQUAL(1U, n1.id()); CPPUNIT_ASSERT_EQUAL(true, 1U == n1); CPPUNIT_ASSERT_EQUAL(true, n1 == n1); CPPUNIT_ASSERT_EQUAL(true, n0 != n1); CPPUNIT_ASSERT_EQUAL(2U, n2.id()); CPPUNIT_ASSERT_EQUAL(true, 2U == n2); // Check if the unique identifier can be reseted to 0 BasicNode::resetID(); BasicNode n3; BasicNode n4; CPPUNIT_ASSERT_EQUAL(0U, n3.id()); CPPUNIT_ASSERT_EQUAL(true, 0U == n3); CPPUNIT_ASSERT_EQUAL(1U, n4.id()); CPPUNIT_ASSERT_EQUAL(true, 1U == n4); // Check cloning BasicNode* n = n2.clone(); CPPUNIT_ASSERT_EQUAL(2U, n->id()); CPPUNIT_ASSERT_EQUAL(true, 2U == *n); CPPUNIT_ASSERT_EQUAL(true, n2 == *n); CPPUNIT_ASSERT_EQUAL(true, n3 != *n); delete n; // Check constructor by copy n = new BasicNode(n1); CPPUNIT_ASSERT_EQUAL(1U, n->id()); CPPUNIT_ASSERT_EQUAL(true, 1U == *n); delete n; // Check constructor with forced id BasicNode n5(42); CPPUNIT_ASSERT_EQUAL(42U, n5.id()); CPPUNIT_ASSERT_EQUAL(true, 42U == n5); }
int main(int argc, char **argv) { ListNode n5(5, NULL); ListNode n4(4, &n5); ListNode n3(3, &n4); ListNode n2(2, &n3); ListNode n1(1, &n2); ListNode *head = s.removeNthFromEnd(&n1, 2); while (head != NULL) { printf("%d ", head->val); head = head->next; } printf("\n"); return 0; }
int main(){ TreeNode n0(0); TreeNode n1(1); TreeNode n2(2); TreeNode n3(3); TreeNode n4(4); TreeNode n5(5); n0.left = &n1; n0.right = &n2; n1.left = &n3; n1.right = &n4; n4.left = &n5; int res = maxDepth(&n0); printf("%d\n", res); return 0; }
int main(){ TreeNode n1(1); TreeNode n2(2); TreeNode n3(3); TreeNode n4(4); TreeNode n5(5); TreeNode n6(6); n1.left = &n2; //n1.right = &n5; n2.left = &n3; n2.right = &n4; n5.right = &n6; bool r = hasPathSum(&n1, 1); printf("%s\n", r?"true":"false"); return 0; }
int main() { { /* _______6______ / \ ___2__ ___8__ / \ / \ 0 _4 7 9 / \ 3 5 */ Node n1( 6 ); Node n2( 2 ); Node n3( 8 ); Node n4( 0 ); Node n5( 4 ); Node n6( 7 ); Node n7( 9 ); Node n8( 3 ); Node n9( 5 ); n1.left = &n2; n1.right = &n3; n2.left = &n4; n2.right = &n5; n3.left = &n6; n3.right = &n7; n5.left = &n8; n5.right = &n9; const Node * p = lca_bst( &n1, &n8, &n9 ); assert( p == &n5 ); p = lca_bst( &n1, &n2, &n8 ); assert( p == &n2 ); p = lca_bst( &n1, &n7, &n9 ); assert( p == &n1 ); Node n10( 10 ); p = lca_bst( &n1, &n2, &n10 ); assert( p == nullptr ); } return 0; }
int main() { std::vector<ListNode*> lists; readFromFile(lists, "klist.txt"); ListNode n1(1), n2(2), n1a(1), n3(3), n4(4), n5(5), n6(6), *result; n1.next = &n3; n2.next = &n4; n4.next = &n6; //lists.push_back(&n1); //lists.push_back(&n2); //lists.push_back(&n5); std::cout << "Calling solution" << std::endl; Solution s; result = s.mergeKLists(lists); s.print(result); return 0; }
fourth() { n4(xnxt,ynxt,h,w4); fs(xnxt,ynxt); xnxt+=s/2; ggg(xnxt,ynxt,h,wg); aaa(xnxt,ynxt,h,wa); mmm(xnxt,ynxt,h,wm); eee(xnxt,ynxt,h,we); xnxt+=s/2; ooo(xnxt,ynxt,h,wo); vvv(xnxt,ynxt,h,wv); eee(xnxt,ynxt,h,we); rrr(xnxt,ynxt,h,wr); xnxt+=s/2; iii(xnxt,ynxt,h,wi); fff(xnxt,ynxt,h,wf); xnxt+=s/2; yyy(xnxt,ynxt,h,wy); ooo(xnxt,ynxt,h,wo); uuu(xnxt,ynxt,h,wu); xnxt+=s/2; mmm(xnxt,ynxt,h,wm); iii(xnxt,ynxt,h,wi); sss(xnxt,ynxt,h,ws); sss(xnxt,ynxt,h,ws); xnxt+=s/2; n5(xnxt,ynxt,h,w5); xnxt+=s/2; bbb(xnxt,ynxt,h,wb); aaa(xnxt,ynxt,h,wa); lll(xnxt,ynxt,h,wl); lll(xnxt,ynxt,h,wl); ooo(xnxt,ynxt,h,wo); ooo(xnxt,ynxt,h,wo); nnn(xnxt,ynxt,h,wn); sss(xnxt,ynxt,h,ws); }
int main() { // 5 // / \ // 4 8 // / \ // 11 4 TreeNode n1(5); TreeNode n2(4); TreeNode n3(8); TreeNode n4(11); TreeNode n5(4); n1.left = &n2; n1.right = &n3; n2.left = &n4; n3.right = &n5; Solution slt; std::cout << slt.hasPathSum(&n1, 20) << std::endl; std::cout << slt.hasPathSum(&n1, 21) << std::endl; }
int main() { TreeNode n1(3); TreeNode n2(9); TreeNode n3(20); TreeNode n4(15); TreeNode n5(7); n1.left = &n2; n1.right = &n3; n3.left = &n4; n3.right = &n5; Solution sln; auto a = sln.levelOrder(&n1); for(int i = 0; i < a.size(); i++) { for(int j = 0; j < a[i].size(); j++) { cout << a[i][j] << "\t"; } cout << endl; } return 0; }
int main(int argc, const char *argv[]) { typedef std::vector<int> Point; int dimensions = 2; Point p1 = {10, 3}; Point p2 = {3, 8}; Point p3 = {8, 3}; Point p4 = {1, 4}; Point p5 = {5, 1}; Node<Point> n1(p1); Node<Point> n2(p2); Node<Point> n3(p3); Node<Point> n4(p4); Node<Point> n5(p5); Node<Point> nodes[] = {n1, n2, n3, n4, n5}; std::cout << "Source:" << std::endl; for (int i = 0; i < sizeof(nodes) / sizeof(*nodes); ++i) { std::cout << "("; for (int j = 0; j < dimensions; ++j) { std::cout << nodes[i].value[j] << ","; } std::cout << "), "; } std::cout << std::endl; std::cout << "Tree:" << std::endl; KDTree<Point> tree; tree.make_tree(nodes, 5, 2); tree.visualise(); return 0; }
void sctensdigit(int a) //ten's place { s=30; xnxt=400-45; ynxt=100; w9=w8=w4=w6=w5=w2=w3=w7=w0=wk=we=wf=wj=wl=wp=wr=ws=s/2;//1/2 w1=wi=s/4; wg=wc=wh=wn=wb=wu=wt=wv=wx=wy=2*s/3;//2/3 ww=wm=wa=wd=wo=wq=wz=3*s/4;//3/4 h=s;//1 glColor3f(0,0,0); glPointSize(2.0); switch(a) { case 0:n0(xnxt,ynxt,h,w0); xnxt-=(w1+sp); ynxt-=3*s/2; break; case 1:n1(xnxt,ynxt,h,w1); xnxt-=(w1+sp); ynxt-=3*s/2; break; case 2: n2(xnxt,ynxt,h,w2); xnxt-=(w1+sp); ynxt-=3*s/2; break; case 3: n3(xnxt,ynxt,h,w3); xnxt-=(w1+sp); ynxt-=3*s/2; break; case 4: n4(xnxt,ynxt,h,w4); xnxt-=(w1+sp); ynxt-=3*s/2; break; case 5: n5(xnxt,ynxt,h,w5); xnxt-=(w1+sp); ynxt-=3*s/2; break; case 6: n6(xnxt,ynxt,h,w6); xnxt-=(w1+sp); ynxt-=3*s/2; break; case 7: n7(xnxt,ynxt,h,w7); xnxt-=(w1+sp); ynxt-=3*s/2; break; case 8: n8(xnxt,ynxt,h,w8); xnxt-=(w1+sp); ynxt-=3*s/2; break; case 9: n9(xnxt,ynxt,h,w9); xnxt-=(w1+sp); ynxt-=3*s/2; break; default: break; } glFlush(); return ; }
int main(){ Scalar a("a",STRING,"Hello"); Scalar b("b",STRING,"World"); std::cout << " a : " << a << std::endl; std::cout << " b : " << b << std::endl; Scalar c("c",STRING,"Hello"); std::cout << " c : " << c << std::endl; std::cout << " a == b : " << (a == b) << std::endl; std::cout << " a == c : " << (a == c) << std::endl; std::cout << " a < b : " << (a < b) << std::endl; std::cout << " c < a : " << (c < a) << std::endl; std::cout << " b < a : " << (b < a) << std::endl; Scalar ga("ga",BOOLEAN,"M"); Scalar gb("gb",BOOLEAN,"F"); Scalar gc("gc",BOOLEAN,"F"); Scalar d("d",STRING,"M"); std::cout << " ga : " << ga << std::endl; std::cout << " gb : " << gb << std::endl; std::cout << " gc : " << gc << std::endl; std::cout << " d : " << d << std::endl; std::cout << " (ga == gb) : " << (ga == gb) << std::endl; std::cout << " (gb == gc) : " << (gb == gc) << std::endl; std::cout << " (ga == d) : " << (ga == d) << std::endl; std::cout << " (gb < gc) : " << (gb < gc) << std::endl; std::cout << " (ga < gc) : " << (ga < gc) << std::endl; std::cout << " (gc < ga) : " << (gc < ga) << std::endl; std::cout << " (ga < a) : " << (ga < a) << std::endl; std::cout << " (b < gb) : " << (b < gb) << std::endl; std::cout << "Testing Assignment and Copy operators" << std::endl; Scalar copy1(ga); std::cout << " copy1 of ga : " << copy1 << std::endl; std::cout << " name and type : " << copy1.getName() << " = " << copy1.getValueType() << std::endl; Scalar e("e",STRING,"Namaste"); std::cout << " e : " << e << std::endl; e=ga; std::cout << " e=ga : " << e << std::endl; std::cout << " name and type : " << e.getName() << " = " << e.getValueType() << std::endl; e=a; std::cout << " e=a : " << e << std::endl; std::cout << " name and type : " << e.getName() << " = " << e.getValueType() << std::endl; ga=a; std::cout << " ga=a : " << ga << std::endl; std::cout << " name and type : " << ga.getName() << " = " << ga.getValueType() << std::endl; std::cout << "Testing Scalar variables of type ANY" << std::endl; Scalar anyVar("anyVar"); std::cout << " name and type : " << anyVar.getName() << " = " << anyVar.getValueType() << std::endl; std::cout << " anyVar : " << anyVar << std::endl; std::cout << " anyVar < ga : " << (anyVar < ga) << std::endl; std::cout << " a < anyVar : " << (a < anyVar) << std::endl; std::cout << " a == anyVar : " << (a == anyVar) << std::endl; anyVar.set(STRING,"Wow"); std::cout << " anyVar=Wow : " << anyVar << std::endl; Scalar anyVar1("anyVar1"); std::cout << " anyVar1 : " << anyVar1 << std::endl; std::cout << " name and type : " << anyVar1.getName() << " = " << anyVar1.getValueType() << std::endl; anyVar1 = anyVar; std::cout << " anyVar1=anyVar : " << anyVar1 << std::endl; std::cout << " name and type : " << anyVar1.getName() << " = " << anyVar1.getValueType() << std::endl; Scalar anyVar2("anyVar2"); std::cout << " anyVar2 : " << anyVar2 << std::endl; std::cout << " name and type : " << anyVar2.getName() << " = " << anyVar2.getValueType() << std::endl; anyVar = anyVar2; std::cout << " anyVar=anyVar2 : " << anyVar << std::endl; std::cout << " anyVar name and type : " << anyVar.getName() << " = " << anyVar.getValueType() << std::endl; std::cout << " anyVar2 name and type : " << anyVar2.getName() << " = " << anyVar2.getValueType() << std::endl; Scalar anyType("anyType",ANY,"Testing Any"); std::cout << " anyType : " << anyType << std::endl; std::cout << " name and type : " << anyType.getName() << " = " << anyType.getValueType() << std::endl; std::cout << " anyVar2 < anyType : " << (anyVar2 < anyType) << std::endl; std::cout << " anyVar2 == anyType : " << (anyVar2 == anyType) << std::endl; Scalar g1("g1",GENOTYPE,"120/120"); Scalar g2("g2",GENOTYPE,"120/120"); Scalar g3("g3",GENOTYPE,"112/118"); Scalar g4("g4",GENOTYPE,"C/T"); Scalar g5("g5",GENOTYPE,"A/A"); std::cout << " g1 : " << g1 << std::endl; std::cout << " name and type : " << g1.getName() << " = " << g1.getValueType() << std::endl; anyVar2 = g1; std::cout << " anyVar2=g1 : " << anyVar2 << std::endl; std::cout << " name and type : " << anyVar2.getName() << " = " << anyVar2.getValueType() << std::endl; std::cout << " g2 : " << g2 << std::endl; std::cout << " g3 : " << g3 << std::endl; std::cout << " g4 : " << g4 << std::endl; std::cout << " g5 : " << g5 << std::endl; std::cout << " name and type : " << g5.getName() << " = " << g5.getValueType() << std::endl; std::cout << " g1 < g2 : " << (g1 < g2) << std::endl; std::cout << " g2 == g1 : " << (g2 == g1) << std::endl; std::cout << " g5 < g4 : " << (g5 < g4) << std::endl; std::cout << " g2 < g4 : " << (g2 < g4) << std::endl; std::cout << " g2 < e : " << (g2 < e) << std::endl; std::cout << " ga == g2 : " << (ga == g2) << std::endl; Scalar f("f",STRING,"123/125"); Scalar g6Any("g6Any"); std::cout << " g6Any : " << g6Any << std::endl; std::cout << " name and type : " << g6Any.getName() << " = " << g6Any.getValueType() << std::endl; g6Any = f; std::cout << " g6Any=f : " << g6Any << std::endl; std::cout << " name and type : " << g6Any.getName() << " = " << g6Any.getValueType() << std::endl; g3 = g6Any; std::cout << " g3=g6Any : " << g3 << std::endl; std::cout << " name and type : " << g3.getName() << " = " << g3.getValueType() << std::endl; Scalar g7Inv("g7Inv"); g7Inv.set(GENOTYPE,"0/0"); std::cout << " g7Inv : " << g7Inv << std::endl; std::cout << " name and type : " << g7Inv.getName() << " = " << g7Inv.getValueType() << std::endl; Scalar d1("d1",DATE,"1987-08-23"); Scalar d2("d2",DATE,"1987-08"); Scalar d3("d3",DATE,"[1987-1990]"); Scalar d4("d4",DATE,"~2000-02-13"); Scalar d5("d5",DATE,"1948"); Scalar d6Any("d6Any"); std::cout << " d1 : " << d1 << std::endl; std::cout << " name and type : " << d1.getName() << " = " << d1.getValueType() << std::endl; std::cout << " d6Any : " << d6Any << std::endl; std::cout << " name and type : " << d6Any.getName() << " = " << d6Any.getValueType() << std::endl; d6Any = d1; std::cout << " d6Any=d1 : " << d6Any << std::endl; std::cout << " name and type : " << d6Any.getName() << " = " << d6Any.getValueType() << std::endl; std::cout << " d2 : " << d2 << std::endl; std::cout << " d3 : " << d3 << std::endl; std::cout << " d4 : " << d4 << std::endl; std::cout << " d5 : " << d5 << std::endl; Scalar d7Inv("d7Inv"); d7Inv.set(DATE,"2003-[03-04-12"); std::cout << " d7Inv : " << d7Inv << std::endl; std::cout << " name and type : " << d7Inv.getName() << " = " << d7Inv.getValueType() << std::endl; std::cout << " d1 < d2 : " << (d1 < d2) << std::endl; std::cout << " d2 == d1 : " << (d2 == d1) << std::endl; std::cout << " d2 == d3 : " << (d2 == d3) << std::endl; std::cout << " d3 < d2 : " << (d3 < d2) << std::endl; std::cout << " d5 < d4 : " << (d5 < d4) << std::endl; std::cout << " d2 < d4 : " << (d2 < d4) << std::endl; std::cout << " d2 < e : " << (d2 < e) << std::endl; std::cout << " ga == d2 : " << (ga == d2) << std::endl; Scalar d8Copy(d4); std::cout << " Copy of d4 : " << d8Copy << std::endl; std::cout << " name and type : " << d8Copy.getName() << " = " << d8Copy.getValueType() << std::endl; std::cout << " d4 == d8Copy : " << (d4 == d8Copy) << std::endl; Scalar n1("n1",NUMBER,"19-23"); Scalar n2("n2",NUMBER,"18-38"); Scalar n3("n3",NUMBER,"17"); Scalar n4("n4",NUMBER,"~20"); Scalar n5("n5",NUMBER,"48"); Scalar n6Any("n6Any"); std::cout << " n1 : " << n1 << std::endl; std::cout << " name and type : " << n1.getName() << " = " << n1.getValueType() << std::endl; std::cout << " n6Any : " << n6Any << std::endl; std::cout << " name and type : " << n6Any.getName() << " = " << n6Any.getValueType() << std::endl; n6Any = n1; std::cout << " n6Any=n1 : " << n6Any << std::endl; std::cout << " name and type : " << n6Any.getName() << " = " << n6Any.getValueType() << std::endl; std::cout << " n2 : " << n2 << std::endl; std::cout << " n3 : " << n3 << std::endl; std::cout << " n4 : " << n4 << std::endl; std::cout << " n5 : " << n5 << std::endl; Scalar n7Inv("n7Inv"); n7Inv.set(NUMBER,"2003-["); std::cout << " n7Inv : " << n7Inv << std::endl; std::cout << " name and type : " << n7Inv.getName() << " = " << n7Inv.getValueType() << std::endl; std::cout << " n1 < n2 : " << (n1 < n2) << std::endl; std::cout << " n2 == n1 : " << (n2 == n1) << std::endl; std::cout << " n2 == n3 : " << (n2 == n3) << std::endl; std::cout << " n3 < n2 : " << (n3 < n2) << std::endl; std::cout << " n5 < n4 : " << (n5 < n4) << std::endl; std::cout << " n2 < n4 : " << (n2 < n4) << std::endl; std::cout << " n2 < e : " << (n2 < e) << std::endl; std::cout << " ga == n2 : " << (ga == n2) << std::endl; Scalar n8Copy(n4); std::cout << " Copy of n4 : " << n8Copy << std::endl; std::cout << " name and type : " << n8Copy.getName() << " = " << n8Copy.getValueType() << std::endl; std::cout << " n4 == n8Copy : " << (n4 == n8Copy) << std::endl; Number::addNumberMissingValue("99"); std::cout << "Added 99 as Number missing value" << std::endl; Scalar n8("n8",NUMBER,"99"); std::cout << " n8=99 : " << n8 << std::endl; return 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(); } } }
void dmz::StarfighterPluginSpaceBoxOSG::_create_box () { const String ImageName (_rc.find_file (_imgRc)); osg::ref_ptr<osg::Image> img = (ImageName ? osgDB::readImageFile (ImageName.get_buffer ()) : 0); if (img.valid ()) { osg::Geode* geode = new osg::Geode (); osg::Geometry* geom = new osg::Geometry; osg::Vec4Array* colors = new osg::Vec4Array; colors->push_back (osg::Vec4 (1.0f, 1.0f, 1.0f, 1.0f)); geom->setColorArray (colors); geom->setColorBinding (osg::Geometry::BIND_OVERALL); osg::StateSet *stateset = geom->getOrCreateStateSet (); stateset->setMode (GL_BLEND, osg::StateAttribute::ON); #if 0 osg::ref_ptr<osg::Material> material = new osg::Material; material->setEmission ( osg::Material::FRONT_AND_BACK, osg::Vec4 (1.0, 1.0, 1.0, 1.0)); stateset->setAttributeAndModes (material.get (), osg::StateAttribute::ON); #endif osg::Texture2D *tex = new osg::Texture2D (img.get ()); tex->setWrap (osg::Texture2D::WRAP_S, osg::Texture2D::REPEAT); tex->setWrap (osg::Texture2D::WRAP_T, osg::Texture2D::REPEAT); stateset->setTextureAttributeAndModes (0, tex, osg::StateAttribute::ON); stateset->setAttributeAndModes (new osg::CullFace (osg::CullFace::BACK)); osg::Vec3Array *vertices = new osg::Vec3Array; osg::Vec2Array *tcoords = new osg::Vec2Array; osg::Vec3Array* normals = new osg::Vec3Array; const float Off (_offset); osg::Vec3 v1 (-Off, -Off, -Off); osg::Vec3 v2 ( Off, -Off, -Off); osg::Vec3 v3 ( Off, Off, -Off); osg::Vec3 v4 (-Off, Off, -Off); osg::Vec3 v5 (-Off, -Off, Off); osg::Vec3 v6 ( Off, -Off, Off); osg::Vec3 v7 ( Off, Off, Off); osg::Vec3 v8 (-Off, Off, Off); osg::Vec3 n1 ( 1.0, 1.0, 1.0); osg::Vec3 n2 (-1.0, 1.0, 1.0); osg::Vec3 n3 (-1.0, -1.0, 1.0); osg::Vec3 n4 ( 1.0, -1.0, 1.0); osg::Vec3 n5 ( 1.0, 1.0, -1.0); osg::Vec3 n6 (-1.0, 1.0, -1.0); osg::Vec3 n7 (-1.0, -1.0, -1.0); osg::Vec3 n8 ( 1.0, -1.0, -1.0); n1.normalize (); n2.normalize (); n3.normalize (); n4.normalize (); n5.normalize (); n6.normalize (); n7.normalize (); n8.normalize (); // const float F1 (5.0f); // const float F2 (2.5f); const float F1 (5.0f); const float F2 (2.5f); int count = 0; // 1 vertices->push_back (v1); vertices->push_back (v2); vertices->push_back (v3); vertices->push_back (v4); tcoords->push_back (osg::Vec2 (0.0, 0.0)); tcoords->push_back (osg::Vec2 (0.0, F1)); tcoords->push_back (osg::Vec2 (F2, F1)); tcoords->push_back (osg::Vec2 (F2, 0.0)); normals->push_back (n1); normals->push_back (n2); normals->push_back (n3); normals->push_back (n4); count += 4; // 2 vertices->push_back (v4); vertices->push_back (v3); vertices->push_back (v7); vertices->push_back (v8); tcoords->push_back (osg::Vec2 (0.0, 0.0)); tcoords->push_back (osg::Vec2 (0.0, F1)); tcoords->push_back (osg::Vec2 (F2, F1)); tcoords->push_back (osg::Vec2 (F2, 0.0)); normals->push_back (n4); normals->push_back (n3); normals->push_back (n7); normals->push_back (n8); count += 4; // 3 vertices->push_back (v8); vertices->push_back (v7); vertices->push_back (v6); vertices->push_back (v5); tcoords->push_back (osg::Vec2 (0.0, 0.0)); tcoords->push_back (osg::Vec2 (0.0, F1)); tcoords->push_back (osg::Vec2 (F2, F1)); tcoords->push_back (osg::Vec2 (F2, 0.0)); normals->push_back (n8); normals->push_back (n7); normals->push_back (n6); normals->push_back (n5); count += 4; // 4 vertices->push_back (v5); vertices->push_back (v6); vertices->push_back (v2); vertices->push_back (v1); tcoords->push_back (osg::Vec2 (0.0, 0.0)); tcoords->push_back (osg::Vec2 (0.0, F1)); tcoords->push_back (osg::Vec2 (F2, F1)); tcoords->push_back (osg::Vec2 (F2, 0.0)); normals->push_back (n5); normals->push_back (n6); normals->push_back (n2); normals->push_back (n1); count += 4; // 5 vertices->push_back (v3); vertices->push_back (v2); vertices->push_back (v6); vertices->push_back (v7); tcoords->push_back (osg::Vec2 (0.0, 0.0)); tcoords->push_back (osg::Vec2 (0.0, F1)); tcoords->push_back (osg::Vec2 (F2, F1)); tcoords->push_back (osg::Vec2 (F2, 0.0)); normals->push_back (n3); normals->push_back (n2); normals->push_back (n6); normals->push_back (n7); count += 4; // 6 vertices->push_back (v1); vertices->push_back (v4); vertices->push_back (v8); vertices->push_back (v5); tcoords->push_back (osg::Vec2 (0.0, 0.0)); tcoords->push_back (osg::Vec2 (0.0, F1)); tcoords->push_back (osg::Vec2 (F2, F1)); tcoords->push_back (osg::Vec2 (F2, 0.0)); normals->push_back (n1); normals->push_back (n4); normals->push_back (n8); normals->push_back (n5); count += 4; geom->setNormalArray (normals); geom->setNormalBinding (osg::Geometry::BIND_PER_VERTEX); geom->addPrimitiveSet (new osg::DrawArrays (GL_QUADS, 0, count)); geom->setVertexArray (vertices); geom->setTexCoordArray (0, tcoords); geode->addDrawable (geom); _box = new osg::MatrixTransform (); _box->addChild (geode); } else { _log.error << "Failed to load: " << _imgRc << ":" << ImageName << endl; } }