LCOV - code coverage report
Current view: top level - src/utils/unittests - bytes.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 16 16 100.0 %
Date: 2025-04-26 20:22:58 Functions: 1 1 100.0 %

          Line data    Source code
       1             : /* FreeTDS - Library of routines accessing Sybase and Microsoft databases
       2             :  * Copyright (C) 2019  Frediano Ziglio
       3             :  *
       4             :  * This library is free software; you can redistribute it and/or
       5             :  * modify it under the terms of the GNU Library General Public
       6             :  * License as published by the Free Software Foundation; either
       7             :  * version 2 of the License, or (at your option) any later version.
       8             :  *
       9             :  * This library is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      12             :  * Library General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU Library General Public
      15             :  * License along with this library; if not, write to the
      16             :  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
      17             :  * Boston, MA 02111-1307, USA.
      18             :  */
      19             : 
      20             : /*
      21             :  * Purpose: test bytes.h header.
      22             :  */
      23             : 
      24             : #include <freetds/utils/test_base.h>
      25             : 
      26             : #include <stdio.h>
      27             : #include <assert.h>
      28             : 
      29             : #ifdef HAVE_STRING_H
      30             : #include <string.h>
      31             : #endif
      32             : 
      33             : #include "tds_sysdep_public.h"
      34             : #include <freetds/bytes.h>
      35             : 
      36             : #define WRITE(buf, off, bytes, endian, val) do { \
      37             :         if ((off % bytes) == 0) TDS_PUT_A ## bytes ## endian(buf+off, val); \
      38             :         else TDS_PUT_UA ## bytes ## endian(buf+off, val); \
      39             : } while(0)
      40             : 
      41             : #define READ(buf, off, bytes, endian) ( \
      42             :         ((off % bytes) == 0) ? TDS_GET_A ## bytes ## endian(buf+off) : \
      43             :         TDS_GET_UA ## bytes ## endian(buf+off) \
      44             : )
      45             : 
      46             : /* check read and write works at a given offset */
      47             : #define CHECK(off, bytes, endian, expected) do { \
      48             :         uint32_t val = READ(bufs.u1.buf, off, bytes, endian); \
      49             :         WRITE(bufs.u2.buf, off, bytes, endian, val); \
      50             :         assert(READ(bufs.u1.buf, off, bytes, endian) == READ(bufs.u2.buf, off, bytes, endian)); \
      51             :         assert(val == expected); \
      52             : } while(0)
      53             : 
      54          10 : TEST_MAIN()
      55             : {
      56             :         /* this structure make sure buffer are properly aligned */
      57             :         struct {
      58             :                 union {
      59             :                         uint8_t buf[26];
      60             :                         uint32_t a1;
      61             :                 } u1;
      62             :                 union {
      63             :                         uint8_t buf[26];
      64             :                         uint32_t a2;
      65             :                 } u2;
      66             :         } bufs;
      67             :         unsigned n;
      68             : 
      69          10 :         memset(&bufs, 0, sizeof(bufs));
      70         270 :         for (n = 0; n < sizeof(bufs.u1.buf); ++n)
      71         260 :                 bufs.u1.buf[n] = (uint8_t) (123 * n + 67);
      72             : 
      73             :         /* aligned access */
      74          10 :         CHECK(0, 4, LE, 0xb439be43u);
      75          10 :         CHECK(4, 2, LE, 0xaa2fu);
      76          10 :         CHECK(6, 2, BE, 0x25a0u);
      77          10 :         CHECK(8, 4, BE, 0x1b96118cu);
      78             : 
      79          10 :         CHECK(12, 1, LE, 0x7u);
      80             : 
      81             :         /* unaligned access */
      82          10 :         CHECK(13, 2, BE, 0x82fdu);
      83          10 :         CHECK(15, 2, LE, 0xf378u);
      84          10 :         CHECK(17, 4, LE, 0xdf64e96eu);
      85          10 :         CHECK(21, 4, BE, 0x5ad550cbu);
      86             : 
      87          10 :         CHECK(25, 1, BE, 0x46);
      88          10 :         assert(memcmp(bufs.u1.buf, bufs.u2.buf, sizeof(bufs.u1.buf)) == 0);
      89             : 
      90          10 :         return 0;
      91             : }
      92             : 

Generated by: LCOV version 1.13