#!/bin/sh
#-*-sh-*-

#
# SPDX-License-Identifier: BSD-3-Clause
# Copyright © 2009-2025 Inria.  All rights reserved.
# Copyright © 2014 Cisco Systems, Inc.  All rights reserved.
# See COPYING in top-level directory.
#

HWLOC_top_srcdir="@HWLOC_top_srcdir@"
HWLOC_top_builddir="@HWLOC_top_builddir@"
srcdir="$HWLOC_top_srcdir/utils/hwloc"
builddir="$HWLOC_top_builddir/utils/hwloc"
annotate="$builddir/hwloc-annotate"

HWLOC_PLUGINS_PATH=${HWLOC_top_builddir}/hwloc/.libs
export HWLOC_PLUGINS_PATH

HWLOC_DEBUG_CHECK=1
export HWLOC_DEBUG_CHECK

if test x@HWLOC_XML_LOCALIZED@ = x1; then
  # make sure we use default numeric formats
  LANG=C
  LC_ALL=C
  export LANG LC_ALL
fi

: ${TMPDIR=/tmp}
{
  tmp=`
    (umask 077 && mktemp -d "$TMPDIR/fooXXXXXX") 2>/dev/null
  ` &&
  test -n "$tmp" && test -d "$tmp"
} || {
  tmp=$TMPDIR/foo$$-$RANDOM
  (umask 077 && mkdir "$tmp")
} || exit $?
file="$tmp/test-hwloc-annotate.output"
file2="$tmp/test-hwloc-annotate.output2"
distances="$tmp/test-hwloc-annotate.distances"

set -e

$annotate $srcdir/test-hwloc-annotate.input $file pu:0 misc pumisc
$annotate $file $file root misc rootmisc
$annotate $file $file misc:0 misc miscmisc1
$annotate $file $file all info Foo Bar
$annotate --ci $file $file -- Core:all L3:all -- info Foo2 Bar2
$annotate --ci $file $file L2Cache:0 none
$annotate --ri $file $file pack:all info Foo
$annotate $file $file pack:0 info Foo2 Bar
$annotate $file $file pack:0 info Foo2 Bar2
$annotate --ri $file $file pack:0 info Foo2 Bar3
$annotate $file $file os:2-3 info myosdev byindex
$annotate $file $file pci:4:2 info mypcidev byindex
$annotate $file $file pci=0000:02:00.0 info mypcidev bybusid
$annotate $file $file 'pci[8086:0046]:all' info mypcidev bymatch
$annotate $file $file bridge:all info mybridges all
$annotate --cu $file $file L1iCache:0 none
$annotate --cd $file $file dummy none
$annotate $file $file -- dummy -- memattr DummyMemattr higher
$annotate $file $file -- pu:0 -- memattr dummymemattr dummy 1234
$annotate $file $file -- dummy -- memattr DummyMemattr2 lower,need_init
$annotate $file $file -- pu:2 -- memattr dummymemattr2 0xf 123
$annotate $file $file -- dummy -- cpukind 0x5 -1 0
$annotate $file $file -- dummy -- cpukind 0xa -1 0 foo bar
$annotate $file $file -- dummy -- cpukind 0x3 4 0 foo2 bar2
$annotate $file $file -- dummy -- cpukind 0xc 7 0 foo2 bar3
cat > $distances << EOF
name=mynewdistances
5
4
pu:2
pu:3
pu:0
pu:1
4*1
EOF
$annotate $file $file dummy distances $distances group\$
$annotate $file $file dummy distances-transform mynewdistances remove-obj pu:3

@DIFF@ @HWLOC_DIFF_U@ @HWLOC_DIFF_W@ $srcdir/test-hwloc-annotate.output "$file"


$annotate $srcdir/../../tests/hwloc/xml/power8gpudistances.xml $file2 -- dummy -- distances-transform NVLinkBandwidth replace-objs nvml opencl
$annotate $file2 $file2 -- dummy -- distances-transform NVLinkBandwidth replace-objs package numa

@DIFF@ @HWLOC_DIFF_U@ @HWLOC_DIFF_W@ $srcdir/test-hwloc-annotate.output2 "$file2"


$annotate $srcdir/../../tests/hwloc/xml/nvidiaDGX2.xml $file2 -- dummy -- distances-transform NVLinkBandwidth transitive-closure
@DIFF@ @HWLOC_DIFF_U@ @HWLOC_DIFF_W@ $srcdir/test-hwloc-annotate.output3 "$file2"


$annotate $srcdir/../../tests/hwloc/xml/nvidiaDGX2.xml $file2 -- dummy -- distances-transform NVLinkBandwidth merge-switch-ports
@DIFF@ @HWLOC_DIFF_U@ @HWLOC_DIFF_W@ $srcdir/test-hwloc-annotate.output4 "$file2"


rm -rf "$tmp"
