symbian-qemu-0.9.1-12/dtc-trunk/tests/parent_offset.c
changeset 1 2fb8b9db1c86
equal deleted inserted replaced
0:ffa851df0825 1:2fb8b9db1c86
       
     1 /*
       
     2  * libfdt - Flat Device Tree manipulation
       
     3  *	Testcase for fdt_parent_offset()
       
     4  * Copyright (C) 2006 David Gibson, IBM Corporation.
       
     5  *
       
     6  * This library is free software; you can redistribute it and/or
       
     7  * modify it under the terms of the GNU Lesser General Public License
       
     8  * as published by the Free Software Foundation; either version 2.1 of
       
     9  * the License, or (at your option) any later version.
       
    10  *
       
    11  * This library is distributed in the hope that it will be useful, but
       
    12  * WITHOUT ANY WARRANTY; without even the implied warranty of
       
    13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    14  * Lesser General Public License for more details.
       
    15  *
       
    16  * You should have received a copy of the GNU Lesser General Public
       
    17  * License along with this library; if not, write to the Free Software
       
    18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
       
    19  */
       
    20 #include <stdlib.h>
       
    21 #include <stdio.h>
       
    22 #include <string.h>
       
    23 #include <stdint.h>
       
    24 
       
    25 #include <fdt.h>
       
    26 #include <libfdt.h>
       
    27 
       
    28 #include "tests.h"
       
    29 #include "testdata.h"
       
    30 
       
    31 int path_parent_len(const char *path)
       
    32 {
       
    33 	const char *p = strrchr(path, '/');
       
    34 
       
    35 	if (!p)
       
    36 		TEST_BUG();
       
    37 	if (p == path)
       
    38 		return 1;
       
    39 	else
       
    40 		return p - path;
       
    41 }
       
    42 
       
    43 void check_path(struct fdt_header *fdt, const char *path)
       
    44 {
       
    45 	char *parentpath;
       
    46 	int nodeoffset, parentoffset, parentpathoffset, pathparentlen;
       
    47 
       
    48 	pathparentlen = path_parent_len(path);
       
    49 	parentpath = alloca(pathparentlen + 1);
       
    50 	strncpy(parentpath, path, pathparentlen);
       
    51 	parentpath[pathparentlen] = '\0';
       
    52 
       
    53 	verbose_printf("Path: \"%s\"\tParent: \"%s\"\n", path, parentpath);
       
    54 
       
    55 	nodeoffset = fdt_path_offset(fdt, path);
       
    56 	if (nodeoffset < 0)
       
    57 		FAIL("fdt_path_offset(%s): %s", path, fdt_strerror(nodeoffset));
       
    58 
       
    59 	parentpathoffset = fdt_path_offset(fdt, parentpath);
       
    60 	if (parentpathoffset < 0)
       
    61 		FAIL("fdt_path_offset(%s): %s", parentpath,
       
    62 		     fdt_strerror(parentpathoffset));
       
    63 
       
    64 	parentoffset = fdt_parent_offset(fdt, nodeoffset);
       
    65 	if (parentoffset < 0)
       
    66 		FAIL("fdt_parent_offset(): %s", fdt_strerror(parentoffset));
       
    67 
       
    68 	if (parentoffset != parentpathoffset)
       
    69 		FAIL("fdt_parent_offset() returns %d instead of %d",
       
    70 		     parentoffset, parentpathoffset);
       
    71 }
       
    72 
       
    73 int main(int argc, char *argv[])
       
    74 {
       
    75 	void *fdt;
       
    76 	int err;
       
    77 
       
    78 	test_init(argc, argv);
       
    79 	fdt = load_blob_arg(argc, argv);
       
    80 
       
    81 	check_path(fdt, "/subnode@1");
       
    82 	check_path(fdt, "/subnode@2");
       
    83 	check_path(fdt, "/subnode@1/subsubnode");
       
    84 	check_path(fdt, "/subnode@2/subsubnode@0");
       
    85 	err = fdt_parent_offset(fdt, 0);
       
    86 	if (err != -FDT_ERR_NOTFOUND)
       
    87 		FAIL("fdt_parent_offset(/) returns %d instead of "
       
    88 		     "-FDT_ERR_NOTFOUND", err);
       
    89 
       
    90 	PASS();
       
    91 }