PageRenderTime 85ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/src/utf8tests.h

http://ollie.googlecode.com/
C Header | 1031 lines | 383 code | 305 blank | 343 comment | 15 complexity | 805b75128f79c989fdd427ca67292466 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /* This file is part of the Ollie Test Suite
  2. *
  3. * This library is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU Library General Public
  5. * License as published by the Free Software Foundation; either
  6. * version 2 of the License, or (at your option) any later version.
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * Library General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Library General Public License
  14. * along with this library; see the file COPYING. If not, write to
  15. * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  16. * Boston, MA 02110-1301, USA.
  17. *
  18. * Copyright (C) 2007 Derrick J. Wippler <thrawn01@gmail.com>
  19. **/
  20. #include "cxxtest/TestSuite.h"
  21. #include <utf8.h>
  22. #include <iohandle.h>
  23. #include <file.h>
  24. #include <iostream>
  25. #include <fstream>
  26. using namespace std;
  27. // --------------------------------
  28. // Unit Test for buffer.cpp
  29. // --------------------------------
  30. class Utf8Tests : public CxxTest::TestSuite
  31. {
  32. public:
  33. // --------------------------------
  34. // Test the base class mSetError() and mGetError()
  35. // --------------------------------
  36. void testmSetErrormGetError( void ) {
  37. OllieCommon ollie;
  38. ollie.mSetError("This is a test");
  39. TS_ASSERT_EQUALS( ollie.mGetError(), "This is a test" );
  40. }
  41. // --------------------------------
  42. // Create an block
  43. // TODO: Add tests to verify attributes in blocks
  44. // --------------------------------
  45. void testmCreateBlock( void ) {
  46. // Create a new block of data
  47. Utf8Block block;
  48. // Should be empty
  49. TS_ASSERT_EQUALS( block.mIsEmpty(), true );
  50. // Assign some data to the block
  51. block.mSetBlockData( "AAAAABBBBBCCCCCDDDDD", 20 );
  52. // Should Not be empty
  53. TS_ASSERT_EQUALS( block.mIsEmpty(), false );
  54. // Ensure the size is correct
  55. TS_ASSERT_EQUALS( block.mGetSize() , 20 );
  56. // Insert some data into the block
  57. block.mInsert( 10 , "GGGGG", 5 );
  58. // Data should be there
  59. TS_ASSERT_EQUALS( block.mGetBlockData(), "AAAAABBBBBGGGGGCCCCCDDDDD" );
  60. // Ensure the size is correct
  61. TS_ASSERT_EQUALS( block.mGetSize() , 25 );
  62. // Insert data at the begining of the block
  63. block.mInsert( 0 , "12345", 5 );
  64. TS_ASSERT_EQUALS( block.mGetBlockData(), "12345AAAAABBBBBGGGGGCCCCCDDDDD" );
  65. // Ensure the size is correct
  66. TS_ASSERT_EQUALS( block.mGetSize() , 30 );
  67. // Append data to the end of the block
  68. block.mInsert( 30 , "ZZZZZ", 5 );
  69. TS_ASSERT_EQUALS( block.mGetBlockData(), "12345AAAAABBBBBGGGGGCCCCCDDDDDZZZZZ" );
  70. // Ensure the size is correct
  71. TS_ASSERT_EQUALS( block.mGetSize() , 35 );
  72. // Split the block
  73. Utf8Block newBlock = block.mSplit(15);
  74. // The returning block should have the first 15 bytes of data
  75. TS_ASSERT_EQUALS( newBlock.mGetBlockData(), "12345AAAAABBBBB" );
  76. // Ensure the size is correct
  77. TS_ASSERT_EQUALS( newBlock.mGetSize() , 15 );
  78. // The remaining blocks still be in the original block
  79. TS_ASSERT_EQUALS( block.mGetBlockData(), "GGGGGCCCCC" );
  80. // Ensure the size is correct
  81. TS_ASSERT_EQUALS( block.mGetSize() , 20 );
  82. newBlock = block.mTruncate(10);
  83. // The returning block should have the last 10 bytes of data
  84. TS_ASSERT_EQUALS( newBlock.mGetBlockData(), "DDDDDZZZZZ" );
  85. // Ensure the size is correct
  86. TS_ASSERT_EQUALS( newBlock.mGetSize() , 10 );
  87. // Ensure the size is correct
  88. TS_ASSERT_EQUALS( block.mGetSize() , 10 );
  89. // Clear the block of data
  90. block.mClear();
  91. // Block should now be empty
  92. TS_ASSERT_EQUALS( block.mIsEmpty(), true )
  93. }
  94. // --------------------------------
  95. // Assign Attributes to a block TODO: Finish this
  96. // --------------------------------
  97. void testmAssignAttributesBlock( void ) {
  98. // Create a new block of data
  99. Utf8Block block;
  100. // Assign some data to the block
  101. block.mSetBlockData( "This is a test", 14 );
  102. //TODO
  103. //block.mSetAttributes( attr );
  104. }
  105. // --------------------------------
  106. // Create a page of data
  107. // --------------------------------
  108. void testmCreatePage( void ) {
  109. Utf8Block block;
  110. Utf8Page page;
  111. // Set the max page size to 100 bytes
  112. page.mSetTargetPageSize( 100 );
  113. // The max page size should be 100 bytes
  114. TS_ASSERT_EQUALS( page.mGetTargetPageSize( ), 100 );
  115. // Assign some data to the block
  116. block.mSetBlockData( "AAAAABBBBBCCCCCDDDDD", 20 );
  117. // Add the block to the page
  118. Utf8Block::Iterator it = page.mAppendBlock( block );
  119. // Verify the data is there
  120. TS_ASSERT_EQUALS( it->mGetBlockData(), "AAAAABBBBBCCCCCDDDDD" );
  121. // Page size should be 20
  122. TS_ASSERT_EQUALS( page.mGetPageSize() , 20 );
  123. // Page should not be full
  124. TS_ASSERT_EQUALS( page.mIsFull(), false );
  125. // Page should accept 1 more byte
  126. TS_ASSERT_EQUALS( page.mCanAcceptBytes( 1 ), true );
  127. // Page should NOT accept 100 more bytes
  128. TS_ASSERT_EQUALS( page.mCanAcceptBytes( 100 ), false );
  129. char* arrBlockData = new char[100];
  130. memset(arrBlockData, 'G', 100);
  131. Utf8Block block2( arrBlockData, 100 );
  132. // Add the block to the page
  133. page.mAppendBlock( block2 );
  134. // Page size should be 120
  135. TS_ASSERT_EQUALS( page.mGetPageSize() , 120 );
  136. // Page should be full
  137. TS_ASSERT_EQUALS( page.mIsFull(), true );
  138. // Page should NOT accept 1 more byte
  139. TS_ASSERT_EQUALS( page.mCanAcceptBytes( 1 ), false );
  140. // Verify we can iterate thru the blocks in the page
  141. it = page.mBegin();
  142. // Verify the data is there
  143. TS_ASSERT_EQUALS( it->mGetBlockData(), "AAAAABBBBBCCCCCDDDDD" );
  144. // Go to the next block on the page
  145. ++it;
  146. // The next block should contain all G's
  147. TS_ASSERT_EQUALS( it->mGetBlockData().substr(0,10) , "GGGGGGGGGG" );
  148. // Go back to the first block on the page
  149. --it;
  150. // Delete that block
  151. it = page.mDeleteBlock( it );
  152. // The only block in the page should contain all G's
  153. TS_ASSERT_EQUALS( it->mGetBlockData().substr(0,10) , "GGGGGGGGGG" );
  154. // Page size should be 100
  155. TS_ASSERT_EQUALS( page.mGetPageSize() , 100 );
  156. delete arrBlockData;
  157. }
  158. // --------------------------------
  159. // Helper method to create a data page from passed character
  160. // --------------------------------
  161. Utf8Page* createDataPage( char charByte, int offSet ) {
  162. Utf8Block block;
  163. Utf8Page *page = new Utf8Page();
  164. page->mSetTargetPageSize( 100 );
  165. char* arrBlockData = new char[100];
  166. memset(arrBlockData, charByte, 100);
  167. block.mSetBlockData( arrBlockData, 100 );
  168. page->mAppendBlock( block );
  169. page->mSetFileOffSet( offSet );
  170. page->mSetOffSet( offSet );
  171. delete arrBlockData;
  172. return page;
  173. }
  174. // --------------------------------
  175. //
  176. // --------------------------------
  177. void testmPageContainers( void ) {
  178. Utf8PageContainer pages;
  179. // Append the pages as if we were reading from a file
  180. pages.mAppendPage( createDataPage('A', 0) );
  181. pages.mAppendPage( createDataPage('B', 100) );
  182. pages.mAppendPage( createDataPage('C', 200) );
  183. pages.mAppendPage( createDataPage('D', 300) );
  184. TS_ASSERT_EQUALS( pages._longSize, 4 );
  185. // Get an Iterator to the Pages
  186. Utf8Page::Iterator it = pages.mBegin();
  187. // The file offset for this page should be 0
  188. TS_ASSERT_EQUALS( it->mGetFileOffSet(), 0 );
  189. // The offset for this page should be 0
  190. TS_ASSERT_EQUALS( it->mGetOffSet(), 0 );
  191. // Get the first Block in the page
  192. Utf8Block::Iterator itBlock = it->mBegin();
  193. // The block should contain all A's
  194. TS_ASSERT_EQUALS( itBlock->mGetBlockData().substr(0,10) , "AAAAAAAAAA" );
  195. // Check the next Page
  196. ++it;
  197. // The starting offset for this page should be 100
  198. TS_ASSERT_EQUALS( it->mGetFileOffSet(), 100 );
  199. // Get the first block in the page
  200. itBlock = it->mBegin();
  201. // The block should contain all B's
  202. TS_ASSERT_EQUALS( itBlock->mGetBlockData().substr(0,10) , "BBBBBBBBBB" );
  203. // Insert a new page
  204. pages.mInsertPage( it , createDataPage('E', 0) );
  205. TS_ASSERT_EQUALS( pages._longSize, 5 );
  206. // Move to the newly inserted page
  207. it--;
  208. // Get the first block in the page
  209. itBlock = it->mBegin();
  210. // The block should contain all E's
  211. TS_ASSERT_EQUALS( itBlock->mGetBlockData().substr(0,10) , "EEEEEEEEEE" );
  212. // TODO: Add tests for mDeletePage()
  213. }
  214. // --------------------------------
  215. // Create a new empty buffer
  216. // --------------------------------
  217. void testmCreateEmptyUtf8Buffer( void ) {
  218. // Create a new buffer
  219. Utf8Buffer* buf = new Utf8Buffer("buffer1");
  220. TS_ASSERT( buf );
  221. // Buffer should have correct name
  222. TS_ASSERT_EQUALS( buf->mGetName() , "buffer1" );
  223. // Buffer should be ready
  224. TS_ASSERT_EQUALS( buf->mIsBufferReady(), true );
  225. delete buf;
  226. }
  227. // --------------------------------
  228. // Create a valid test file in /tmp
  229. // --------------------------------
  230. void testCreateFileForTests( void ) {
  231. fstream ioFile;
  232. ioFile.open(TEST_FILE, fstream::out);
  233. if( ( ! ioFile.is_open() ) || ( ! ioFile.good() ) ) {
  234. TS_FAIL( "Unable to create test file '" TEST_FILE "'");
  235. }
  236. int count=0;
  237. char* cstrBuf = new char[41];
  238. // Null out the array
  239. memset(cstrBuf, 0, 41 );
  240. // Iterate this 10 times
  241. while( count < 10 ) {
  242. int i = 48;
  243. // Each line in the file is 40 charaters of 1 ascii character
  244. // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
  245. // BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
  246. // Create 127 lines of characters
  247. while( i < 127 ) {
  248. memset(cstrBuf, i, 40 );
  249. ioFile << cstrBuf << "\n";
  250. ++i;
  251. }
  252. ++count;
  253. }
  254. ioFile.close();
  255. delete cstrBuf;
  256. }
  257. // --------------------------------
  258. // To Create a new buffer from a file
  259. // --------------------------------
  260. void testmCreateBufferFromFile( void ) {
  261. long longPercent = 0;
  262. // Get the default IO handler for this Operating System
  263. IOHandle* ioHandle = IOHandle::mGetDefaultIOHandler();
  264. TS_ASSERT( ioHandle );
  265. // Open The File ReadWrite
  266. TS_ASSERT_EQUALS( ioHandle->mOpen(TEST_FILE, IOHandle::ReadWrite ), true );
  267. // Create file handler for utf8 files
  268. File* file = new Utf8File( ioHandle );
  269. TS_ASSERT( file );
  270. // Create the buffer and assign this file handle to the buffer
  271. Utf8Buffer* buf = new Utf8Buffer( file );
  272. TS_ASSERT( buf );
  273. // The name should be the same as the file name
  274. TS_ASSERT_EQUALS( buf->mGetName() , TEST_FILE );
  275. // The Buffer should not be full
  276. TS_ASSERT_EQUALS( buf->mBufferFull(), false );
  277. // The Buffer should be ready ( No Loading operation assigned )
  278. TS_ASSERT_EQUALS( buf->mIsBufferReady(), true );
  279. // Tell the buffer we want to load the file
  280. TS_ASSERT_EQUALS( buf->mLoadBuffer(), true );
  281. // The Buffer should NOT be ready ( Need to load file )
  282. TS_ASSERT_EQUALS( buf->mIsBufferReady(), false );
  283. // Status should be "Loading TEST_FILE..."
  284. TS_ASSERT_EQUALS( buf->mGetTaskStatus(), "Loading " TEST_FILE "..." );
  285. // Preform tasks until the buffer is ready
  286. // The current task is to load the file into the buffer
  287. while( ! buf->mIsBufferReady() ) {
  288. // Preform task should return true
  289. TS_ASSERT_EQUALS( buf->mPreformTask(), true );
  290. // Get the progress of the current task
  291. buf->mGetProgress( &longPercent );
  292. TS_ASSERT( longPercent != 0 );
  293. }
  294. // Buffer should be ready
  295. TS_ASSERT_EQUALS( buf->mIsBufferReady(), true );
  296. // Progress should return false, when there is nothing left to do
  297. TS_ASSERT_EQUALS( buf->mGetProgress( &longPercent ), false );
  298. // Precent should be at 100%
  299. TS_ASSERT_EQUALS( longPercent, 100 );
  300. // The Buffer should be the size of our file
  301. TS_ASSERT_EQUALS( buf->mGetBufferSize(), 32390 );
  302. // Get the page iterator
  303. Utf8Page::Iterator it = buf->_pageContainer.mBegin();
  304. // The starting offset for this page should be 0
  305. TS_ASSERT_EQUALS( it->mGetOffSet(), 0 );
  306. TS_ASSERT_EQUALS( it->mGetFileOffSet(), 0 );
  307. // Get the first Block in the page
  308. Utf8Block::Iterator itBlock = it->mBegin();
  309. // The block should contain the file contents
  310. TS_ASSERT_EQUALS( itBlock->mGetBlockData().substr(0,41) , "0000000000000000000000000000000000000000\n" );
  311. // Go to the next page
  312. ++it;
  313. // The starting offset for this page should be the same as the DEFAULT_PAGE_SIZE
  314. TS_ASSERT_EQUALS( it->mGetOffSet(), DEFAULT_PAGE_SIZE );
  315. TS_ASSERT_EQUALS( it->mGetFileOffSet(), DEFAULT_PAGE_SIZE );
  316. itBlock = it->mBegin();
  317. delete buf;
  318. }
  319. // --------------------------------
  320. // Test mInsert()
  321. // --------------------------------
  322. void testmBufferInsert( void ) {
  323. Attributes attr;
  324. // Create a new Buffer Called "buffer1"
  325. Utf8Buffer* buf = new Utf8Buffer("buffer1", 20 );
  326. TS_ASSERT( buf );
  327. TS_ASSERT_EQUALS( buf->mIsModified(), false );
  328. BufferIterator it = buf->mBegin();
  329. // Asking for a char in an empty buffer should return 0
  330. TS_ASSERT_EQUALS( it.mGetUtf8Char(), 0 );
  331. // Insert text At the begining of the file
  332. buf->mInsert( it, "AAAAAGGGGGDDDDD12345" , 20, attr );
  333. // The buffer should now have a size of 20
  334. TS_ASSERT_EQUALS( buf->mGetBufferSize(), 20 );
  335. // Buffer should report modified
  336. TS_ASSERT_EQUALS( buf->mIsModified(), true );
  337. // Get the first Block in the page
  338. Utf8Block::Iterator itBlock = buf->_pageContainer.mBegin()->mBegin();
  339. // The block should contain the inserted contents
  340. TS_ASSERT_EQUALS( itBlock->mGetBlockData() , "AAAAAGGGGGDDDDD12345" );
  341. // Move the iterator up to the end of our insert
  342. TS_ASSERT_EQUALS( it.mNext( 20 ) , true );
  343. // Iterator should point past the last character
  344. TS_ASSERT_EQUALS( it.mGetUtf8Char(), 0 );
  345. // Insert At the end of the current text
  346. buf->mInsert( it, "CCCCCZZZZZXXXXX12345" , 20, attr );
  347. // The Page should be split with the first half of the page with
  348. // the first insert and the second half with the other insert
  349. Utf8Page::Iterator itPage = buf->_pageContainer.mBegin();
  350. itBlock = itPage->mBegin();
  351. // This page should start at the begining of the buffer
  352. TS_ASSERT_EQUALS( itPage->mGetOffSet() , 0 );
  353. // Inserted data should be there
  354. TS_ASSERT_EQUALS( itBlock->mGetBlockData() , "AAAAAGGGGGDDDDD12345" );
  355. TS_ASSERT_EQUALS( itBlock->mGetSize() , 20 );
  356. // If we move to the next block that should be the end of the buffer
  357. TS_ASSERT( ++itBlock == itPage->mEnd() );
  358. // Get the next Page
  359. ++itPage;
  360. // This page should not be the end of the container
  361. TS_ASSERT( itPage != buf->_pageContainer.mEnd() );
  362. // This page should start at offset 20
  363. TS_ASSERT_EQUALS( itPage->mGetOffSet() , 20 );
  364. // Get the block
  365. itBlock = itPage->mBegin();
  366. // This block should have the second insert
  367. TS_ASSERT_EQUALS( itBlock->mGetBlockData() , "CCCCCZZZZZXXXXX12345" );
  368. TS_ASSERT_EQUALS( itBlock->mGetSize() , 20 );
  369. delete buf;
  370. }
  371. // --------------------------------
  372. // Test BufferIteratorClass
  373. // TODO: Add tests for reverse iterator
  374. // ( For languages that insert right to left instead of left to right )
  375. // TODO: After we add attribute support, test iterator movement between blocks
  376. // --------------------------------
  377. void testmBufferIterator( void ) {
  378. long longPercent = 0;
  379. Attributes attr;
  380. // Create a new Buffer Called "buffer1"
  381. Utf8Buffer* buf = new Utf8Buffer("buffer1", 100);
  382. TS_ASSERT( buf );
  383. // Get the iterator for this buffer
  384. BufferIterator it = buf->mBegin();
  385. // Asking for the first character in an empty buffer should return 0
  386. TS_ASSERT_EQUALS( it.mGetUtf8Char(), 0 );
  387. // Move the iterator to a position with no data will return false
  388. // and the iterator will not move
  389. TS_ASSERT_EQUALS( it.mNext(), false );
  390. // mGetError() should tell us what happend
  391. TS_ASSERT_EQUALS( it.mGetError(), "Buffer Error: Requested OffSet in buffer out of bounds" );
  392. // Insert text At the begining of the file
  393. BufferIterator itNew = buf->mInsert( it, "A1234GGGGGDDDDDEFGHB" , 20, attr );
  394. // New iterator should point to the end of the buffer, rdy for the next insert
  395. TS_ASSERT( itNew == buf->mEnd() );
  396. // The first character in the buffer should be an 'A'
  397. TS_ASSERT_EQUALS( it.mGetUtf8Char(), 'A' );
  398. TS_ASSERT_EQUALS( string( it.mGetUtf8String( 20 ) ), "A1234GGGGGDDDDDEFGHB" );
  399. // Move to the next char in the buffer
  400. TS_ASSERT_EQUALS( it.mNext(), true );
  401. // Get the character the iterator points 2
  402. TS_ASSERT_EQUALS( it.mGetUtf8Char(), '1' );
  403. // Get the first 10 chars in the buffer
  404. TS_ASSERT_EQUALS( string( it.mGetUtf8String( 10 ) ), "1234GGGGGD" );
  405. // Move the iterator 18 positions down in the buffer
  406. TS_ASSERT_EQUALS( it.mNext(18), true );
  407. // Get the character the iterator points 2
  408. TS_ASSERT_EQUALS( it.mGetUtf8Char(), 'B' );
  409. // Get 10 chars in the buffer, String returned only returns
  410. // the last char since this is the end of the buffer
  411. TS_ASSERT_EQUALS( string( it.mGetUtf8String( 10 ) ), "B" );
  412. // Move the cursor next 1 position in the buffer ( Just after the last char )
  413. TS_ASSERT_EQUALS( it.mNext(), true );
  414. // Get the character the iterator points 2 ( Should be nothing )
  415. TS_ASSERT_EQUALS( it.mGetUtf8Char(), 0 );
  416. // Try to move past the last char in the buffer
  417. TS_ASSERT_EQUALS( it.mNext(), false );
  418. // Should be an error
  419. TS_ASSERT_EQUALS( it.mGetError(), "Buffer Error: Requested OffSet in buffer out of bounds" );
  420. // Iterator still points to 21th position
  421. TS_ASSERT_EQUALS( it.mGetUtf8Char(), 0 );
  422. // Iterator still points to 21th position
  423. TS_ASSERT_EQUALS( string( it.mGetUtf8String( 10 ) ), "" );
  424. // The Iterator should be at the end of the buffer
  425. TS_ASSERT( it == buf->mEnd() );
  426. // Make a copy of the iterator
  427. BufferIterator itCopy = it;
  428. // Both iterators should be equal
  429. TS_ASSERT( itCopy == it );
  430. // Move the copy back 1 character
  431. TS_ASSERT_EQUALS( itCopy.mPrev(), true );
  432. // Iterators should not be equal
  433. TS_ASSERT( itCopy != it );
  434. // Should point to the last character in the buffer
  435. TS_ASSERT_EQUALS( itCopy.mGetUtf8Char(), 'B' );
  436. // The first iterator should still
  437. // point beyond the last character in the buffer
  438. TS_ASSERT( it == buf->mEnd() );
  439. // FIXME: Should the iterator have an insertBlock() method?
  440. //BufferIterator itNew = buf->mInsertBlock( it, newBlock );
  441. // TODO Add a tests for mNextBlock()
  442. // TODO Add a tests for mPrevBlock()
  443. delete buf;
  444. }
  445. // --------------------------------
  446. // Test BufferIterator offsets and multipage buffers
  447. // --------------------------------
  448. void testmBufferIteratorOffsets( void ) {
  449. Attributes attr;
  450. // Create a new Buffer Called "buffer1"
  451. Utf8Buffer* buf = new Utf8Buffer("buffer1", 20);
  452. TS_ASSERT( buf );
  453. // Get the iterator for this buffer
  454. BufferIterator it = buf->mBegin();
  455. // Add 4 pages of text
  456. it = buf->mInsert( it, "12345GGGGGDDDDDEFGHB" , 20, attr );
  457. it = buf->mInsert( it, "222221234567890EFGHB" , 20, attr );
  458. it = buf->mInsert( it, "333336789012345EFGHB" , 20, attr );
  459. it = buf->mInsert( it, "44444GGGGGDDDDDEFGHB" , 20, attr );
  460. // Iterator should point to the end of the buffer
  461. TS_ASSERT( it == buf->mEnd() );
  462. // Reset the iterator to the begining of the buffer
  463. it = buf->mBegin();
  464. // Iterator should point to the first char in the buffer
  465. TS_ASSERT_EQUALS( it.mGetUtf8Char(), '1' );
  466. // offset should be 0
  467. TS_ASSERT_EQUALS( it.mGetOffSet( ), 0 );
  468. // Advance forward 29 positions
  469. TS_ASSERT_EQUALS( it.mNext( 29 ), true );
  470. // Iterator should point to the 30th position
  471. TS_ASSERT_EQUALS( it.mGetUtf8Char(), '5' );
  472. TS_ASSERT_EQUALS( it.mGetOffSet( ), 29 );
  473. // Set the iterator to position 50 in the buffer
  474. // ( buffer starts at 0, so position 50 is actually 49 )
  475. TS_ASSERT_EQUALS( it.mSetOffSet( 49 ), true );
  476. // Iterator should point to the 50th position
  477. TS_ASSERT_EQUALS( it.mGetUtf8Char(), '0' );
  478. TS_ASSERT_EQUALS( it.mGetOffSet( ), 49 );
  479. // Move the iterator forward 6 characters
  480. TS_ASSERT_EQUALS( it.mNext( 6 ), true );
  481. // Iterator should point to the 56th position
  482. TS_ASSERT_EQUALS( it.mGetUtf8Char(), 'E' );
  483. TS_ASSERT_EQUALS( it.mGetOffSet( ), 55 );
  484. TS_ASSERT_EQUALS( it.mPrev( 10 ), true );
  485. // Iterator should point to the 46th position
  486. TS_ASSERT_EQUALS( it.mGetUtf8Char(), '6' );
  487. TS_ASSERT_EQUALS( it.mGetOffSet( ), 45 );
  488. delete buf;
  489. }
  490. // --------------------------------
  491. // Test mInsert()
  492. // --------------------------------
  493. void testmBufferInsertSaveAndLoad( void ) {
  494. long longPercent = 0;
  495. Attributes attr;
  496. // Create a new Buffer Called "buffer1"
  497. Utf8Buffer* buf = new Utf8Buffer("Buffer1", 20 );
  498. TS_ASSERT( buf );
  499. TS_ASSERT_EQUALS( buf->mIsModified(), false );
  500. // Get the buffer iterator
  501. BufferIterator it = buf->mBegin();
  502. // Insert text At the begining of the file
  503. it = buf->mInsert( it, "AAAAAGGGGGDDDDDEFGHB" , 20, attr );
  504. // Insert At the end of the current text
  505. buf->mInsert( it, "CCCCCZZZZZXXXXXBBBBB" , 20, attr );
  506. TS_ASSERT_EQUALS( buf->mIsModified(), true );
  507. // Get the default IO handler for this Operating System
  508. IOHandle* ioHandle = IOHandle::mGetDefaultIOHandler();
  509. TS_ASSERT( ioHandle );
  510. // Open The File ReadWrite
  511. TS_ASSERT_EQUALS( ioHandle->mOpen(TEST_FILE, IOHandle::ReadWrite ), true );
  512. File* file = new Utf8File( ioHandle );
  513. TS_ASSERT( file );
  514. // Assign a file to the buffer
  515. TS_ASSERT_EQUALS(buf->mAssignFile(file), true );
  516. // The Assignment updated the buffer name
  517. TS_ASSERT_EQUALS(buf->mGetName(), TEST_FILE );
  518. // Save the buffer to the file
  519. TS_ASSERT_EQUALS(buf->mSaveBuffer(), true );
  520. // Status should be "Saving TEST_FILE..."
  521. TS_ASSERT_EQUALS( buf->mGetTaskStatus(), "Saving " TEST_FILE "..." );
  522. // Preforms the task until the buffer is ready
  523. while( ! buf->mIsBufferReady() ) {
  524. // Preform task should return true
  525. TS_ASSERT_EQUALS( buf->mPreformTask(), true );
  526. // Get the progress of the current task
  527. buf->mGetProgress( &longPercent );
  528. TS_ASSERT( longPercent != 0 );
  529. }
  530. // Progress should return false, when there is nothing left to do
  531. TS_ASSERT_EQUALS( buf->mGetProgress( &longPercent ), false );
  532. // Precent should be at 100%
  533. TS_ASSERT_EQUALS( longPercent, 100 );
  534. // Buffer should be ready
  535. TS_ASSERT_EQUALS( buf->mIsBufferReady(), true );
  536. TS_ASSERT_EQUALS( buf->mIsModified(), false );
  537. // ----------------------------------------------------------
  538. // Now Load the same file we just saved into a second buffer
  539. // ----------------------------------------------------------
  540. // NOTE: The original ioHandle pointer is still owned by the first buffer
  541. // and will get deleted when we delete the first buffer
  542. ioHandle = IOHandle::mGetDefaultIOHandler();
  543. TS_ASSERT( ioHandle );
  544. // Open The File ReadWrite
  545. TS_ASSERT_EQUALS( ioHandle->mOpen(TEST_FILE, IOHandle::ReadWrite ), true );
  546. // Create the buffer and assign this file handle to the buffer
  547. Utf8Buffer* buf2 = new Utf8Buffer( new Utf8File( ioHandle ), 20 );
  548. TS_ASSERT( buf2 );
  549. // Tell the buffer we want to load the file
  550. TS_ASSERT_EQUALS( buf2->mLoadBuffer(), true );
  551. while( ! buf2->mIsBufferReady() ) {
  552. // Preform task should return true
  553. TS_ASSERT_EQUALS( buf2->mPreformTask(), true );
  554. // Get the progress of the current task
  555. buf->mGetProgress( &longPercent );
  556. TS_ASSERT( longPercent != 0 );
  557. }
  558. // Precent should be at 100%
  559. TS_ASSERT_EQUALS( longPercent, 100 );
  560. // Get the iterator for this buffer
  561. BufferIterator it2 = buf2->mBegin();
  562. // The data we load from the file should be the same
  563. TS_ASSERT_EQUALS( string( it2.mGetUtf8String( 40 ) ), "AAAAAGGGGGDDDDDEFGHBCCCCCZZZZZXXXXXBBBBB" );
  564. delete buf;
  565. delete buf2;
  566. }
  567. // --------------------------------
  568. // Helper method to load a buffer for testing
  569. // --------------------------------
  570. Utf8Buffer* loadFileForTest( void ) {
  571. // Open the file
  572. IOHandle* ioHandle = IOHandle::mGetDefaultIOHandler();
  573. ioHandle->mOpen(TEST_FILE, IOHandle::ReadWrite );
  574. // Create a new buffer with the file handle
  575. Utf8Buffer* buf = new Utf8Buffer( new Utf8File( ioHandle ) );
  576. // Load the file
  577. buf->mLoadBuffer();
  578. // continue working till the buffer is loaded
  579. while( ! buf->mIsBufferReady() ) {
  580. TS_ASSERT_EQUALS( buf->mPreformTask(), true );
  581. }
  582. // Return pointer to the loaded buffer
  583. return buf;
  584. }
  585. // --------------------------------
  586. // Test delete buffer text
  587. // --------------------------------
  588. void testDeleteBufferText( void ) {
  589. Attributes attr;
  590. // Create a new Buffer Called "buffer1"
  591. Utf8Buffer* buf = loadFileForTest();
  592. TS_ASSERT( buf );
  593. // Get the iterator for this buffer
  594. BufferIterator it = buf->mBegin();
  595. // Remove the first 10 bytes from the buffer
  596. TS_ASSERT_EQUALS( buf->mDelete( it , 10 ), true );
  597. // Iterator should still point to the begining of the buffer
  598. TS_ASSERT( it == buf->mBegin() );
  599. Utf8Page::Iterator itPage = buf->_pageContainer.mBegin();
  600. Utf8Block::Iterator itBlock = itPage->mBegin();
  601. // Deleted data should be gone
  602. TS_ASSERT_EQUALS( itBlock->mGetBlockData() , "-=qwertyui" );
  603. // Should be the correct size
  604. TS_ASSERT_EQUALS( itBlock->mGetSize() , 10 );
  605. ++itPage;
  606. // This block should still have it's data
  607. TS_ASSERT_EQUALS( itBlock->mGetBlockData() , "op[]asdfghjkl;'zxcvb" );
  608. // Should be the correct size
  609. TS_ASSERT_EQUALS( itBlock->mGetSize() , 20 );
  610. // This page should start at offset 10
  611. TS_ASSERT_EQUALS( itPage->mGetOffSet() , 10 );
  612. // The File OffSet should still be the same
  613. TS_ASSERT_EQUALS( itPage->mGetFileOffSet() , 20 );
  614. // Remove the next 10 bytes, the page is now empty
  615. TS_ASSERT_EQUALS( buf->mDelete( it , 10 ), true );
  616. // NOTE: itPage and itBlock are now in-validated
  617. // Buffer Iterator should still point to the begining of the buffer
  618. TS_ASSERT( it == buf->mBegin() );
  619. itPage = buf->_pageContainer.mBegin();
  620. // The offset should now be 0 ( at the start of the buffer )
  621. TS_ASSERT_EQUALS( itPage->mGetOffSet() , 0 );
  622. // The file offset should still be the same
  623. TS_ASSERT_EQUALS( itPage->mGetFileOffSet() , 20 );
  624. itBlock = itPage->mBegin();
  625. // The second page should now be the first page
  626. TS_ASSERT_EQUALS( itBlock->mGetBlockData() , "op[]asdfghjkl;'zxcvb" );
  627. // Should be the correct size
  628. TS_ASSERT_EQUALS( itBlock->mGetSize() , 20 );
  629. ++itPage;
  630. // This page should start at offset 20
  631. TS_ASSERT_EQUALS( itPage->mGetOffSet() , 20 );
  632. itBlock = itPage->mBegin();
  633. // This block should still have it's data
  634. TS_ASSERT_EQUALS( itBlock->mGetBlockData() , "nm,./QWERTYUIOP[]ASD" );
  635. // Should be the correct size
  636. TS_ASSERT_EQUALS( itBlock->mGetSize() , 20 );
  637. // Go to position 5
  638. TS_ASSERT_EQUALS( it.mSetOffSet( 4 ), true );
  639. // Remove the next 10 bytes
  640. TS_ASSERT_EQUALS( buf->mDelete( it , 10 ), true );
  641. // This block should be missing the 10 bytes starting at offset 4
  642. TS_ASSERT_EQUALS( itBlock->mGetBlockData() , "nm,./[]ASD" );
  643. // Should be the correct size
  644. TS_ASSERT_EQUALS( itBlock->mGetSize() , 10 );
  645. delete buf;
  646. }
  647. // --------------------------------
  648. // Test delete buffer accross pages
  649. // --------------------------------
  650. void testDeleteBufferTextPages( void ) {
  651. Attributes attr;
  652. // Create a new Buffer Called "buffer1"
  653. Utf8Buffer* buf = new Utf8Buffer("buffer1", 20);
  654. TS_ASSERT( buf );
  655. // Get the iterator for this buffer
  656. BufferIterator it = buf->mBegin();
  657. // Add 4 pages of text
  658. it = buf->mInsert( it, "AAAAAAAAAAAAAAAAAAAA" , 20, attr );
  659. it = buf->mInsert( it, "BBBBBBBBBBBBBBBBBBBB" , 20, attr );
  660. it = buf->mInsert( it, "CCCCCCCCCCCCCCCCCCCC" , 20, attr );
  661. it = buf->mInsert( it, "EEEEEEEEEEEEEEEEEEEE" , 20, attr );
  662. // Reset our iterator to the begining of the buffer
  663. it = buf->mBegin();
  664. // Move the iterator up 6 positions
  665. it.mNext( 6 );
  666. // Remove the next 40 bytes from the buffer
  667. TS_ASSERT_EQUALS( buf->mDelete( it , 50 ), true );
  668. Utf8Page::Iterator itPage = buf->_pageContainer.mBegin();
  669. Utf8Block::Iterator itBlock = itPage->mBegin();
  670. // Deleted data should be gone, and pages 1 and 3 should have merged
  671. TS_ASSERT_EQUALS( itBlock->mGetBlockData() , "AAAAACCCCC" );
  672. // Should be the correct size
  673. TS_ASSERT_EQUALS( itBlock->mGetSize() , 10 );
  674. ++itPage;
  675. // This block should still have it's data
  676. TS_ASSERT_EQUALS( itBlock->mGetBlockData() , "EEEEEEEEEEEEEEEEEEEE" );
  677. // Should be the correct size
  678. TS_ASSERT_EQUALS( itBlock->mGetSize() , 20 );
  679. // This page should start at offset 10
  680. TS_ASSERT_EQUALS( itPage->mGetOffSet() , 10 );
  681. }
  682. // --------------------------------
  683. // Test changeset Insert recording
  684. // --------------------------------
  685. void testChangeSetInsertRecording( void ) {
  686. // Create a new Buffer Called "buffer1"
  687. Utf8Buffer* buf = new Utf8Buffer("bufferOne");
  688. TS_ASSERT(buf);
  689. // Insert "Derrick J. Wippler"
  690. //TS_ASSERT( buf->mInsert("Derrick J. Wippler") );
  691. /*ChangeSet* changeSet = buf->mGetChangeSet();
  692. TS_ASSERT( changeSet );
  693. TS_ASSERT_EQUALS( changeSet->mIsDelete(), false );
  694. TS_ASSERT_EQUALS( changeSet->mIsInsert(), true );
  695. TS_ASSERT_EQUALS( changeSet->mGetText(), "Derrick J. Wippler" );
  696. TS_ASSERT_EQUALS( changeSet->mGetAbsPosition(), 1);
  697. TS_ASSERT_EQUALS( changeSet->mGetLineNum(), 1);
  698. TS_ASSERT_EQUALS( changeSet->mGetStartPos(), 1);
  699. TS_ASSERT_EQUALS( changeSet->mGetEndPos(), 18);
  700. delete changeSet;*/
  701. }
  702. // --------------------------------
  703. // Test changeset Delete recording
  704. // --------------------------------
  705. void testChangeSetDeleteRecording( void ) {
  706. // Create a new Buffer Called "buffer1"
  707. Utf8Buffer* buf = new Utf8Buffer("bufferOne");
  708. TS_ASSERT(buf);
  709. // Insert "Derrick J. Wippler" without returning a change set
  710. //TS_ASSERT( buf->mInsert("Derrick J. Wippler") );
  711. // Delete " J. Wippler"
  712. //TS_ASSERT( buf->mDelete(8,18) );
  713. /*ChangeSet* changeSet = buf->mGetChangeSet();
  714. TS_ASSERT( changeSet );
  715. TS_ASSERT_EQUALS( changeSet->mIsInsert(), false );
  716. TS_ASSERT_EQUALS( changeSet->mIsDelete(), true );
  717. TS_ASSERT_EQUALS( changeSet->mGetText(), " J. Wippler" );
  718. TS_ASSERT_EQUALS( changeSet->mGetAbsPosition(), 8);
  719. TS_ASSERT_EQUALS( changeSet->mGetLineNum(), 1);
  720. TS_ASSERT_EQUALS( changeSet->mGetStartPos(), 8);
  721. TS_ASSERT_EQUALS( changeSet->mGetEndPos(), 18);
  722. delete changeSet;*/
  723. }
  724. };