tests.c revision 323136
11558Srgrimes/* $OpenBSD: tests.c,v 1.1 2017/03/14 01:20:29 dtucker Exp $ */ 21558Srgrimes/* 31558Srgrimes * Regress test for conversions 41558Srgrimes * 51558Srgrimes * Placed in the public domain 61558Srgrimes */ 71558Srgrimes 81558Srgrimes#include "includes.h" 91558Srgrimes 101558Srgrimes#include <sys/types.h> 111558Srgrimes#include <sys/param.h> 121558Srgrimes#include <stdio.h> 131558Srgrimes#ifdef HAVE_STDINT_H 141558Srgrimes#include <stdint.h> 151558Srgrimes#endif 161558Srgrimes#include <stdlib.h> 171558Srgrimes#include <string.h> 181558Srgrimes 191558Srgrimes#include "../test_helper/test_helper.h" 201558Srgrimes 211558Srgrimes#include "misc.h" 221558Srgrimes 231558Srgrimesvoid 241558Srgrimestests(void) 251558Srgrimes{ 261558Srgrimes char buf[1024]; 271558Srgrimes 281558Srgrimes TEST_START("conversion_convtime"); 291558Srgrimes ASSERT_LONG_EQ(convtime("0"), 0); 30114589Sobrien ASSERT_LONG_EQ(convtime("1"), 1); 311558Srgrimes ASSERT_LONG_EQ(convtime("1S"), 1); 3223675Speter /* from the examples in the comment above the function */ 33114589Sobrien ASSERT_LONG_EQ(convtime("90m"), 5400); 3441477Sjulian ASSERT_LONG_EQ(convtime("1h30m"), 5400); 35114589Sobrien ASSERT_LONG_EQ(convtime("2d"), 172800); 36114589Sobrien ASSERT_LONG_EQ(convtime("1w"), 604800); 371558Srgrimes 381558Srgrimes /* negative time is not allowed */ 3966861Sadrian ASSERT_LONG_EQ(convtime("-7"), -1); 4066861Sadrian ASSERT_LONG_EQ(convtime("-9d"), -1); 4123675Speter 421558Srgrimes /* overflow */ 431558Srgrimes snprintf(buf, sizeof buf, "%llu", (unsigned long long)LONG_MAX + 1); 441558Srgrimes ASSERT_LONG_EQ(convtime(buf), -1); 4523799Sbde 4623675Speter /* overflow with multiplier */ 4766861Sadrian snprintf(buf, sizeof buf, "%lluM", (unsigned long long)LONG_MAX/60 + 1); 4823799Sbde ASSERT_LONG_EQ(convtime(buf), -1); 4966861Sadrian ASSERT_LONG_EQ(convtime("1000000000000000000000w"), -1); 5066861Sadrian TEST_DONE(); 5175927Smckusick} 5266861Sadrian