From patchwork Fri Feb 20 02:45:05 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Axtens X-Patchwork-Id: 441823 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.ozlabs.org (lists.ozlabs.org [103.22.144.68]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 5B82B14017C for ; Fri, 20 Feb 2015 13:45:16 +1100 (AEDT) Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) by lists.ozlabs.org (Postfix) with ESMTP id 459821A0EAF for ; Fri, 20 Feb 2015 13:45:16 +1100 (AEDT) X-Original-To: skiboot@lists.ozlabs.org Delivered-To: skiboot@lists.ozlabs.org Received: from mail-pd0-x236.google.com (mail-pd0-x236.google.com [IPv6:2607:f8b0:400e:c02::236]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id C83FD1A0E32 for ; Fri, 20 Feb 2015 13:45:13 +1100 (AEDT) Received: by pdbfl12 with SMTP id fl12so4378473pdb.2 for ; Thu, 19 Feb 2015 18:45:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=axtens.net; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=8ZmpCEtOIQY8Y8AhKDPHxnVXF9SB7XcDek5MP3B8ajw=; b=EqIKLVNoFqiC1AkW6BrHjGtTfnKoIoyOYQzO+jxjaZMs52huGlGKV6lfLtqQ534RlP hVrK3wh94T3EPGU7QMpGmdxXhNf0Tn3cyyJoTp6MddkeRFwy4OAf6YvEPfp2cnZTgMg0 w9gIv2x4xZ0QmnsKEXSc47KVJOlPDuq7vLN0I= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=8ZmpCEtOIQY8Y8AhKDPHxnVXF9SB7XcDek5MP3B8ajw=; b=HdUvKBvpGCbXIfKXOJ/dffk/yiv96CZzxvtas5yE1gssyhZKhaEMUPxehMZPAuWdTW Eu6s2QaZ3UtqcHf/X+dHgy0zcjviTEfeo+4RSx8TcKC0WsX2tOPI+WXM/5NcqSeTAP9V QT/Y8yWv46vySbW8x+8CqO8AoOFNxoIPBk4+sKwChelCeqWaXhs8LKOioV3WIhVc4qlO 6FS5y8SDtrt1tWu21WpBILngMzjX2TZ2Wgoc3PCa7Kr60679dpUbxyF64/aFRDvIsp9N MXfCHehgSqjmcMUnnIGtITDqc5Hd0zUh8knSlPfgEn9GUSamwOgCBDCTgTmuZDN5nmHD pgBw== X-Gm-Message-State: ALoCoQldHU+A8VMTvo4d3kojBwCneWlZNcDN0yjCfiWkKqbD097CidLzE3SOifvuXEifKoPOA7Ng X-Received: by 10.66.250.196 with SMTP id ze4mr13310516pac.32.1424400311721; Thu, 19 Feb 2015 18:45:11 -0800 (PST) Received: from omicron.ozlabs.ibm.com (bh02i525f01.au.ibm.com. [202.81.18.30]) by mx.google.com with ESMTPSA id nq16sm14800954pdb.85.2015.02.19.18.45.09 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 19 Feb 2015 18:45:11 -0800 (PST) From: Daniel Axtens To: skiboot@lists.ozlabs.org Date: Fri, 20 Feb 2015 13:45:05 +1100 Message-Id: <1424400307-13664-2-git-send-email-dja@axtens.net> X-Mailer: git-send-email 2.1.4 In-Reply-To: <1424400307-13664-1-git-send-email-dja@axtens.net> References: <1424400307-13664-1-git-send-email-dja@axtens.net> Subject: [Skiboot] [PATCH 1/3] Test libc stdlib functions (atoi/strtol and friends) X-BeenThere: skiboot@lists.ozlabs.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Mailing list for skiboot development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: skiboot-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org Sender: "Skiboot" This increases coverage of atoi, atol, strtol and strtoul to 100%. Signed-off-by: Daniel Axtens Reviewed-by: Cyril Bur --- libc/test/run-stdlib.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/libc/test/run-stdlib.c b/libc/test/run-stdlib.c index 15ec9bd..bb64790 100644 --- a/libc/test/run-stdlib.c +++ b/libc/test/run-stdlib.c @@ -23,5 +23,65 @@ int main(void) { + char buf[] = "42, and stuff."; + char *ptr; + + /* atoi/strtol - general correct behavior */ + assert(atoi("0") == 0); + assert(atoi("1") == 1); + assert(atoi(" 123456") == 123456); + assert(atoi("-72") == -72); + assert(atoi(" -84") == -84); + assert(atoi("2147483647") == 2147483647); + + /* atoi/strtol - numbers before and after strings */ + assert(atoi("hello!123") == 0); + assert(atoi(buf) == 42); + assert(atoi("42isthemagicnumber") == 42); + + /* our atoi recognises hex! */ + assert(atoi("0x800") == 0x800); + /* Really weird hex! */ + assert(atoi("0x0x800") == 0x800); + + /* atol - ensure it recognises longs */ + assert(atol("2147483648") == 2147483648); + assert(atol("-2147483649") == -2147483649); + + /* strtol - invalid/weird bases */ + assert(strtol("z", NULL, -1) == 0); + assert(strtol("11111", NULL, 1) == 0); + assert(strtol("z", NULL, 37) == 0); + assert(strtol("z", NULL, 36) == 35); + assert(strtol("-Y", NULL, 36) == -34); + + /* strtol - ptr advanced correctly */ + ptr = buf; + assert(strtol(buf, &ptr, 10) == 42); + assert(ptr == buf + 2); + + /* strtoul - base 10 */ + assert(strtoul("0", NULL, 10) == 0); + assert(strtoul("1", NULL, 10) == 1); + assert(strtoul(" 123456", NULL, 10) == 123456); + assert(strtoul("-72", NULL, 10) == 0); + assert(strtoul("9999999999", NULL, 10) == 9999999999); + assert(strtoul("hello!123", NULL, 10) == 0); + assert(strtoul(buf, NULL, 10) == 42); + assert(strtoul("42isthemagicnumber", NULL, 10) == 42); + + /* strtoul - autodetection of base */ + assert(strtoul(" 123456", NULL, 0) == 123456); + assert(strtoul("0x800", NULL, 0) == 0x800); + /* Again, really weird hex */ + assert(strtoul("0x0x800", NULL, 0) == 0x800); + + /* strtoul - weird/invalid bases */ + assert(strtoul("z", NULL, -1) == 0); + assert(strtoul("11111", NULL, 1) == 0); + assert(strtoul("z", NULL, 37) == 0); + assert(strtoul("z", NULL, 36) == 35); + assert(strtoul("Y", NULL, 36) == 34); + return 0; }