encoding_test.cpp

Go to the documentation of this file.
00001 /*
00002  * File Name: encoding_test.cpp
00003  */
00004 
00005 /*
00006  * This file is part of uds-plugin-plaintext.
00007  *
00008  * uds-plugin-plaintext is free software: you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation, either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * uds-plugin-plaintext is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program. If not, see <http://www.gnu.org/licenses/>.
00020  */
00021 
00022 /**
00023  * Copyright (C) 2008 iRex Technologies B.V.
00024  * All rights reserved.
00025  */
00026 
00027 #include <stdio.h>
00028 
00029 #include <string>
00030 #include <cassert>
00031 
00032 #include "tut.h"
00033 #include "tut_reporter.h"
00034 #include "encoding.h"
00035 
00036 #define BUFSIZE 1024
00037 
00038 namespace tut
00039 {
00040     using namespace text;
00041     struct empty
00042     { 
00043     };
00044 
00045     typedef test_group<empty> tf;
00046     typedef tf::object object;
00047     tf interface_ptr_group("encoding test");
00048 
00049     // Test 1: Test GB2312
00050     template<>
00051     template<>
00052     void object::test<1>()
00053     {
00054         std::string file_path = "GB2312";
00055         FILE* fp = fopen(file_path.c_str(), "r");
00056         
00057         if (fp)
00058         {
00059             char buf[BUFSIZE];
00060             size_t bytes_read = fread(buf, 1, BUFSIZE, fp);
00061             fclose(fp);
00062 
00063             // Get encodings
00064             std::string enc = Encoding::get_enc("zh", (unsigned char *)buf, bytes_read);
00065             ensure(enc == file_path);
00066         }
00067         else
00068         {
00069             ensure(false);
00070         }
00071     }
00072 
00073     // Test 2: Test utf-8
00074     template<>
00075     template<>
00076     void object::test<2>()
00077     {
00078         std::string file_path = "UTF-8";
00079         FILE* fp = fopen(file_path.c_str(), "r");
00080         
00081         if (fp)
00082         {
00083             char buf[BUFSIZE];
00084             size_t bytes_read = fread(buf, 1, BUFSIZE, fp);
00085             fclose(fp);
00086 
00087             // Get encodings
00088             std::string enc = Encoding::get_enc("zh", (unsigned char *)buf, bytes_read);
00089             ensure(enc == file_path);
00090         }
00091         else
00092         {
00093             ensure(false);
00094         }
00095     }
00096 }
00097 
00098 using std::exception;
00099 using std::cerr;
00100 using std::endl;
00101 
00102 namespace tut
00103 {
00104     test_runner_singleton runner;
00105 }
00106 
00107 int main()
00108 {
00109     tut::reporter reporter;
00110     tut::runner.get().set_callback(&reporter);
00111 
00112     try
00113     {
00114         tut::runner.get().run_tests();
00115     }
00116     catch (const std::exception& ex)
00117     {
00118         cerr << "tut raised ex: " << ex.what() << endl;
00119         return 1;
00120     }
00121 
00122     return 0;
00123 }
00124 
00125 
Generated by  doxygen 1.6.2-20100208