AyushP123

tests

Jun 7th, 2017
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.44 KB | None | 0 0
  1. DEFINE_TESTCASE(set_item_weight, backend) {
  2.     // open the database (in this case a simple text file
  3.     // we prepared earlier)
  4.  
  5.     Xapian::Database mydb(get_database("apitest_onedoc"));
  6.  
  7.     Xapian::Enquire enquire(mydb);
  8.  
  9.     // make a simple query, with one word in it - "word".
  10.     enquire.set_query(Xapian::Query("word"));
  11.  
  12.     // retrieve the top ten results (we only expect one)
  13.     Xapian::MSet mymset = enquire.get_mset(0, 10);
  14.  
  15.     vector<double> weights;
  16.     weights.push_back(-1);
  17.     mymset.replace_weights(weights.begin(), weights.end());
  18.     Xapian::MSetIterator i = mymset.begin();
  19.     TEST_EQUAL_DOUBLE_(i.get_weight(), double(-1));
  20.     TEST_EQUAL_DOUBLE_(mymset.get_max_attained(), double(-1));
  21.  
  22.     return true;
  23. }
  24.  
  25. DEFINE_TESTCASE(set_wrong_item_weights, backend) {
  26.     // open the database (in this case a simple text file
  27.     // we prepared earlier)
  28.  
  29.     Xapian::Database mydb(get_database("apitest_onedoc"));
  30.  
  31.     Xapian::Enquire enquire(mydb);
  32.  
  33.     // make a simple query, with one word in it - "word".
  34.     enquire.set_query(Xapian::Query("word"));
  35.  
  36.     // retrieve the top ten results (we only expect one)
  37.     Xapian::MSet mymset = enquire.get_mset(0, 10);
  38.  
  39.     vector<double> weights;
  40.     weights.push_back(-1);
  41.     weights.push_back(-2);
  42.     mymset.replace_weights(weights.begin(), weights.end());
  43.  
  44.     return true;
  45. }
  46.  
  47. DEFINE_TESTCASE(sort_by_relevance, backend) {
  48.     // open the database (in this case a simple text file
  49.     // we prepared earlier)
  50.     Xapian::Database db = get_database("apitest_simpledata");
  51.     Xapian::Enquire enquire(db);
  52.     enquire.set_query(Xapian::Query("word"));
  53.  
  54.     // retrieve the top results
  55.     Xapian::MSet mymset = enquire.get_mset(0, 10);
  56.  
  57.     // We've done the query, now check that the result is what
  58.     // we expect (documents 2 and 4)
  59.  
  60.     // Check the weights
  61.     vector<Xapian::docid> docids;
  62.     Xapian::MSetIterator i = mymset.begin();
  63.     docids.push_back(*i);
  64.     i++;
  65.     docids.push_back(*i);
  66.     vector<double> weights;
  67.     weights.push_back(-2);
  68.     weights.push_back(-1);
  69.     mymset.replace_weights(weights.begin(),weights.end());
  70.     mymset.sort_by_relevance();
  71.     int j = -1, k = 1;
  72.     bool answer = true;
  73.     for (Xapian::MSetIterator m = mymset.begin(); m != mymset.end(); ++m, --j, --k) {
  74.     TEST_EQUAL(m.get_weight(), j);
  75.     TEST_EQUAL(docids[k], *m);
  76.     }
  77.     TEST_EQUAL(mymset.get_max_possible(),double(-1));
  78.     return true;
  79. }
Add Comment
Please, Sign In to add comment