~ubuntu-branches/ubuntu/raring/clucene-core/raring-proposed

« back to all changes in this revision

Viewing changes to src/test/search/TestForDuplicates.cpp

  • Committer: Package Import Robot
  • Author(s): Fathi Boudra
  • Date: 2012-08-11 09:33:38 UTC
  • mfrom: (1.1.5)
  • Revision ID: package-import@ubuntu.com-20120811093338-fgrx41ftqew3qt6a
Tags: 2.3.3.4-1
* New upstream release (Closes: #661703).
* Convert package to multiarch.
* Drop obsolete patches:
  - 01_add_missing_include_bug505667.diff
  - 02_posixness_fix_bug530308.diff
* Add patches:
  - Fixing_ZLIB_configuration_in_shared_CMakeLists.patch
  - Fix-pkgconfig-file-by-adding-clucene-shared-library.patch
  - Install-contribs-lib.patch
  - multiarch.patch
* Update debian/compat: bump to 8.
* Update debian/control:
  - update build dependencies (add cmake, libboost-dev and libz-dev).
  - bump Standards-Version to 3.9.3.
  - rename packages due to ABI bump: libclucene0ldbl -> libclucene-core1.
  - add libclucene-contribs1 package.
* Update debian/rules:
  - rewrite to use CMake.
  - add multiarch support.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*------------------------------------------------------------------------------
 
2
* Copyright (C) 2003-2006 Ben van Klinken and the CLucene Team
 
3
*
 
4
* Distributable under the terms of either the Apache License (Version 2.0) or
 
5
* the GNU Lesser General Public License, as specified in the COPYING file.
 
6
------------------------------------------------------------------------------*/
 
7
#include "test.h"
 
8
 
 
9
  static void print_tHits( CuTest *tc, Hits* hits ) {
 
10
    CuMessageA(tc,"%d total results\n\n", hits->length());
 
11
    for (size_t i = 0 ; i < hits->length(); i++) {
 
12
      if ( i < 10 || (i > 94 && i < 105) ) {
 
13
        const Document& d = hits->doc(i);
 
14
              CuMessage(tc, _T("%d %s\n"), i, d.get(_T("id")) );
 
15
      }
 
16
    }
 
17
  }
 
18
  void testSearchTestForDuplicatesRaw(CuTest *tc){
 
19
                const int MAX_DOCS=1500;
 
20
                const char *strBody[10] = {"test", "value", "why not", "computer", "clucene",
 
21
                        "sun", "program", "main", "database", "code"};
 
22
                RAMDirectory ram;
 
23
 
 
24
                //---
 
25
                WhitespaceAnalyzer an;
 
26
                IndexWriter* writer = _CLNEW IndexWriter(&ram, &an, true);
 
27
                Document *doc = 0;
 
28
 
 
29
                //---
 
30
                TCHAR strDb[1024];
 
31
                //printf("Indexing, please wait...\n");
 
32
                for (int32_t i = 0; i < MAX_DOCS; i++) {
 
33
                        //****
 
34
                        //printf("%d/%d=%s\n", i, MAX_DOCS,strBody[i%10]);
 
35
                        doc = _CLNEW Document();
 
36
 
 
37
                        //---
 
38
                        _sntprintf(strDb, 1024, _T("%d"), i);
 
39
                        doc->add( *_CLNEW Field(_T("id"), strDb,Field::STORE_YES | Field::INDEX_UNTOKENIZED) );
 
40
 
 
41
                        STRCPY_AtoT(strDb, strBody[i%10], 1022);
 
42
 
 
43
                        doc->add(*_CLNEW Field(_T("body"), strDb,Field::STORE_NO | Field::INDEX_TOKENIZED) );
 
44
                        //---
 
45
                        writer->addDocument(doc);
 
46
                        _CLDELETE(doc);
 
47
                        //****
 
48
                }
 
49
                //printf("\nDone.\n");
 
50
 
 
51
                //---
 
52
                writer->close();
 
53
                _CLDELETE(writer);
 
54
 
 
55
 
 
56
 
 
57
                IndexSearcher searcher(&ram);
 
58
                //---
 
59
                int32_t dupl = 0;
 
60
                Query* query = QueryParser::parse(_T("test"), _T("body"), &an);
 
61
                Hits* result = searcher.search(query);
 
62
 
 
63
                CLUCENE_ASSERT(result->length()==((int)MAX_DOCS/10));
 
64
 
 
65
                //printf("Building result map...\n");
 
66
                std::map<int32_t, int32_t> resMap;
 
67
                int32_t id;
 
68
                for (size_t j = 0; j < result->length(); j++) {
 
69
                        doc = &result->doc(j);
 
70
 
 
71
                        id = _ttoi(doc->get(_T("id")));
 
72
                        if ( resMap.find(id) ==resMap.end() ) {
 
73
                                resMap.insert( std::pair<int32_t,int32_t>(id, 1));
 
74
                                //printf("Inserted $d\n",id);
 
75
                        } else {
 
76
                                TCHAR tmp[2048];
 
77
                                _sntprintf(tmp,2048,_T("Duplicated result found - Id: %d\n"), id);
 
78
                                CuAssert(tc,tmp,false);
 
79
                                dupl++;
 
80
                        }
 
81
                }
 
82
                //printf("Total duplicated found: %d\n", dupl);
 
83
 
 
84
                //---
 
85
                _CLDELETE(result);
 
86
                _CLDELETE(query);
 
87
                searcher.close();
 
88
                ram.close();
 
89
        }
 
90
 
 
91
   void testSearchTestForDuplicates(CuTest *tc) {
 
92
      RAMDirectory directory;
 
93
      SimpleAnalyzer analyzer;
 
94
      IndexWriter* writer = _CLNEW IndexWriter(&directory, &analyzer, true);
 
95
      const int32_t MAX_DOCS = 255;
 
96
 
 
97
      for (int32_t j = 0; j < MAX_DOCS; j++) {
 
98
                    Document* d = _CLNEW Document();
 
99
                    d->add(*_CLNEW Field(_T("priority"), _T("high"),Field::STORE_YES | Field::INDEX_TOKENIZED));
 
100
                    TCHAR buf[80];
 
101
                    _i64tot(j,buf,10);
 
102
 
 
103
                    d->add(*_CLNEW Field(_T("id"), buf,Field::STORE_YES | Field::INDEX_TOKENIZED));
 
104
                    writer->addDocument(d);
 
105
 
 
106
                    _CLDELETE(d);
 
107
            }
 
108
            writer->close();
 
109
            _CLDELETE(writer);
 
110
 
 
111
      // try a search without OR
 
112
      Searcher* searcher = _CLNEW IndexSearcher( &directory );
 
113
      QueryParser* parser = _CLNEW QueryParser(_T("priority"), &analyzer);
 
114
      Hits* hits = NULL;
 
115
 
 
116
      Query* query = parser->parse(_T("high"));
 
117
      TCHAR* tmp = query->toString(_T("priority"));
 
118
      CuMessage(tc, _T("Query: %s\n"), tmp );
 
119
      _CLDELETE_CARRAY(tmp);
 
120
 
 
121
      hits = searcher->search(query);
 
122
      print_tHits(tc, hits);
 
123
      _CLDELETE(hits);
 
124
      _CLDELETE(query);
 
125
      _CLDELETE(parser);
 
126
 
 
127
      searcher->close();
 
128
      _CLDELETE(searcher);
 
129
 
 
130
 
 
131
 
 
132
      // try a new search with OR
 
133
      searcher = _CLNEW IndexSearcher( &directory );
 
134
      parser = _CLNEW QueryParser(_T("priority"), &analyzer);
 
135
      hits = NULL;
 
136
 
 
137
      query = parser->parse(_T("high OR medium"));
 
138
      tmp = query->toString(_T("priority"));
 
139
      CuMessage(tc, _T("Query: %s\n"), tmp );
 
140
      _CLDELETE_CARRAY(tmp);
 
141
 
 
142
      hits = searcher->search(query);
 
143
      print_tHits(tc, hits);
 
144
      _CLDELETE(hits);
 
145
      _CLDELETE(query);
 
146
      _CLDELETE(parser);
 
147
 
 
148
      searcher->close();
 
149
      _CLDELETE(searcher);
 
150
 
 
151
      directory.close();
 
152
   }
 
153
 
 
154
 
 
155
  CuSuite *testduplicates(void)
 
156
  {
 
157
          CuSuite *suite = CuSuiteNew(_T("CLucene Duplicates Test"));
 
158
 
 
159
    SUITE_ADD_TEST(suite, testSearchTestForDuplicates);
 
160
    SUITE_ADD_TEST(suite, testSearchTestForDuplicatesRaw);
 
161
 
 
162
    return suite;
 
163
  }
 
164
// EOF