Path: blob/main/contrib/libxo/tests/core/test_02.c
39537 views
/*1* Copyright (c) 2014-2019, Juniper Networks, Inc.2* All rights reserved.3* This SOFTWARE is licensed under the LICENSE provided in the4* ../Copyright file. By downloading, installing, copying, or otherwise5* using the SOFTWARE, you agree to be bound by the terms of that6* LICENSE.7* Phil Shafer, July 20148*/910#include <stdio.h>11#include <stdlib.h>12#include <stdint.h>13#include <string.h>1415#include "xo.h"16#include "xo_encoder.h"1718#include "xo_humanize.h"1920int21main (int argc, char **argv)22{23xo_set_program("test_02");2425argc = xo_parse_args(argc, argv);26if (argc < 0)27return 1;2829for (argc = 1; argv[argc]; argc++) {30if (xo_streq(argv[argc], "xml"))31xo_set_style(NULL, XO_STYLE_XML);32else if (xo_streq(argv[argc], "json"))33xo_set_style(NULL, XO_STYLE_JSON);34else if (xo_streq(argv[argc], "text"))35xo_set_style(NULL, XO_STYLE_TEXT);36else if (xo_streq(argv[argc], "html"))37xo_set_style(NULL, XO_STYLE_HTML);38else if (xo_streq(argv[argc], "pretty"))39xo_set_flags(NULL, XOF_PRETTY);40else if (xo_streq(argv[argc], "xpath"))41xo_set_flags(NULL, XOF_XPATH);42else if (xo_streq(argv[argc], "info"))43xo_set_flags(NULL, XOF_INFO);44}4546xo_set_flags(NULL, XOF_UNITS); /* Always test w/ this */47xo_set_file(stdout);4849xo_open_container_h(NULL, "top");5051xo_open_container("data");5253xo_emit("{kt:name/%-*.*s}{eq:flags/0x%x}",545, 5, "em0", 34883);5556xo_emit("{d:/%-*.*s}{etk:name}{eq:flags/0x%x}",575, 5, "em0", "em0", 34883);5859xo_emit("We are {{emit}}{{ting}} some {:what}\n", "braces");6061xo_message("abcdef");62close(-1);63xo_message_e("abcdef");6465xo_message("improper use of profanity; %s; %s",66"ten yard penalty", "first down");6768xo_emit("length {:length/%6.6s}\n", "abcdefghijklmnopqrstuvwxyz");6970close(-1);71xo_emit("close {:fd/%d} returned {:error/%m} {:test}\n", -1, "good");72close(-1);73xo_emit("close {:fd/%d} returned {:error/%6.6m} {:test}\n", -1, "good");747576xo_message("improper use of profanity; %s; %s",77"ten yard penalty", "first down");7879xo_emit(" {:lines/%7ju} {:words/%7ju} "80"{:characters/%7ju} {d:filename/%s}\n",81(uintmax_t) 20, (uintmax_t) 30, (uintmax_t) 40, "file");8283int i;84for (i = 0; i < 5; i++)85xo_emit("{lw:bytes/%d}{Np:byte,bytes}\n", i);8687xo_emit("{Lc:Low\\/warn granularity}{P:\t}{:granularity-lw/%d}{Uw:/%sh}\n",88155, "mA");8990xo_emit("{:mbuf-current/%u}/{:mbuf-cache/%u}/{:mbuf-total/%u} "91"{N:mbufs <&> in use (current\\/cache\\/total)}\n",9210, 20, 30);9394xo_emit("{:distance/%u}{Uw:miles} from {:location}\n", 50, "Boston");95xo_emit("{:memory/%u}{U:k} left out of {:total/%u}{U:kb}\n", 64, 640);96xo_emit("{:memory/%u}{U:/%s} left out of {:total/%u}{U:/%s}\n",9764, "k", 640, "kilobytes");9899xo_emit("{,title:/before%safter:}\n", "working");100101xo_emit("{,display,white,colon:some/%s}"102"{,value:ten/%ju}{,value:eleven/%ju}\n",103"string", (uintmax_t) 10, (uintmax_t) 11);104105xo_emit("{:unknown/%u} "106"{N:/packet%s here\\/there\\/everywhere}\n",1071010, "s");108109xo_emit("{:unknown/%u} "110"{,note:/packet%s here\\/there\\/everywhere}\n",1111010, "s");112113xo_emit("({[:/%d}{n:min/15}/{n:cur/20}/{:max/%d}{]:})\n", 30, 125);114xo_emit("({[:30}{:min/%u}/{:cur/%u}/{:max/%u}{]:})\n", 15, 20, 125);115xo_emit("({[:-30}{n:min/15}/{n:cur/20}/{n:max/125}{]:})\n");116xo_emit("({[:}{:min/%u}/{:cur/%u}/{:max/%u}{]:/%d})\n", 15, 20, 125, -30);117118xo_emit("Humanize: {h:val1/%u}, {h,hn-space:val2/%u}, "119"{h,hn-decimal:val3/%u}, {h,hn-1000:val4/%u}, "120"{h,hn-decimal:val5/%u}\n",12121,12257 * 1024,12396 * 1024 * 1024,124(42 * 1024 + 420) * 1024,1251342172800);126127xo_open_list("flag");128xo_emit("{lq:flag/one} {lq:flag/two} {lq:flag/three}\n");129xo_close_list("flag");130131xo_emit("{n:works/%s}\n", NULL);132133xo_emit("{e:empty-tag/}");134xo_emit("1:{qt:t1/%*d} 2:{qt:t2/test%-*u} "135"3:{qt:t3/%10sx} 4:{qt:t4/x%-*.*s}\n",1366, 1000, 8, 5000, "ten-long", 10, 10, "test");137xo_emit("{E:this is an error}\n");138xo_emit("{E:/%s more error%s}\n", "two", "s" );139xo_emit("{W:this is an warning}\n");140xo_emit("{W:/%s more warning%s}\n", "two", "s" );141xo_emit("{L:/V1\\/V2 packet%s}: {:count/%u}\n", "s", 10);142143int test = 4;144xo_emit("{:test/%04d} {L:/tr%s}\n", test, (test == 1) ? "y" : "ies");145146xo_message("improper use of profanity; %s; %s",147"ten yard penalty", "first down");148149xo_error("Shut 'er down, Clancey! She's a-pumpin' mud! <>!,\"!<>\n");150xo_error("err message (%d)", 1);151xo_error("err message (%d)\n", 2);152xo_errorn("err message (%d)", 1);153xo_errorn("err message (%d)\n", 2);154155xo_close_container("data");156157xo_close_container_h(NULL, "top");158159xo_finish();160161return 0;162}163164165