• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1[+ AutoGen5 template c +]
2/*
3** Copyright (C) 2002-2016 Erik de Castro Lopo <erikd@mega-nerd.com>
4**
5** This program is free software; you can redistribute it and/or modify
6** it under the terms of the GNU Lesser General Public License as published by
7** the Free Software Foundation; either version 2.1 of the License, or
8** (at your option) any later version.
9**
10** This program is distributed in the hope that it will be useful,
11** but WITHOUT ANY WARRANTY; without even the implied warranty of
12** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13** GNU Lesser General Public License for more details.
14**
15** You should have received a copy of the GNU Lesser General Public License
16** along with this program; if not, write to the Free Software
17** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18*/
19
20#include "sfconfig.h"
21
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <errno.h>
26#include <inttypes.h>
27
28#if HAVE_UNISTD_H
29#include <unistd.h>
30#else
31#include "sf_unistd.h"
32#endif
33
34#include "common.h"
35#include "sfendian.h"
36
37#include "test_main.h"
38
39#define	FMT_SHORT	"0x%04x\n"
40#define	FMT_INT		"0x%08x\n"
41#define	FMT_INT64	"0x%016" PRIx64 "\n"
42
43/*==============================================================================
44** Test functions.
45*/
46
47[+ FOR int_type +]
48static void
49dump_[+ (get "name") +]_array (const char * name, [+ (get "name") +] * data, int datalen)
50{	int k ;
51
52	printf ("%-6s : ", name) ;
53	for (k = 0 ; k < datalen ; k++)
54		printf ([+ (get "format") +], data [k]) ;
55	putchar ('\n') ;
56} /* dump_[+ (get "name") +]_array */
57
58static void
59test_endswap_[+ (get "name") +] (void)
60{	[+ (get "name") +] orig [4], first [4], second [4] ;
61	int64_t k ;
62
63	printf ("    %-40s : ", "test_endswap_[+ (get "name") +]") ;
64	fflush (stdout) ;
65
66	for (k = 0 ; k < ARRAY_LEN (orig) ; k++)
67		orig [k] = [+ (get "value") +] + k ;
68
69	endswap_[+ (get "name") +]_copy (first, orig, ARRAY_LEN (first)) ;
70	endswap_[+ (get "name") +]_copy (second, first, ARRAY_LEN (second)) ;
71
72	if (memcmp (orig, first, sizeof (orig)) == 0)
73	{	printf ("\n\nLine %d : test 1 : these two array should not be the same:\n\n", __LINE__) ;
74		dump_[+ (get "name") +]_array ("orig", orig, ARRAY_LEN (orig)) ;
75		dump_[+ (get "name") +]_array ("first", first, ARRAY_LEN (first)) ;
76		exit (1) ;
77		} ;
78
79	if (memcmp (orig, second, sizeof (orig)) != 0)
80	{	printf ("\n\nLine %d : test 2 : these two array should be the same:\n\n", __LINE__) ;
81		dump_[+ (get "name") +]_array ("orig", orig, ARRAY_LEN (orig)) ;
82		dump_[+ (get "name") +]_array ("second", second, ARRAY_LEN (second)) ;
83		exit (1) ;
84		} ;
85
86	endswap_[+ (get "name") +]_array (first, ARRAY_LEN (first)) ;
87
88	if (memcmp (orig, first, sizeof (orig)) != 0)
89	{	printf ("\n\nLine %d : test 3 : these two array should be the same:\n\n", __LINE__) ;
90		dump_[+ (get "name") +]_array ("orig", orig, ARRAY_LEN (orig)) ;
91		dump_[+ (get "name") +]_array ("first", first, ARRAY_LEN (first)) ;
92		exit (1) ;
93		} ;
94
95	endswap_[+ (get "name") +]_copy (first, orig, ARRAY_LEN (first)) ;
96	endswap_[+ (get "name") +]_copy (first, first, ARRAY_LEN (first)) ;
97
98	if (memcmp (orig, first, sizeof (orig)) != 0)
99	{	printf ("\n\nLine %d : test 4 : these two array should be the same:\n\n", __LINE__) ;
100		dump_[+ (get "name") +]_array ("orig", orig, ARRAY_LEN (orig)) ;
101		dump_[+ (get "name") +]_array ("first", first, ARRAY_LEN (first)) ;
102		exit (1) ;
103		} ;
104
105	puts ("ok") ;
106} /* test_endswap_[+ (get "name") +] */
107[+ ENDFOR int_type
108+]
109
110[+ FOR int_size +]
111static void
112test_psf_put_be[+ (get "name") +] (void)
113{	const char *test = "[+ (get "strval") +]" ;
114	uint8_t array [32] ;
115	int k ;
116
117	printf ("    %-40s : ", __func__) ;
118	fflush (stdout) ;
119
120	for (k = 0 ; k < 10 ; k++)
121	{	memset (array, 0, sizeof (array)) ;
122
123		psf_put_be[+ (get "name") +] (array, k, [+ (get "value") +]) ;
124		if (memcmp (array + k, test, sizeof ([+ (get "typename") +])) != 0)
125		{	printf ("\n\nLine %d : Put failed at index %d.\n", __LINE__, k) ;
126			exit (1) ;
127			} ;
128		if (psf_get_be[+ (get "name") +] (array, k) != [+ (get "value") +])
129		{	printf ("\n\nLine %d : Get failed at index %d.\n", __LINE__, k) ;
130			exit (1) ;
131			} ;
132		} ;
133
134	puts ("ok") ;
135} /* test_psf_put_be[+ (get "name") +] */
136[+ ENDFOR int_size
137+]
138
139void
140test_endswap (void)
141{
142[+ FOR int_type
143+]	test_endswap_[+ (get "name") +] () ;
144[+ ENDFOR int_type
145+]
146
147[+ FOR int_size
148+]	test_psf_put_be[+ (get "name") +] () ;
149[+ ENDFOR int_size
150+]
151
152} /* test_endswap */
153
154