Use AutoTimer for SystemDependentDataBuffer
[LibreOffice.git] / smoketest / libtest.cxx
blobc23f1d2a992c8e23fd31fbf7b68fa96b459a1b9a
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
10 #include <stdio.h>
11 #include <string.h>
12 #include <malloc.h>
13 #include <assert.h>
14 #include <math.h>
16 #include <LibreOfficeKit/LibreOfficeKitInit.h>
17 #include <LibreOfficeKit/LibreOfficeKit.hxx>
20 #ifdef _WIN32
21 //#include <Windows.h> // come from LibreOfficeKitInit.h
22 static long getTimeMS()
24 return GetTickCount();
27 static bool IsAbsolutePath(char const *pPath)
29 if (pPath[1] != ':')
31 fprintf( stderr, "Absolute path required to libreoffice install\n" );
32 return false;
35 return true;
38 #else
39 #include <sys/time.h>
40 #include <sal/types.h>
41 static long getTimeMS()
43 struct timeval t;
44 gettimeofday(&t, nullptr);
45 return t.tv_sec*1000 + t.tv_usec/1000;
48 static bool IsAbsolutePath(char const *pPath)
50 if (pPath[0] != '/')
52 fprintf( stderr, "Absolute path required to libreoffice install\n" );
53 return false;
56 return true;
58 #endif
61 using namespace ::lok;
64 static int help()
66 fprintf( stderr, "Usage: libtest <absolute-path-to-libreoffice-install> [path to load document] [path to save document].\n" );
67 return 1;
70 int main (int argc, char **argv)
72 long start, end;
74 start = getTimeMS();
76 if (argc < 2 || !strcmp(argv[1], "--help") || !strcmp(argv[1], "-h"))
77 return help();
80 if( !IsAbsolutePath(argv[1]) )
81 return 1;
83 // coverity[tainted_string] - build time test tool
84 char *install_path = argv[1];
86 if( argc > 4 )
88 fprintf( stderr, "testing preinit\n");
89 char *imp_lib;
90 void *dlhandle;
91 dlhandle = lok_dlopen( install_path, &imp_lib );
92 if( !dlhandle )
94 fprintf( stderr, "Failed to link '%s'\n", lok_dlerror() );
95 return -1;
97 LokHookPreInit *preinit = reinterpret_cast<LokHookPreInit *>(lok_dlsym( dlhandle, "lok_preinit" ));
98 if( !preinit )
100 fprintf( stderr, "Failed to find pre-init symbol: %s\n", lok_dlerror() );
101 return -1;
103 preinit(install_path, nullptr);
106 Office *pOffice = lok_cpp_init( install_path );
107 if( !pOffice )
109 fprintf( stderr, "Failed to initialize\n" );
110 return -1;
113 end = getTimeMS();
114 fprintf( stderr, "init time: %ld ms\n", (end-start) );
115 start = end;
117 fprintf( stderr, "start to load document '%s'\n", argv[2] );
118 Document *pDocument = pOffice->documentLoad( argv[2] );
119 if( !pDocument )
121 char *pError = pOffice->getError();
122 fprintf( stderr, "failed to load document '%s': '%s'\n",
123 argv[2], pError );
124 free (pError);
125 return -1;
128 end = getTimeMS();
129 fprintf( stderr, "load time: %ld ms\n", (end-start) );
130 start = end;
132 if( argc > 3 )
134 const char *pFilter = nullptr;
135 if( argc > 4 )
136 pFilter = argv[4];
137 fprintf( stderr, "save document as '%s' (%s)\n", argv[3], pFilter ? pFilter : "<null>" );
138 if( !pDocument->saveAs( argv[3], pFilter ) )
140 char *pError = pOffice->getError();
141 fprintf( stderr, "failed to save document '%s'\n", pError);
142 free (pError);
144 else
146 fprintf( stderr, "Save succeeded\n" );
147 end = getTimeMS();
148 fprintf( stderr, "save time: %ld ms\n", (end-start) );
151 fprintf( stderr, "all tests passed.\n" );
153 delete pDocument;
154 delete pOffice;
156 return 0;
159 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */