Eine aufbereitete Darstellung der Quelle

 
     
 
 
Anforderungen  |   Konzepte  |   Entwurf  |   Entwicklung  |   Qualitätssicherung  |   Lebenszyklus  |   Steuerung
 
 
 
 

Benutzer

Quelle  TestSplayTree.cpp

  Sprache: C
 

/* -*- Mode: C++; tab-width: 9; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */


#include "mozilla/ArrayUtils.h"
#include "mozilla/Assertions.h"
#include "mozilla/SplayTree.h"
#include "mozilla/Unused.h"

using mozilla::SplayTree;
using mozilla::SplayTreeNode;

// The following array contains the values 0..999 in random order, as computed
// with the following Python program:
//
//   from random import shuffle
//   x = range(1000)
//   shuffle(x)
//   print(x);
//
static int gValues[] = {
    77899924879560717772533,  215565436821941802322,
    54,  15141653165,  81899,  340401274767278617425629,
    833878440984724519100369490131422169932476823,
    521390781747218376461717532471298720608334788,
    16150028096343048477957296,  333650158199137991,
    39988268935854819671821138813318832189225,  694,
    73588687278519527569697539361989418,  281191792,
    846861351542806570702931585444284217132251253,
    30280822437,  63,  86340949,  78079031,  638890186114,
    152949491207392170460794482877407263909249710,
    61451,  43191562,  33274,  49590123,  36575289,  660745,
    741547669449465605107774205852266247690835765,
    410140122400510664105935230134106959375884361,
    527715840272232102415903117313153463464876406,
    96771338183655519085917248361,  63329499372,  337,
    11,  896523101916244566706533439201222695739553,
    571289918209189357814670866910579246636750891,
    494758341626426772254682588104347184977126498,
    165955241516235497121123791844259995283602417,
    22130885542986,  34592844,  679796363402445492450,
    964749925847637982648635481564867940291159290,
    92959,  712986611954820103622316142204225678314,
    84,  57831514199088050496941274647,  517124848466,
    43867497978265118126,  43583238695122964265591,
    16292164711368656,  805763245581287998525641135,
    6342377281128282288991,   72316,  61314465997,  185,
    312292733624276387926339768960610807656851219,
    58270992751468087059753677,  16451214990085,  335,
    9978,   70577765381531170150720253082754195882,
    87455,  487383885684180829760109194540816906657,
    46944685790738,  600618797950822277842116513255,
    42464316337212967,  118754529917687473174538939,
    66377547424288320,  83729358494332,  17690414,  448,
    893888744171714454691261934606789825671397338,
    31761273713041,  92357413698085012,  72919740357,
    78336014675,  432447192799740267214250367853968,
    12073639188178466568,  398350839268697567428738,
    48,  18270,  220865418374148945353539589307427506,
    26555812846,  33629934930937730442030,  34,  875948,
    212394442719273269157502675751838897862831676,
    59081196685447715,  59857310898,  81,  40842129673,
    64445636266655033136819347020376934236,  60460,
    97074881352251590,  672243793947595632912475258,
    80,  87362352454626272721650533037358,  297609908,
    150206703755260511213198766898992488405974770,
    9367435540,   49997694,  160919434324156757830677,
    183630871640938518344366742552306535200652496,
    23341978731898137116614338488,  508698812559658,
    54920859962196166856393,  1545875603893,   210326,
    4,   9243002,   80491480175365427,  23619,  708451985,
    59647892224012799498338547240,  528288111543568,
    155625759937956545953962382479809557501354414,
    343378843379178556800803592627942576920704707,
    72622311940424,  8797228685,   238817520631946462,
    45729548095744114528630368817,  628493364226110,
    61569,  32053459372141128586995284913935634628,
    88781092,  79854445899669239666732817322,  77350,
    64598742,  685734700683601580639913323858179761,
    6,   84190523473029,  21,  575586902443826646257125,
    64953,  45325213,  87,  97122748516838071179,  732325,
    52,  46876,  55139,  39532797345945,  583989147455776,
    94456988925635,  175834756933860526845864764771,
    2829,   6933527317,   577264319138467819930231115,
    98897876248630161610,  78,  603452965279972413895,
    59166259434842348943,  69943350935527066,  83,  95,
    56166156232962037064,  18750371685631078616771,
    239359537437305673824911681271};

struct SplayInt : SplayTreeNode<SplayInt> {
  explicit SplayInt(int aValue) : mValue(aValue) {}

  static int compare(const SplayInt& aOne, const SplayInt& aTwo) {
    if (aOne.mValue < aTwo.mValue) {
      return -1;
    }
    if (aOne.mValue > aTwo.mValue) {
      return 1;
    }
    return 0;
  }

  int mValue;
};

struct SplayNoCopy : SplayTreeNode<SplayNoCopy> {
  SplayNoCopy(const SplayNoCopy&) = delete;
  SplayNoCopy(SplayNoCopy&&) = delete;

  static int compare(const SplayNoCopy&, const SplayNoCopy&) { return 0; }
};

static SplayTree<SplayNoCopy, SplayNoCopy> testNoCopy;

int main() {
  mozilla::Unused << testNoCopy;

  SplayTree<SplayInt, SplayInt> tree;

  MOZ_RELEASE_ASSERT(tree.empty());

  MOZ_RELEASE_ASSERT(!tree.find(SplayInt(1)));

  static const int N = std::size(gValues);

  // Insert the values, and check each one is findable just after insertion.
  for (int i = 0; i < N; i++) {
    tree.insert(new SplayInt(gValues[i]));
    SplayInt* inserted = tree.find(SplayInt(gValues[i]));
    MOZ_RELEASE_ASSERT(inserted);
    MOZ_RELEASE_ASSERT(tree.findOrInsert(SplayInt(gValues[i])) == inserted);
    tree.checkCoherency();
  }

  // Check they're all findable after all insertions.
  for (int i = 0; i < N; i++) {
    MOZ_RELEASE_ASSERT(tree.find(SplayInt(gValues[i])));
    MOZ_RELEASE_ASSERT(tree.findOrInsert(SplayInt(gValues[i])));
    tree.checkCoherency();
  }

  // Check that non-inserted values cannot be found.
  MOZ_RELEASE_ASSERT(!tree.find(SplayInt(-1)));
  MOZ_RELEASE_ASSERT(!tree.find(SplayInt(N)));
  MOZ_RELEASE_ASSERT(!tree.find(SplayInt(0x7fffffff)));

  // Remove the values, and check each one is not findable just after removal.
  for (int i = 0; i < N; i++) {
    SplayInt* removed = tree.remove(SplayInt(gValues[i]));
    MOZ_RELEASE_ASSERT(removed->mValue == gValues[i]);
    MOZ_RELEASE_ASSERT(!tree.find(*removed));
    delete removed;
    tree.checkCoherency();
  }

  MOZ_RELEASE_ASSERT(tree.empty());

  // Insert the values, and check each one is findable just after insertion.
  for (int i = 0; i < N; i++) {
    SplayInt* inserted = tree.findOrInsert(SplayInt(gValues[i]));
    MOZ_RELEASE_ASSERT(tree.find(SplayInt(gValues[i])) == inserted);
    MOZ_RELEASE_ASSERT(tree.findOrInsert(SplayInt(gValues[i])) == inserted);
    tree.checkCoherency();
  }

  // Check they're all findable after all insertions.
  for (int i = 0; i < N; i++) {
    MOZ_RELEASE_ASSERT(tree.find(SplayInt(gValues[i])));
    MOZ_RELEASE_ASSERT(tree.findOrInsert(SplayInt(gValues[i])));
    tree.checkCoherency();
  }

  // Check that non-inserted values cannot be found.
  MOZ_RELEASE_ASSERT(!tree.find(SplayInt(-1)));
  MOZ_RELEASE_ASSERT(!tree.find(SplayInt(N)));
  MOZ_RELEASE_ASSERT(!tree.find(SplayInt(0x7fffffff)));

  // Remove the values, and check each one is not findable just after removal.
  for (int i = 0; i < N; i++) {
    SplayInt* removed = tree.remove(SplayInt(gValues[i]));
    MOZ_RELEASE_ASSERT(removed->mValue == gValues[i]);
    MOZ_RELEASE_ASSERT(!tree.find(*removed));
    delete removed;
    tree.checkCoherency();
  }

  MOZ_RELEASE_ASSERT(tree.empty());

  // Reinsert the values, in reverse order to last time.
  for (int i = 0; i < N; i++) {
    tree.insert(new SplayInt(gValues[N - i - 1]));
    tree.checkCoherency();
  }

  // Remove the minimum value repeatedly.
  for (int i = 0; i < N; i++) {
    SplayInt* removed = tree.removeMin();
    MOZ_RELEASE_ASSERT(removed->mValue == i);
    delete removed;
    tree.checkCoherency();
  }

  MOZ_RELEASE_ASSERT(tree.empty());

  return 0;
}

Messung V0.5 in Prozent
C=80 H=96 G=88

¤ Dauer der Verarbeitung: 0.12 Sekunden  (vorverarbeitet am  2026-06-05) ¤

*© Formatika GbR, Deutschland






Wurzel

Suchen

PVS Prover

Isabelle Prover

NIST Cobol Testsuite

Cephes Mathematical Library

Vienna Development Method

Haftungshinweis

Die Informationen auf dieser Webseite wurden nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit, noch Qualität der bereit gestellten Informationen zugesichert.

Bemerkung:

Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.






                                                                                                                                                                                                                                                                                                                                                                                                     


Neuigkeiten

     Aktuelles
     Motto des Tages

Software

     Quellcodebibliothek
     Eigene Quellcodes
     Fremde Quellcodes
     Suchen

Aktivitäten

     Artikel über Sicherheit
     Anleitung zur Aktivierung von SSL

Muße

     Gedichte
     Musik
     Bilder

Jenseits des Üblichen ....
    

Besucherstatistik

Besucherstatistik