File: UefiSortLibGoogleTest.cpp

package info (click to toggle)
edk2 2025.11-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 338,556 kB
  • sloc: ansic: 2,166,376; asm: 270,725; perl: 235,301; python: 149,839; sh: 34,744; cpp: 23,311; makefile: 3,326; pascal: 1,602; xml: 806; lisp: 35; ruby: 16; sed: 6; tcl: 4
file content (66 lines) | stat: -rw-r--r-- 1,666 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/** @file
  Unit tests for the implementation of UefiSortLib.

  Copyright (c) 2022, Intel Corporation. All rights reserved.
  SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#include <Library/GoogleTestLib.h>

extern "C" {
  #include <Uefi.h>
  #include <Library/SortLib.h>
}

using namespace testing;

INTN
EFIAPI
CompareUint32 (
  IN CONST VOID  *Left,
  IN CONST VOID  *Right
  )
{
  if (*(UINT32 *)Right > *(UINT32 *)Left) {
    return 1;
  } else if (*(UINT32 *)Right < *(UINT32 *)Left) {
    return -1;
  }

  return 0;
}

// Test PerformQuickSort() API from UefiSortLib to verify a UINT32 array
// with 9 elements in ascending order is sorted into descending order.
TEST (PerformQuickSortTest, SortUint32AscendingArray_Size9) {
  CONST UINT32  ArraySize = 9;
  UINT32        BuffActual[ArraySize];
  UINT32        BuffExpected[ArraySize];

  for (UINT32 Index = 0; Index < ArraySize; Index++) {
    BuffActual[Index]   = Index + 1;
    BuffExpected[Index] = ArraySize - Index;
  }

  PerformQuickSort (BuffActual, (UINTN)ArraySize, sizeof (UINT32), (SORT_COMPARE)CompareUint32);
  EXPECT_THAT (BuffActual, ElementsAreArray (BuffExpected, ArraySize));
}

// Test StringCompare() API from UefiSortLib to verify the comparison
// succeeds when the same buffer is compared with itself.
TEST (StringCompareTest, CompareSameBuffer) {
  INTN          RetVal;
  CONST CHAR16  *Buffer = (CHAR16 *)L"abcdefg";

  RetVal = StringCompare (&Buffer, &Buffer);
  EXPECT_EQ (RetVal, 0);
}

int
main (
  int   argc,
  char  *argv[]
  )
{
  testing::InitGoogleTest (&argc, argv);
  return RUN_ALL_TESTS ();
}