+#if GDB_SELF_TEST
+#include "selftest.h"
+
+namespace selftests {
+
+class regcache_access : public regcache
+{
+public:
+
+ /* Return the number of elements in current_regcache. */
+
+ static size_t
+ current_regcache_size ()
+ {
+ return std::distance (regcache::current_regcache.begin (),
+ regcache::current_regcache.end ());
+ }
+};
+
+static void
+current_regcache_test (void)
+{
+ /* It is empty at the start. */
+ SELF_CHECK (regcache_access::current_regcache_size () == 0);
+
+ ptid_t ptid1 (1), ptid2 (2), ptid3 (3);
+
+ /* Get regcache from ptid1, a new regcache is added to
+ current_regcache. */
+ regcache *regcache = get_thread_arch_aspace_regcache (ptid1,
+ target_gdbarch (),
+ NULL);
+
+ SELF_CHECK (regcache != NULL);
+ SELF_CHECK (regcache->ptid () == ptid1);
+ SELF_CHECK (regcache_access::current_regcache_size () == 1);
+
+ /* Get regcache from ptid2, a new regcache is added to
+ current_regcache. */
+ regcache = get_thread_arch_aspace_regcache (ptid2,
+ target_gdbarch (),
+ NULL);
+ SELF_CHECK (regcache != NULL);
+ SELF_CHECK (regcache->ptid () == ptid2);
+ SELF_CHECK (regcache_access::current_regcache_size () == 2);
+
+ /* Get regcache from ptid3, a new regcache is added to
+ current_regcache. */
+ regcache = get_thread_arch_aspace_regcache (ptid3,
+ target_gdbarch (),
+ NULL);
+ SELF_CHECK (regcache != NULL);
+ SELF_CHECK (regcache->ptid () == ptid3);
+ SELF_CHECK (regcache_access::current_regcache_size () == 3);
+
+ /* Get regcache from ptid2 again, nothing is added to
+ current_regcache. */
+ regcache = get_thread_arch_aspace_regcache (ptid2,
+ target_gdbarch (),
+ NULL);
+ SELF_CHECK (regcache != NULL);
+ SELF_CHECK (regcache->ptid () == ptid2);
+ SELF_CHECK (regcache_access::current_regcache_size () == 3);
+
+ /* Mark ptid2 is changed, so regcache of ptid2 should be removed from
+ current_regcache. */
+ registers_changed_ptid (ptid2);
+ SELF_CHECK (regcache_access::current_regcache_size () == 2);
+}
+
+} // namespace selftests
+#endif /* GDB_SELF_TEST */
+