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

#
# SPDX-License-Identifier: BSD-3-Clause
# Copyright © 2009 CNRS
# Copyright © 2009-2025 Inria.  All rights reserved.
# Copyright © 2009 Université Bordeaux
# 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"
info="$builddir/hwloc-info"
linuxdir="$HWLOC_top_srcdir/tests/hwloc/linux"
xmldir="$HWLOC_top_srcdir/tests/hwloc/xml"

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

HWLOC_DEBUG_CHECK=1
export HWLOC_DEBUG_CHECK

HWLOC_DONT_ADD_VERSION_INFO=1
export HWLOC_DONT_ADD_VERSION_INFO

: ${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-info.output"

set -e
(
  echo "# (default)"
  $info --if synthetic --input "node:2 core:3 pu:4"
  echo
  echo "# topology infos"
  $info --if synthetic --input "node:2 core:3 pu:4" topology | grep -v hwlocVersion | grep -v ProcessName
  echo
  echo "# topology infos --get-attr"
  $info --if synthetic --input "node:2 core:3 pu:4" --get-attr "info SyntheticDescription" topology
  echo
  echo "# levels support"
  $info --if synthetic --input "node:2 core:3 pu:4" levels support
  echo

  echo "# Core range"
  $info --if synthetic --input "node:2 core:3 pu:4" core:2-4
  echo

  echo "# all ancestors of PU range"
  $info --if synthetic --input "node:2 core:3 pu:4" -n --ancestors pu:10-11
  echo
  echo "# Core ancestors of PU range"
  $info --if synthetic --input "node:2 core:3 pu:4" --ancestor core pu:7-9
  echo
  echo "# L2 ancestor of PU"
  $info --if synthetic --input "node:2 core:2 l2:2 l1d:2 pu:2" --ancestor l2 pu:12
  echo
  echo "# L1 ancestor of PU range"
  $info --if synthetic --input "node:2 core:2 l2:2 l1d:2 pu:2" --ancestor l1 -s pu:7-10
  echo
  echo "# Ancestor caches above L1d"
  $info --if synthetic --input "node:2 l4:2 l3:2 core:2 l2:2 l1d:2 pu:2" --ancestor kind=cache -n -s l1d:4 l1d:8
  echo
  echo "# Descendant CPU objects below L4"
  $info --if synthetic --input "node:2 l4:2 l3:2 core:2 l2:1 pu:2" --descendants kind=cpu -n -s l4:0 l4:3
  echo
  echo "# Descendant Memory objects below L3"
  $info --if synthetic --input "pack:4 [numa] l3:2 [numa] [numa] core:4 pu:2" --descendants kind=memory -n -s l3:1 l3:7
  echo
  echo "# Where a NUMA is attached"
  $info --if synthetic --input "pack:4 [numa] l3:2 [numa] [numa] core:4 pu:2" --ancestor kind=normal --first -s numa:3 numa:14
  echo
  echo "# Allowed cpuset of where NUMA is attached"
  $info --if synthetic --input "pack:4 [numa] l3:2 [numa] [numa] core:4 pu:2" --ancestor kind=normal --first --get-attr "allowed cpuset" numa:3 numa:14
  echo

  echo "# Children of L2 and Core of Node, silent"
  $info --if synthetic --input "node:2 core:2 l2:2 l1d:2 pu:2" --children -s l2:1 node:1.core:1
  echo
  echo "# L1d descendants of Core range, silent"
  $info --if synthetic --input "node:2 core:2 l2:2 l1d:2 pu:2" --descendants l1d -s core:1-2
  echo

  echo "# 2 local memory for one PU"
  $info --if synthetic --input "pack:4 [numa] l3:2 [numa] core:4 pu:2" --local-memory pu:8
  echo
  echo "# 2 local-or-larger memories for one PU, silent"
  $info --if synthetic --input "pack:4 [numa] l3:2 [numa] core:4 pu:2" --local-memory-flags larger -s pu:8
  echo
  echo "# no local-or-larger memory for root, silent"
  $info --if synthetic --input "pack:4 [numa] l3:2 [numa] core:4 pu:2" --local-memory-flags larger -s root
  echo
  echo "# no local-or-smaller memory for one PU, silent"
  $info --if synthetic --input "pack:4 [numa] l3:2 [numa] core:4 pu:2" --local-memory-flags smaller -s pu:8
  echo
  echo "# 3 local-or-smaller memories for on Package, silent"
  $info --if synthetic --input "pack:4 [numa] l3:2 [numa] core:4 pu:2" --local-memory-flags smaller -s pack:1
  echo
  echo "# no strict-local memory for one PU, silent"
  $info --if synthetic --input "pack:4 [numa] l3:2 [numa] core:4 pu:2" --local-memory-flags none -s pu:8
  echo
  echo "# 1 strict-local memory for one NUMANode, silent"
  $info --if synthetic --input "pack:4 [numa] l3:2 [numa] core:4 pu:2" --local-memory-flags none -s node:1
  echo
  echo "# 12 local-all memories for one PU, silent"
  $info --if synthetic --input "pack:4 [numa] l3:2 [numa] core:4 pu:2" --local-memory-flags all\$ -s pu:3
  echo
  echo "# only the smallest locality among 2 local-or-larger memories for one PU, silent"
  $info --if synthetic --input "pack:4 [numa] l3:2 [numa] core:4 pu:2" --local-memory-flags larger --best-memattr locality -s pu:63
  echo
  echo "# only the highest capacity among 2 local-or-larger memories for one PU, silent"
  $info --if synthetic --input "pack:4 [numa(memory=1000000)] l3:2 [numa(memory=1000)] core:4 pu:2" --local-memory-flags larger --best-memattr capacity -s pu:63
  echo
  echo "# default NUMA node of a PU"
  $info --if xml --input $xmldir/64intel64-fakeKNL-SNC4-hybrid.xml -s --default-nodes pu:44
  echo

  echo
  echo "# MCDRAM subtype NUMA filter"
  $info --if xml --input $xmldir/64intel64-fakeKNL-SNC4-hybrid.xml -s 'numa[mcdram]:1-2'
  echo
  echo "# Memory Tier 1 filter"
  $info --if xml --input $xmldir/64intel64-fakeKNL-SNC4-hybrid.xml -s 'numa[tier=1]:3'
  echo
  echo "# NVML subtype OSdev type filter"
  $info --if xml --input $xmldir/power8gpudistances.xml -s 'os[nvml]:all'
  echo
  echo "# GPU OSdev type filter"
  $info --if xml --input $xmldir/power8gpudistances.xml -s 'gpu:3-5'
  echo

  echo
  echo "# cpukinds for the entire machine"
  $info --if xml --input $linuxdir/fakeheterocpunuma.output all
  echo
  echo "# cpukind for a single PU"
  $info --if xml --input $linuxdir/fakeheterocpunuma.output pu:15
  echo

) > "$file"
@DIFF@ @HWLOC_DIFF_U@ @HWLOC_DIFF_W@ $srcdir/test-hwloc-info.output "$file"
rm -rf "$tmp"
