+
+#if GDB_SELF_TEST
+namespace selftests {
+namespace findvar_tests {
+
+/* Function to test copy_integer_to_size. Store SOURCE_VAL with size
+ SOURCE_SIZE to a buffer, making sure no sign extending happens at this
+ stage. Copy buffer to a new buffer using copy_integer_to_size. Extract
+ copied value and compare to DEST_VALU. Copy again with a signed
+ copy_integer_to_size and compare to DEST_VALS. Do everything for both
+ LITTLE and BIG target endians. Use unsigned values throughout to make
+ sure there are no implicit sign extensions. */
+
+static void
+do_cint_test (ULONGEST dest_valu, ULONGEST dest_vals, int dest_size,
+ ULONGEST src_val, int src_size)
+{
+ for (int i = 0; i < 2 ; i++)
+ {
+ gdb_byte srcbuf[sizeof (ULONGEST)] = {};
+ gdb_byte destbuf[sizeof (ULONGEST)] = {};
+ enum bfd_endian byte_order = i ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
+
+ /* Fill the src buffer (and later the dest buffer) with non-zero junk,
+ to ensure zero extensions aren't hidden. */
+ memset (srcbuf, 0xaa, sizeof (srcbuf));
+
+ /* Store (and later extract) using unsigned to ensure there are no sign
+ extensions. */
+ store_unsigned_integer (srcbuf, src_size, byte_order, src_val);
+
+ /* Test unsigned. */
+ memset (destbuf, 0xaa, sizeof (destbuf));
+ copy_integer_to_size (destbuf, dest_size, srcbuf, src_size, false,
+ byte_order);
+ SELF_CHECK (dest_valu == extract_unsigned_integer (destbuf, dest_size,
+ byte_order));
+
+ /* Test signed. */
+ memset (destbuf, 0xaa, sizeof (destbuf));
+ copy_integer_to_size (destbuf, dest_size, srcbuf, src_size, true,
+ byte_order);
+ SELF_CHECK (dest_vals == extract_unsigned_integer (destbuf, dest_size,
+ byte_order));
+ }
+}
+
+static void
+copy_integer_to_size_test ()
+{
+ /* Destination is bigger than the source, which has the signed bit unset. */
+ do_cint_test (0x12345678, 0x12345678, 8, 0x12345678, 4);
+ do_cint_test (0x345678, 0x345678, 8, 0x12345678, 3);
+
+ /* Destination is bigger than the source, which has the signed bit set. */
+ do_cint_test (0xdeadbeef, 0xffffffffdeadbeef, 8, 0xdeadbeef, 4);
+ do_cint_test (0xadbeef, 0xffffffffffadbeef, 8, 0xdeadbeef, 3);
+
+ /* Destination is smaller than the source. */
+ do_cint_test (0x5678, 0x5678, 2, 0x12345678, 3);
+ do_cint_test (0xbeef, 0xbeef, 2, 0xdeadbeef, 3);
+
+ /* Destination and source are the same size. */
+ do_cint_test (0x8765432112345678, 0x8765432112345678, 8, 0x8765432112345678,
+ 8);
+ do_cint_test (0x432112345678, 0x432112345678, 6, 0x8765432112345678, 6);
+ do_cint_test (0xfeedbeaddeadbeef, 0xfeedbeaddeadbeef, 8, 0xfeedbeaddeadbeef,
+ 8);
+ do_cint_test (0xbeaddeadbeef, 0xbeaddeadbeef, 6, 0xfeedbeaddeadbeef, 6);
+
+ /* Destination is bigger than the source. Source is bigger than 32bits. */
+ do_cint_test (0x3412345678, 0x3412345678, 8, 0x3412345678, 6);
+ do_cint_test (0xff12345678, 0xff12345678, 8, 0xff12345678, 6);
+ do_cint_test (0x432112345678, 0x432112345678, 8, 0x8765432112345678, 6);
+ do_cint_test (0xff2112345678, 0xffffff2112345678, 8, 0xffffff2112345678, 6);
+}
+
+} // namespace findvar_test
+} // namespace selftests
+
+#endif
+
+void _initialize_findvar ();
+void
+_initialize_findvar ()
+{
+#if GDB_SELF_TEST
+ selftests::register_test
+ ("copy_integer_to_size",
+ selftests::findvar_tests::copy_integer_to_size_test);
+#endif
+}