From patchwork Sat Sep 21 01:14:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joseph Myers X-Patchwork-Id: 1988086 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=ZLkztra/; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces~incoming=patchwork.ozlabs.org@sourceware.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4X9WX74hGCz1y2P for ; Sat, 21 Sep 2024 11:15:19 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 0620E3858D29 for ; Sat, 21 Sep 2024 01:15:17 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTP id 687313858D29 for ; Sat, 21 Sep 2024 01:14:54 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 687313858D29 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 687313858D29 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1726881299; cv=none; b=IIsEFoPsz6j0BHSBGeVorW+89QzAPcp4wkjo9FyZobiuatLyiIzWtWRh6f9Bn8gzCbwHPPHmVu04AR7LddLZFRa/N3wPQdKS6wax4vvO5sbcoPK0yg9WVVUBUSyumD2UztZa1RAQuPs30/YnTjLSY3oYELriqFfCDApoyEMWEzk= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1726881299; c=relaxed/simple; bh=R+KF/9NlnHbecloMPKpsrBRvNv7m7/45kpWsChfeRZE=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=umKb4lh9z1d8qDr4mdCzHys7GYfi3UUQ8GgkDOzwkkn5X/DUrhI3Zfs8i9lqn3CnN+GDB0m3swbvMQiaeOxTp/L8fIWSzUY+nT/sLwAur8xKK4E0m07iq9OKZUGiF7n2+XDvNEZRhEanwH6TE5jGMzhL2h5SwVYbuyvQI5eYzvY= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1726881294; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type; bh=snuaq4yDtCFvq8xFFD1JTcOXSCXJ+gXDZo7rgqDBJ2Y=; b=ZLkztra/ucd+wi4OSsgJF8yKvjDi44nB8DeMsRvhhUOGeHbxCl8koTj8iROfUnKNISZ1+N upHATJYY263Y+BiQRhVVDAIploXt3KOcSMHFpvu+5h7tBW/4z7aKX6gW3N080iQhPUsgfm 3Ja6CCOF5gdNDDWgg9bTqivvtOEvfQ4= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-620-xsOR_8_4MdasE7HVmGn8ww-1; Fri, 20 Sep 2024 21:14:52 -0400 X-MC-Unique: xsOR_8_4MdasE7HVmGn8ww-1 Received: by mail-wm1-f71.google.com with SMTP id 5b1f17b1804b1-42cb236ad4aso16596735e9.3 for ; Fri, 20 Sep 2024 18:14:52 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726881291; x=1727486091; h=mime-version:message-id:subject:to:from:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=snuaq4yDtCFvq8xFFD1JTcOXSCXJ+gXDZo7rgqDBJ2Y=; b=rYo+uvAmgx9LyJFMYEShCraLRsQ5tLOGyDPt5XDqJJMiJKCTlPODXdzvNS2lUksWHz aYszjwJ/OEDx/+LHAsN/5gZcPmIxoVJOpgnWo13KsZvEk+RMOsvCSldflv6VqFMyjtN5 hAAyC0cRwTVPpUa5nCAiceHvOnAFfgKPWskkzkQhWINVO0a3WV5wU3/Ae0GEmalqCKUr 9vOyzA5P9bKZkbF3XOIHJo7AjLKOCJNegOFkEXT8UWTd6M33RYVxL+WP/6SoCosk4PuX ESaxOsKanQ/l2+KD0gMYasrHkea6Mi+QRya3YVIif0xBFWq7JJmWyQ01qw8zKRsif/tG wX1Q== X-Gm-Message-State: AOJu0YwwgiXTMKik382cQ8OtWYSAL/iSizXywCYH3GDMwZROOaQtJAU5 pZ0Z9vQ1lXVSJ3K1CY8ZEK63DBWD9hw/mE0Nyqss93ECjN89WCvx537f5C9wf/GDl8xoW/NopTF r/W7M1Del1IfUaTqd09XxEhRShwWx3FVHIJZ47ad8k6QusoxbrDNXXFYk1iVDUx3ZjVAdumur7n ILXJT60KbPBWN71iKdjFJ8Iu/F3zGtEepOlYFK9IlCSw== X-Received: by 2002:a05:6000:186c:b0:368:5bb4:169b with SMTP id ffacd0b85a97d-37a43128c93mr2927423f8f.4.1726881291362; Fri, 20 Sep 2024 18:14:51 -0700 (PDT) X-Google-Smtp-Source: AGHT+IG74QeMSpk98//BBlL/YfO1gfGIrTO6dIY5OT0sLPpEoopExbm0OXXCv7c9eArisKuUnhEv8w== X-Received: by 2002:a05:6000:186c:b0:368:5bb4:169b with SMTP id ffacd0b85a97d-37a43128c93mr2927415f8f.4.1726881290841; Fri, 20 Sep 2024 18:14:50 -0700 (PDT) Received: from digraph.polyomino.org.uk (digraph.polyomino.org.uk. [2001:8b0:bf73:93f7::51bb:e332]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-378e7800152sm18957407f8f.74.2024.09.20.18.14.48 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Sep 2024 18:14:49 -0700 (PDT) Received: from jsm28 (helo=localhost) by digraph.polyomino.org.uk with local-esmtp (Exim 4.97) (envelope-from ) id 1srohN-0000000C9mL-0Lco for libc-alpha@sourceware.org; Sat, 21 Sep 2024 01:14:09 +0000 Date: Sat, 21 Sep 2024 01:14:09 +0000 (UTC) From: Joseph Myers To: libc-alpha@sourceware.org Subject: Add tests of fread Message-ID: <56bc8b92-ff5a-57cd-3836-b281480d96e8@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-9.0 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~incoming=patchwork.ozlabs.org@sourceware.org There seem to be no glibc tests specifically for the fread function. Add basic tests of that function. Tested for x86_64. diff --git a/stdio-common/Makefile b/stdio-common/Makefile index 62f8b99b06..e3e5f634c5 100644 --- a/stdio-common/Makefile +++ b/stdio-common/Makefile @@ -217,6 +217,7 @@ tests := \ tst-fmemopen4 \ tst-fphex \ tst-fphex-wide \ + tst-fread \ tst-freopen2 \ tst-freopen3 \ tst-freopen4 \ diff --git a/stdio-common/tst-fread.c b/stdio-common/tst-fread.c new file mode 100644 index 0000000000..44aee839a1 --- /dev/null +++ b/stdio-common/tst-fread.c @@ -0,0 +1,137 @@ +/* Test fread. + Copyright (C) 2024 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +int +do_test (void) +{ + char *temp_dir = support_create_temp_directory ("tst-fread"); + char *file1 = xasprintf ("%s/file1", temp_dir); + support_write_file_string (file1, "file1"); + add_temp_file (file1); + FILE *fp; + size_t ret; + char buf[1024]; + + verbose_printf ("test single-byte reads\n"); + fp = xfopen (file1, "r"); + memset (buf, 0, sizeof buf); + ret = fread (buf, 1, 2, fp); + TEST_COMPARE (ret, 2); + TEST_COMPARE (buf[0], 'f'); + TEST_COMPARE (buf[1], 'i'); + TEST_COMPARE (feof (fp), 0); + TEST_COMPARE (ftell (fp), 2); + memset (buf, 0, sizeof buf); + ret = fread (buf, 1, 3, fp); + TEST_COMPARE (ret, 3); + TEST_COMPARE (buf[0], 'l'); + TEST_COMPARE (buf[1], 'e'); + TEST_COMPARE (buf[2], '1'); + TEST_COMPARE (ftell (fp), 5); + TEST_COMPARE (feof (fp), 0); + memset (buf, 0, sizeof buf); + ret = fread (buf, 1, 1, fp); + TEST_COMPARE (ret, 0); + TEST_COMPARE (!!feof (fp), 1); + TEST_COMPARE (ferror (fp), 0); + TEST_COMPARE (ftell (fp), 5); + xfclose (fp); + + verbose_printf ("test single-byte reads, EOF part way through\n"); + fp = xfopen (file1, "r"); + memset (buf, 0, sizeof buf); + ret = fread (buf, 1, sizeof buf, fp); + TEST_COMPARE (ret, 5); + TEST_COMPARE (buf[0], 'f'); + TEST_COMPARE (buf[1], 'i'); + TEST_COMPARE (buf[2], 'l'); + TEST_COMPARE (buf[3], 'e'); + TEST_COMPARE (buf[4], '1'); + TEST_COMPARE (!!feof (fp), 1); + TEST_COMPARE (ferror (fp), 0); + TEST_COMPARE (ftell (fp), 5); + xfclose (fp); + + verbose_printf ("test multi-byte reads\n"); + fp = xfopen (file1, "r"); + memset (buf, 0, sizeof buf); + ret = fread (buf, 2, 2, fp); + TEST_COMPARE (ret, 2); + TEST_COMPARE (buf[0], 'f'); + TEST_COMPARE (buf[1], 'i'); + TEST_COMPARE (buf[2], 'l'); + TEST_COMPARE (buf[3], 'e'); + TEST_COMPARE (feof (fp), 0); + TEST_COMPARE (ftell (fp), 4); + memset (buf, 0, sizeof buf); + ret = fread (buf, 3, 3, fp); + TEST_COMPARE (ret, 0); + /* The bytes written for a partial element read are unspecified. */ + TEST_COMPARE (!!feof (fp), 1); + TEST_COMPARE (ferror (fp), 0); + TEST_COMPARE (ftell (fp), 5); + xfclose (fp); + + verbose_printf ("test read error\n"); + fp = xfopen (file1, "r"); + xclose (fileno (fp)); + memset (buf, 0, sizeof buf); + ret = fread (buf, 1, sizeof buf, fp); + TEST_COMPARE (ret, 0); + TEST_COMPARE (feof (fp), 0); + TEST_COMPARE (!!ferror (fp), 1); + fclose (fp); + + verbose_printf ("test zero size\n"); + fp = xfopen (file1, "r"); + ret = fread (buf, 0, SIZE_MAX, fp); + TEST_COMPARE (ret, 0); + TEST_COMPARE (feof (fp), 0); + TEST_COMPARE (ferror (fp), 0); + TEST_COMPARE (ftell (fp), 0); + xfclose (fp); + + verbose_printf ("test zero items\n"); + fp = xfopen (file1, "r"); + ret = fread (buf, SIZE_MAX, 0, fp); + TEST_COMPARE (ret, 0); + TEST_COMPARE (feof (fp), 0); + TEST_COMPARE (ferror (fp), 0); + TEST_COMPARE (ftell (fp), 0); + xfclose (fp); + + free (temp_dir); + free (file1); + return 0; +} + +#include