xref: /xnu-12377.81.4/tests/unit/ecc_test_remove_duplicates.c (revision 043036a2b3718f7f0be807e2870f8f47d3fa0796)
1 /*
2  * Copyright (c) 2000-2024 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 
29 #include <darwintest.h>
30 #include <vm/pmap.h>
31 
32 extern uint32_t remove_bad_ram_duplicates(uint32_t bad_pages_count, ppnum_t *bad_pages);
33 
34 #define UT_MODULE osfmk
35 T_GLOBAL_META(
36 	T_META_NAMESPACE("xnu.unit.ecc_test_remove_duplicates"),
37 	T_META_RADAR_COMPONENT_NAME("xnu"),
38 	T_META_OWNER("tgal2"),
39 	T_META_RUN_CONCURRENTLY(false)
40 	);
41 
42 T_DECL(ecc_rm_dups_zero_elements, "make sure 0 is returned for empty input")
43 {
44 	uint32_t bp_count = 0;
45 	ppnum_t bp[] = {};
46 	bp_count = remove_bad_ram_duplicates(bp_count, bp);
47 	if (bp_count != 0) {
48 		T_FAIL("Expected: bp_count == 0 but got: bp_count == %u", bp_count);
49 	}
50 	T_PASS("bp_count == 0 as expected");
51 }
52 
53 T_DECL(ecc_rm_dups_0, "make sure 1 is returned for [0]")
54 {
55 	uint32_t bp_count = 1;
56 	ppnum_t bp[] = { 0 };
57 	bp_count = remove_bad_ram_duplicates(bp_count, bp);
58 	if (bp_count != 1 || bp[0] != 0) {
59 		T_FAIL("Expected: bp_count == 1 and bp[0] == 0 but got: bp_count == %u, bp[0] == %u", bp_count, bp[0]);
60 	} else {
61 		T_PASS("bp_count == 1 and bp[0] == 0 as expected");
62 	}
63 }
64 
65 T_DECL(ecc_rm_dups_00, "make sure 1 is returned for [0, 0]")
66 {
67 	uint32_t bp_count = 2;
68 	ppnum_t bp[] = { 0, 0 };
69 	bp_count = remove_bad_ram_duplicates(bp_count, bp);
70 	if (bp_count != 1 || bp[0] != 0) {
71 		T_FAIL("Expected: bp_count == 1 and bp[0] == 0 but got: bp_count == %u, bp[0] == %u", bp_count, bp[0]);
72 	} else {
73 		T_PASS("bp_count == 1 and bp[0] == 0 as expected");
74 	}
75 }
76 
77 T_DECL(ecc_rm_dups_001, "make sure 2 is returned for [0, 0, 1]")
78 {
79 	uint32_t bp_count = 3;
80 	ppnum_t bp[] = { 0, 0, 1 };
81 	bp_count = remove_bad_ram_duplicates(bp_count, bp);
82 	if (bp_count != 2 || bp[0] != 0 || bp[1] != 1) {
83 		T_FAIL("Expected: bp_count == 2 and bp == [0, 1] but got: bp_count == %u, bp == [%u, %u]", bp_count, bp[0], bp[1]);
84 	} else {
85 		T_PASS("bp_count == 2 and bp == [0, 1] as expected");
86 	}
87 }
88 
89 T_DECL(ecc_rm_dups_101, "make sure 2 is returned for [1, 0, 1]")
90 {
91 	uint32_t bp_count = 3;
92 	ppnum_t bp[] = { 1, 0, 1 };
93 	bp_count = remove_bad_ram_duplicates(bp_count, bp);
94 	if (bp_count != 2 || bp[0] != 0 || bp[1] != 1) {
95 		T_FAIL("Expected: bp_count == 2 and bp == [0, 1] but got: bp_count == %u, bp == [%u, %u]", bp_count, bp[0], bp[1]);
96 	} else {
97 		T_PASS("bp_count == 2 and bp == [0, 1] as expected");
98 	}
99 }
100 
101 T_DECL(ecc_rm_dups_201, "make sure 3 is returned for [2, 0, 1]")
102 {
103 	uint32_t bp_count = 3;
104 	ppnum_t bp[] = { 2, 0, 1 };
105 	bp_count = remove_bad_ram_duplicates(bp_count, bp);
106 	if (bp_count != 3 || bp[0] != 0 || bp[1] != 1 || bp[2] != 2) {
107 		T_FAIL("Expected: bp_count == 3 and bp == [0, 1, 2] but got: bp_count == %u, bp == [%u, %u, %u]", bp_count, bp[0], bp[1], bp[2]);
108 	} else {
109 		T_PASS("bp_count == 3 and bp == [0, 1, 2] as expected");
110 	}
111 }
112 
113 T_DECL(ecc_rm_dups_2012, "make sure 3 is returned for [2, 0, 1, 2]")
114 {
115 	uint32_t bp_count = 4;
116 	ppnum_t bp[] = { 2, 0, 1, 2 };
117 	bp_count = remove_bad_ram_duplicates(bp_count, bp);
118 	if (bp_count != 3 || bp[0] != 0 || bp[1] != 1 || bp[2] != 2) {
119 		T_FAIL("Expected: bp_count == 3 and bp == [0, 1, 2] but got: bp_count == %u, bp == [%u, %u, %u]", bp_count, bp[0], bp[1], bp[2]);
120 	} else {
121 		T_PASS("bp_count == 3 and bp == [0, 1, 2] as expected");
122 	}
123 }
124 
125 T_DECL(ecc_rm_dups_large, "make sure large input is handled correctly")
126 {
127 	uint32_t bp_count = 1000;
128 	ppnum_t bp[1000];
129 	for (uint32_t i = 0; i < bp_count; i++) {
130 		bp[i] = (i % 10); // Repeated numbers [0-9]
131 	}
132 	bp_count = remove_bad_ram_duplicates(bp_count, bp);
133 	bool valid = (bp_count == 10);
134 	for (uint32_t i = 0; i < bp_count && valid; i++) {
135 		if (bp[i] != i) {
136 			valid = false;
137 		}
138 	}
139 	if (!valid) {
140 		T_FAIL("Expected: bp_count == 10 and bp == [0-9] but got: bp_count == %u", bp_count);
141 	} else {
142 		T_PASS("bp_count == 10 and bp == [0-9] as expected");
143 	}
144 }
145