Import zstd 1.1.4
[freebsd.git] / tests / datagencli.c
1 /**
2  * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
3  * All rights reserved.
4  *
5  * This source code is licensed under the BSD-style license found in the
6  * LICENSE file in the root directory of this source tree. An additional grant
7  * of patent rights can be found in the PATENTS file in the same directory.
8  */
9
10
11 /*-************************************
12 *  Dependencies
13 **************************************/
14 #include "util.h"      /* Compiler options */
15 #include <stdio.h>     /* fprintf, stderr */
16 #include "datagen.h"   /* RDG_generate */
17
18
19 /*-************************************
20 *  Constants
21 **************************************/
22 #define KB *(1 <<10)
23 #define MB *(1 <<20)
24 #define GB *(1U<<30)
25
26 #define SIZE_DEFAULT ((64 KB) + 1)
27 #define SEED_DEFAULT 0
28 #define COMPRESSIBILITY_DEFAULT 50
29
30
31 /*-************************************
32 *  Macros
33 **************************************/
34 #define DISPLAY(...)         fprintf(stderr, __VA_ARGS__)
35 #define DISPLAYLEVEL(l, ...) if (displayLevel>=l) { DISPLAY(__VA_ARGS__); }
36 static unsigned displayLevel = 2;
37
38
39 /*-*******************************************************
40 *  Command line
41 *********************************************************/
42 static int usage(const char* programName)
43 {
44     DISPLAY( "Compressible data generator\n");
45     DISPLAY( "Usage :\n");
46     DISPLAY( "      %s [args]\n", programName);
47     DISPLAY( "\n");
48     DISPLAY( "Arguments :\n");
49     DISPLAY( " -g#    : generate # data (default:%i)\n", SIZE_DEFAULT);
50     DISPLAY( " -s#    : Select seed (default:%i)\n", SEED_DEFAULT);
51     DISPLAY( " -P#    : Select compressibility in %% (default:%i%%)\n", COMPRESSIBILITY_DEFAULT);
52     DISPLAY( " -h     : display help and exit\n");
53     return 0;
54 }
55
56
57 int main(int argc, const char** argv)
58 {
59     double proba = (double)COMPRESSIBILITY_DEFAULT / 100;
60     double litProba = 0.0;
61     U64 size = SIZE_DEFAULT;
62     U32 seed = SEED_DEFAULT;
63     const char* const programName = argv[0];
64
65     int argNb;
66     for(argNb=1; argNb<argc; argNb++) {
67         const char* argument = argv[argNb];
68
69         if(!argument) continue;   /* Protection if argument empty */
70
71         /* Handle commands. Aggregated commands are allowed */
72         if (*argument=='-') {
73             argument++;
74             while (*argument!=0) {
75                 switch(*argument)
76                 {
77                 case 'h':
78                     return usage(programName);
79                 case 'g':
80                     argument++;
81                     size=0;
82                     while ((*argument>='0') && (*argument<='9'))
83                         size *= 10, size += *argument++ - '0';
84                     if (*argument=='K') { size <<= 10; argument++; }
85                     if (*argument=='M') { size <<= 20; argument++; }
86                     if (*argument=='G') { size <<= 30; argument++; }
87                     if (*argument=='B') { argument++; }
88                     break;
89                 case 's':
90                     argument++;
91                     seed=0;
92                     while ((*argument>='0') && (*argument<='9'))
93                         seed *= 10, seed += *argument++ - '0';
94                     break;
95                 case 'P':
96                     argument++;
97                     proba=0.0;
98                     while ((*argument>='0') && (*argument<='9'))
99                         proba *= 10, proba += *argument++ - '0';
100                     if (proba>100.) proba=100.;
101                     proba /= 100.;
102                     break;
103                 case 'L':   /* hidden argument : Literal distribution probability */
104                     argument++;
105                     litProba=0.;
106                     while ((*argument>='0') && (*argument<='9'))
107                         litProba *= 10, litProba += *argument++ - '0';
108                     if (litProba>100.) litProba=100.;
109                     litProba /= 100.;
110                     break;
111                 case 'v':
112                     displayLevel = 4;
113                     argument++;
114                     break;
115                 default:
116                     return usage(programName);
117                 }
118     }   }   }   /* for(argNb=1; argNb<argc; argNb++) */
119
120     DISPLAYLEVEL(4, "Data Generator \n");
121     DISPLAYLEVEL(3, "Seed = %u \n", seed);
122     if (proba!=COMPRESSIBILITY_DEFAULT) DISPLAYLEVEL(3, "Compressibility : %i%%\n", (U32)(proba*100));
123
124     RDG_genStdout(size, proba, litProba, seed);
125     DISPLAYLEVEL(1, "\n");
126
127     return 0;
128 }