Home | History | Annotate | Line # | Download | only in tests
supernode_atdepth_offset.c revision 1.1.1.1
      1  1.1  macallan /*
      2  1.1  macallan  * libfdt - Flat Device Tree manipulation
      3  1.1  macallan  *	Testcase for fdt_supernode_atdepth_offset()
      4  1.1  macallan  * Copyright (C) 2006 David Gibson, IBM Corporation.
      5  1.1  macallan  *
      6  1.1  macallan  * This library is free software; you can redistribute it and/or
      7  1.1  macallan  * modify it under the terms of the GNU Lesser General Public License
      8  1.1  macallan  * as published by the Free Software Foundation; either version 2.1 of
      9  1.1  macallan  * the License, or (at your option) any later version.
     10  1.1  macallan  *
     11  1.1  macallan  * This library is distributed in the hope that it will be useful, but
     12  1.1  macallan  * WITHOUT ANY WARRANTY; without even the implied warranty of
     13  1.1  macallan  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     14  1.1  macallan  * Lesser General Public License for more details.
     15  1.1  macallan  *
     16  1.1  macallan  * You should have received a copy of the GNU Lesser General Public
     17  1.1  macallan  * License along with this library; if not, write to the Free Software
     18  1.1  macallan  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
     19  1.1  macallan  */
     20  1.1  macallan #include <stdlib.h>
     21  1.1  macallan #include <stdio.h>
     22  1.1  macallan #include <string.h>
     23  1.1  macallan #include <stdint.h>
     24  1.1  macallan 
     25  1.1  macallan #include <libfdt.h>
     26  1.1  macallan 
     27  1.1  macallan #include "tests.h"
     28  1.1  macallan #include "testdata.h"
     29  1.1  macallan 
     30  1.1  macallan static int path_depth(const char *path)
     31  1.1  macallan {
     32  1.1  macallan 	const char *p;
     33  1.1  macallan 	int depth = 0;
     34  1.1  macallan 
     35  1.1  macallan 	if (path[0] != '/')
     36  1.1  macallan 		TEST_BUG();
     37  1.1  macallan 
     38  1.1  macallan 	if (strcmp(path, "/") == 0)
     39  1.1  macallan 		return 0;
     40  1.1  macallan 	for (p = path; *p; p++)
     41  1.1  macallan 		if (*p == '/')
     42  1.1  macallan 			depth++;
     43  1.1  macallan 
     44  1.1  macallan 	/* Special case for path == "/" */
     45  1.1  macallan 	if (p == (path + 1))
     46  1.1  macallan 		return 0;
     47  1.1  macallan 	else
     48  1.1  macallan 		return depth;
     49  1.1  macallan }
     50  1.1  macallan 
     51  1.1  macallan static int path_prefix(const char *path, int depth)
     52  1.1  macallan {
     53  1.1  macallan 	const char *p;
     54  1.1  macallan 	int i;
     55  1.1  macallan 
     56  1.1  macallan 	if (path[0] != '/')
     57  1.1  macallan 		TEST_BUG();
     58  1.1  macallan 
     59  1.1  macallan 	if (depth == 0)
     60  1.1  macallan 		return 1;
     61  1.1  macallan 
     62  1.1  macallan 	p = path;
     63  1.1  macallan 	for (i = 0; i < depth; i++)
     64  1.1  macallan 		p = p+1 + strcspn(p+1, "/");
     65  1.1  macallan 
     66  1.1  macallan 	return p - path;
     67  1.1  macallan }
     68  1.1  macallan 
     69  1.1  macallan static void check_supernode_atdepth(struct fdt_header *fdt, const char *path,
     70  1.1  macallan 			     int depth)
     71  1.1  macallan {
     72  1.1  macallan 	int pdepth = path_depth(path);
     73  1.1  macallan 	char *superpath;
     74  1.1  macallan 	int nodeoffset, supernodeoffset, superpathoffset, pathprefixlen;
     75  1.1  macallan 	int nodedepth;
     76  1.1  macallan 
     77  1.1  macallan 	pathprefixlen = path_prefix(path, depth);
     78  1.1  macallan 	superpath = alloca(pathprefixlen + 1);
     79  1.1  macallan 	strncpy(superpath, path, pathprefixlen);
     80  1.1  macallan 	superpath[pathprefixlen] = '\0';
     81  1.1  macallan 
     82  1.1  macallan 	verbose_printf("Path %s (%d), depth %d, supernode is %s\n",
     83  1.1  macallan 		       path, pdepth, depth, superpath);
     84  1.1  macallan 
     85  1.1  macallan 	nodeoffset = fdt_path_offset(fdt, path);
     86  1.1  macallan 	if (nodeoffset < 0)
     87  1.1  macallan 		FAIL("fdt_path_offset(%s): %s", path, fdt_strerror(nodeoffset));
     88  1.1  macallan 	superpathoffset = fdt_path_offset(fdt, superpath);
     89  1.1  macallan 	if (superpathoffset < 0)
     90  1.1  macallan 		FAIL("fdt_path_offset(%s): %s", superpath,
     91  1.1  macallan 		     fdt_strerror(superpathoffset));
     92  1.1  macallan 
     93  1.1  macallan 	supernodeoffset = fdt_supernode_atdepth_offset(fdt, nodeoffset,
     94  1.1  macallan 						       depth, &nodedepth);
     95  1.1  macallan 	if (supernodeoffset < 0)
     96  1.1  macallan 		FAIL("fdt_supernode_atdepth_offset(): %s",
     97  1.1  macallan 		     fdt_strerror(supernodeoffset));
     98  1.1  macallan 
     99  1.1  macallan 	if (supernodeoffset != superpathoffset)
    100  1.1  macallan 		FAIL("fdt_supernode_atdepth_offset() returns %d instead of %d",
    101  1.1  macallan 		     supernodeoffset, superpathoffset);
    102  1.1  macallan 
    103  1.1  macallan 	if (nodedepth != pdepth)
    104  1.1  macallan 		FAIL("fdt_supernode_atdept_offset() returns node depth %d "
    105  1.1  macallan 		     "instead of %d", nodedepth, pdepth);
    106  1.1  macallan }
    107  1.1  macallan 
    108  1.1  macallan static void check_supernode_overdepth(struct fdt_header *fdt, const char *path)
    109  1.1  macallan {
    110  1.1  macallan 	int pdepth = path_depth(path);
    111  1.1  macallan 	int nodeoffset, err;
    112  1.1  macallan 
    113  1.1  macallan 	nodeoffset = fdt_path_offset(fdt, path);
    114  1.1  macallan 	if (nodeoffset < 0)
    115  1.1  macallan 		FAIL("fdt_path_offset(%s): %s", path, fdt_strerror(nodeoffset));
    116  1.1  macallan 
    117  1.1  macallan 	err = fdt_supernode_atdepth_offset(fdt, nodeoffset, pdepth + 1, NULL);
    118  1.1  macallan 	if (err != -FDT_ERR_NOTFOUND)
    119  1.1  macallan 		FAIL("fdt_supernode_atdept_offset(%s, %d) returns %d instead "
    120  1.1  macallan 		     "of FDT_ERR_NOTFOUND", path, pdepth+1, err);
    121  1.1  macallan }
    122  1.1  macallan 
    123  1.1  macallan static void check_path(struct fdt_header *fdt, const char *path)
    124  1.1  macallan {
    125  1.1  macallan 	int i;
    126  1.1  macallan 
    127  1.1  macallan 	for (i = 0; i <= path_depth(path); i++)
    128  1.1  macallan 		check_supernode_atdepth(fdt, path, i);
    129  1.1  macallan 	check_supernode_overdepth(fdt, path);
    130  1.1  macallan }
    131  1.1  macallan int main(int argc, char *argv[])
    132  1.1  macallan {
    133  1.1  macallan 	void *fdt;
    134  1.1  macallan 
    135  1.1  macallan 	test_init(argc, argv);
    136  1.1  macallan 	fdt = load_blob_arg(argc, argv);
    137  1.1  macallan 
    138  1.1  macallan 	check_path(fdt, "/");
    139  1.1  macallan 	check_path(fdt, "/subnode@1");
    140  1.1  macallan 	check_path(fdt, "/subnode@2");
    141  1.1  macallan 	check_path(fdt, "/subnode@1/subsubnode");
    142  1.1  macallan 	check_path(fdt, "/subnode@2/subsubnode@0");
    143  1.1  macallan 
    144  1.1  macallan 	PASS();
    145  1.1  macallan }
    146