public void TestWriteNumbers()
        {
            var filename = @"C:\Tests\testWriteNumbers.sav";

            using (FileStream fileStream = new FileStream(filename, FileMode.Create, FileAccess.Write))
            {
                var variable1 = new Variable
                {
                    Label       = "The variable Label",
                    ValueLabels = new Dictionary <double, string>
                    {
                        { 1, "Label for 1" },
                        { 2, "Label for 2" },
                    },
                    Name             = "avariablename_01",
                    PrintFormat      = new OutputFormat(FormatType.F, 8, 2),
                    WriteFormat      = new OutputFormat(FormatType.F, 8, 2),
                    Type             = DataType.Numeric,
                    Width            = 10,
                    MissingValueType = 1
                };
                variable1.MissingValues[0] = 999;
                var variable2 = new Variable
                {
                    Label       = "Another variable",
                    ValueLabels = new Dictionary <double, string>
                    {
                        { 1, "this is 1" },
                        { 2, "this is 2" },
                    },
                    Name             = "avariablename_02",
                    PrintFormat      = new OutputFormat(FormatType.F, 8, 2),
                    WriteFormat      = new OutputFormat(FormatType.F, 8, 2),
                    Type             = DataType.Numeric,
                    Width            = 10,
                    MissingValueType = 1
                };
                variable2.MissingValues[0] = 999;
                var variables = new List <Variable>
                {
                    variable1,
                    variable2
                };

                var options = new SpssOptions();

                using (var writer = new SpssWriter(fileStream, variables, options))
                {
                    var newRecord = writer.CreateRecord();
                    newRecord[0] = 15d;
                    newRecord[1] = 15.5d;
                    writer.WriteRecord(newRecord);
                    newRecord    = writer.CreateRecord();
                    newRecord[0] = null;
                    newRecord[1] = 200d;
                    writer.WriteRecord(newRecord);
                    writer.EndFile();
                }
            }
        }
        public void Reallocate_UncompressedFile_CopiesAllReordsAndVariables()
        {
            //after this test the new file is substantially smaller thean the original

            int originalRecordCount;
            int originalVariableCount;

            using (FileStream fileStream = new FileStream(@"TestFiles/230mbfile.sav.gz", FileMode.Open,
                                                          FileAccess.Read, FileShare.Read, 2048 * 10, FileOptions.SequentialScan))
                using (GZipStream deCompressStream = new GZipStream(fileStream, CompressionMode.Decompress, true))
                    using (MemoryStream memStream = new MemoryStream())
                    {
                        deCompressStream.CopyTo(memStream);

                        memStream.Position = 0;

                        SpssReader spssDataset = new SpssReader(memStream);

                        originalRecordCount   = spssDataset.Records.Count();
                        originalVariableCount = spssDataset.Variables.Count;

                        var deallocator = new DataReallocator();

                        // code under test
                        var records = deallocator.ReAllocateRecords(spssDataset.Records);

                        using (FileStream outputFileStream = new FileStream(@"TestFiles/34mbfile-after-reallocation.sav", FileMode.Create, FileAccess.Write))
                        {
                            using (var writer = new SpssWriter(outputFileStream, spssDataset.Variables, new SpssOptions()
                            {
                                Cases = originalRecordCount
                            }))
                            {
                                foreach (var record in records)
                                {
                                    var newRecord = writer.CreateRecord(record);
                                    writer.WriteRecord(newRecord);
                                }
                                writer.EndFile();
                            }
                        }
                    }

            var results = FileValidator.CheckSpssFile(@"TestFiles/34mbfile-after-reallocation.sav");

            Assert.AreEqual(originalRecordCount, results.RecordCount);
            Assert.AreEqual(originalVariableCount, results.VariableCount);
        }
예제 #3
0
        public void TestCopyFile()
        {
            using (FileStream fileStream = new FileStream(@"TestFiles/cakespss1000similarvars.sav", FileMode.Open,
                                                          FileAccess.Read,
                                                          FileShare.Read, 2048 * 10, FileOptions.SequentialScan))
            {
                using (FileStream writeStream = new FileStream("newcake1000similarvars.sav", FileMode.Create, FileAccess.Write))
                {
                    SpssReader spssDataset = new SpssReader(fileStream);

                    SpssWriter spssWriter = new SpssWriter(writeStream, spssDataset.Variables);

                    foreach (var record in spssDataset.Records)
                    {
                        var newRecord = spssWriter.CreateRecord(record);
                        spssWriter.WriteRecord(newRecord);
                    }

                    spssWriter.EndFile();
                }
            }
            Assert.IsTrue(true); // To check errors, set <DeleteDeploymentDirectoryAfterTestRunIsComplete> to False and open the file
        }
예제 #4
0
        public void TestWriteDoubleLongStringVar()
        {
            var filename = @"testWriteDoubleLongString.sav";

            using (FileStream fileStream = new FileStream(filename, FileMode.Create, FileAccess.Write))
            {
                var var1 = new Variable("stringvar_01")
                {
                    Label     = "This is a string variable",
                    Type      = DataType.Text,
                    Width     = 50,
                    TextWidth = 5000
                };

                var var2 = new Variable("stringvar_02")
                {
                    Label       = "This is a string variable",
                    PrintFormat = new OutputFormat(FormatType.A, 60),
                    WriteFormat = new OutputFormat(FormatType.A, 70),
                    Type        = DataType.Text,
                    Width       = 50,
                    TextWidth   = 5000
                };

                var var3 = new Variable("stringvar_03")
                {
                    Label       = "This is a string variable",
                    PrintFormat = new OutputFormat(FormatType.A, 60),
                    WriteFormat = new OutputFormat(FormatType.A, 70),
                    Type        = DataType.Text,
                    Width       = 50,
                    TextWidth   = 5000
                };

                var var4 = new Variable("stringvar_04")
                {
                    Label     = "This is a string variable",
                    Type      = DataType.Text,
                    Width     = 50,
                    TextWidth = 50
                };

                var var5 = new Variable("stringvar_05")
                {
                    Label     = "This is a string variable",
                    Type      = DataType.Text,
                    Width     = 4,
                    TextWidth = 4
                };

                var var6 = new Variable("LIKES")
                {
                    Label       = "This is a string variable",
                    PrintFormat = new OutputFormat(FormatType.A, 255),
                    WriteFormat = new OutputFormat(FormatType.A, 255),
                    Type        = DataType.Text,
                    Width       = 50,
                    TextWidth   = 1026
                };


                var variables = new List <Variable>
                {
                    var1,
                    var2,
                    var3,
                    var4,
                    var5,
                    var6
                };

                var options = new SpssOptions();

                using (var writer = new SpssWriter(fileStream, variables, options))
                {
                    object[] newRecord = writer.CreateRecord();
                    newRecord[0] = "test";
                    newRecord[1] = "test";
                    newRecord[2] = "Lorem";
                    //newRecord[3] = 200d;
                    writer.WriteRecord(newRecord);
                    writer.EndFile();
                }
            }


            FileStream readFileStream = new FileStream(filename, FileMode.Open, FileAccess.Read,
                                                       FileShare.Read, 2048 * 10, FileOptions.SequentialScan);

            TestSpssReader.ReadData(readFileStream, out int varCount, out int rowCount);

            Assert.AreEqual(varCount, 6, "Variable count does not match");
            Assert.AreEqual(rowCount, 1, "Rows count does not match");
        }
예제 #5
0
        public void TestWriteLongWeirdString()
        {
            var filename = @"testTestWriteLongWeirdString.sav";

            var varString1 = new Variable("stringvar_01")
            {
                Label     = "This is a string variable",
                Type      = DataType.Text,
                TextWidth = 5000,
            };

            var variable1 = new Variable("avariablename_01")
            {
                Label       = "The variable Label",
                ValueLabels = new Dictionary <double, string>
                {
                    { 1, "Label for 1" },
                    { 2, "Label for 2" },
                },
                PrintFormat      = new OutputFormat(FormatType.F, 8, 2),
                WriteFormat      = new OutputFormat(FormatType.F, 8, 2),
                Type             = DataType.Numeric,
                Width            = 10,
                MissingValueType = MissingValueType.OneDiscreteMissingValue,
                MissingValues    = { [0] = 999 }
            };

            var varString = new Variable("stringvar_02")
            {
                Label           = "This is a string variable",
                Type            = DataType.Text,
                TextWidth       = 60,
                Alignment       = Alignment.Centre,
                MeasurementType = MeasurementType.Ordinal
            };

            var variable2 = new Variable("avariablename_02")
            {
                Label       = "Another variable",
                ValueLabels = new Dictionary <double, string>
                {
                    { 1, "this is 1" },
                    { 2, "this is 2" },
                },
                PrintFormat      = new OutputFormat(FormatType.F, 8, 2),
                WriteFormat      = new OutputFormat(FormatType.F, 8, 2),
                Type             = DataType.Numeric,
                Width            = 10,
                MissingValueType = MissingValueType.OneDiscreteMissingValue,
                MissingValues    = { [0] = 999 }
            };

            var variables = new List <Variable>()
            {
                varString1,
                variable1,
                varString,
                variable2
            };

            var options = new SpssOptions();

            using (FileStream writeFileStream = new FileStream(filename, FileMode.Create, FileAccess.Write))
            {
                using (var writer = new SpssWriter(writeFileStream, variables, options))
                {
                    var newRecord = writer.CreateRecord();
                    // Exactly 500
                    newRecord[0] =
                        "Lorem ipsum dolor sit amet, consectetur adipiscing elit. In iaculis neque eget neque semper, vitae lacinia libero rhoncus. Mauris tellus lorem, imperdiet vitae bibendum ac, rhoncus nec dui. Donec in consequat leo. Nunc at ante nec metus aliquam hendrerit quis a augue. Suspendisse faucibus nunc mauris, sed faucibus mauris bibendum et. Sed auctor, dolor non luctus interdum, tellus neque auctor dui, sit amet luctus neque risus vel nibh. Nullam ornare ultricies quam. Vestibulum eget erat sit nullam.";
                    newRecord[1] = 15d;
                    newRecord[2] = "L߀rêϻ iρѕüm ԁòɭor ѕìt ρoѕùerê.";
                    newRecord[3] = 15.5d;
                    writer.WriteRecord(newRecord);

                    newRecord = writer.CreateRecord();

                    newRecord[0] =
                        "Lòrêm ìpsúϻ dòlòr ѕït àϻet, c߀ԉsèctetûr aᏧìpiѕciԉg elìt. Vëѕtìƅùɭuϻ ac ipsum ɭëctùѕ. Craѕ vìtaè ѵïѵërrä tortòr. Nunc vìtàë eѕt värìúѕ, ɭãorèët tòrt߀r á, ïɑcûlíѕ sãpíen. Phɑsêɭɭus metus ϻáɢлå, fêûɢíàt ä ɋùåϻ âɭïqúám, ѕcëlérïѕqüé mòlêѕtiè sɑpiëԉ. Ut núлc ԉïѕl, éffìcïtúr vel curѕûѕ à, ãɭiqûãϻ ѕit áϻet úrnå. Maúrïs conԁìϻentùm ornãrë pùrús, ρհäretrã bɭãлԁít ɭê߀ c߀ϻϻòᏧ߀ eû. Mãúrìs égët mäѕѕä nèc ѕéϻ vòlútρɑt elëϻentúϻ ɋûis ɑ dùï. Curåƅìtûr iԉterᏧùϻ pùlѵíԉàr fèɭïѕ, èú fåcïlísis ɭëctûѕ ԁàρìƅüs sit àϻét. Crãѕ témpüs ëlëϻéлtüm mëtùs. Fúsce ëх íρsûϻ, fríԉgílɭâ àt léctùs ét, lùctùѕ pɭɑcérat ρùrûѕ. Måéceлaѕ nec lèò ɭeò. Víѵàϻus ѵüɭpútɑtë lìƅèro êɢêt ѵ߀ɭútpat ѵòɭùtpàt. Aëлèàԉ âc dïctúm mëtuѕ, лêc c߀лdìmëлtum áԉte. M߀rbï ɭãcüѕ ríѕûs, imρërᏧíêt volütpát léo л߀л, lúctùs tëmpús ߀rcí. D߀ԉêc äc aɭiquɑm elit. Cúráƅítur tíncíԁúnt åԉté ϻollís, ѕòԁálëѕ ëlit ëǥèt, pëɭléntèѕɋûe äùgúé. Nuɭɭâ èt éХ fèɭìs. Fuscë ɭoƅortìs l߀reϻ ԉec neqûe ùltrícés, á ƃɭâԉdít tùrρïѕ còԉѕèɋùät. Séԁ èü cürsûs åügúè, iᏧ c߀лѕëɋuãt Ꮷoɭ߀r. Seԁ ɑccumsâԉ, doɭ߀r iԉterdüm mòɭëѕtíê åliqúèt, riѕus ԉìsɭ iåcùlïs ántè, ԉ߀л ԁïǥԉíssíϻ ϻâɢnå ԉìbɦ ɑt ärcú. Ut éffïcitûr ét ԁìàϻ vitáê ԁìctùϻ. Etiaϻ üt ɑüct߀r nïƃh. Iлtêɢér èrât sâρiéԉ, prètìuϻ á êst ԉëc, ornɑrê lûctûѕ лìsl. Pêlɭëԉtèѕɋuë ϻàlesüáԁa díctûm èԉím íԁ áɭìqüèt. SusρêԉᏧiѕse sít ãmët ߀rnåre rïѕus, vel íácùɭìs lɑcúѕ. Pհäѕellûs յûsto mètúѕ, mäxïϻûѕ âc ρretíuϻ ѵêl, sãgìttìѕ faúciƃuѕ l߀rêϻ. Vïѵãϻüs qúïs conѵäɭlis ϻï, ɋuïs fèùɢïåt лìsì. Pháѕêɭɭûѕ trïѕtiqùê ɭɑcûѕ äc ϻòɭestïé mälésúadâ. Qûìsqûê ρrëtìum, níbh êgét rütrùϻ ѕolɭïcitüᏧíԉ, métûѕ lïƃër߀ fáücíƅuѕ ɭectúѕ, élemèԉtüϻ ρortɑ ɭïǥûlɑ dol߀r pûlѵïлɑr èr߀s. Aéԉêɑл nèc rütrûϻ pûruѕ. Nɑm vèl ԉìsí ϻàgnà. Cùräƃìtur ut ρ߀sûèrè յüѕtò. Sèԁ effìcìtûr p߀rttïtòr dïäϻ, ä fèûgíât vëɭít ïԉtérᏧüm éü. SêᏧ ut còлѕéqúat ɭãcûs. Iԉ ïn c߀ԉᏧïϻéлtum tûrpìs, sìt aϻet c߀ngûé erät. Qùìѕɋûe ƃɭàԉᏧit làcùѕ iϻpêrᏧìét ѕàpíeл ƅíƅéndüm ԁìɢniѕsím. Mòrbi bïbèлdüm ѵ߀lùtρät ԉíѕl. Sëd ùltrïcíeѕ teɭlûѕ nòԉ ɋûäm âûctòr, seᏧ ρɭacèrât lìgûlɑ temρòr. Mòrƃì imρérᏧièt coԉǥùé qûáϻ, ïᏧ pulѵíлar ԁüi. Pélɭêԉtêѕqúë mòlêstïè êХ t߀rtor. Mäürìs ѵìtâê rïѕùs ìd ߀rcï mâttis fiлïƅüѕ. Dònéc cûrsûѕ soɭlícìtuԁïԉ лïsl at sêϻpër. Doлèc ɦêndrërit ɭêctüѕ á líberò ϻɑɭèѕuãdâ, ѕit ɑϻét ϻáӽìmüѕ jùѕt߀ ültricêѕ. Vëstïƃùlûm ɭiƅerò nísí, vèstïƃûlum äc coԉѕêqúãt åc, còԉѕèctêtûr л߀ԉ eros. Mäûris åɭíɋûåm àrcü cùrѕûѕ, heԉᏧrërït ìρsùϻ èü, ѵëɦïcüɭá fèɭìs. Phàsèllûs ρosuère teɭɭús eɢèt ɭïǥúlã måttïѕ, ѕit àmét viѵerrå ùrnɑ ãliɋûêt. Pélleлtëѕqûë ìԁ tòrtor aлtè. Aeлèãԉ ѕ߀llicìtüᏧïn véɭit ìᏧ pëlɭeԉtësɋué éùïsϻ߀ԁ. Nùɭɭâ gráѵìdá tìԉciԁúԉt ùrлá, ѕëԁ còлԁïméлtûm maùríѕ trìstíɋúé ut. Cräѕ mɑttïѕ fêɭïs ɋûïs cursus íϻρerԁiet. Iлtegèr c߀лgùë låcìлia tûrpís ùt ѵeհïcuɭa. Dùís lëctus úrnå, ѕéϻper å ѵíѵërrà ïᏧ, ϻãleѕuâᏧå ût ëԉïm. Aeԉêãԉ rïsuѕ êrät, èǥéstâs a l߀ƃòrtís ín, ɑɭíɋûët finïƅùѕ sëm. Vëѕtïƃulúϻ հëԉԁrérït éԉim ԉèc äùctor Ꮷïǥлìѕѕím. Nam ìàcúɭïs ɭäcùѕ eǥét ԁui orлáre ïãcûɭïs. Ut íᏧ ѵârïúѕ лëqúè, êgét tëϻp߀r fèliѕ. Sêd ρòrttitor ѵíѵérrà léò sít åmèt ϻ߀ɭɭïs. Núllä comϻ߀ԁ߀ èffícìtûr ԉïѕl. Donec vârìüs ԉulɭá ѕït ɑϻét ѕàpìëл ԁiɢлísѕíϻ, ìԉ uɭɭɑmcorper òᏧío seϻρér. M߀rƃi c߀mϻoԁ߀ veɭït ùt c߀лᏧíϻeԉtûm tíncìԁùлt. Veѕtïbülûm cursüѕ dictûϻ fêlïѕ vëɭ fìлíƃus. Clasѕ àptênt tåcìtí ѕòcïòsqü ãᏧ lít߀ra torquent pêr c߀nüƃíɑ nostrâ, per ìncèpt߀ѕ hiϻeԉáé߀ѕ. Susρéndïѕsé ût ԉüлc uɭtricêѕ maǥԉã vúɭpûtätë pharétrɑ non lâcínïâ dïâϻ. Pèɭleԉtéѕɋúe ǥràѵídã sodâléѕ sèm, ët coԉgûë лùllà c߀nѕèctëtúr ɋûìs. Cúrãƃítûr sùѕcïρït àrcù rísùѕ, ԉoл còϻmoᏧo ɋùâm êɭeíféԉԁ ѵêɭ. Maêcéԉàѕ m߀ɭɭíѕ núɭlä àc ɭòbòrtís fïлïƃüs. Vivãϻúѕ лec äԉte ѕâρíéԉ. Sëԁ scélërísqüe mágna ѕëm, ëǥèt volutpät êx trístiqùê ïԁ. Intèɢer eléifënԁ ɭɑòrêet tòrtor, èt ѕodaɭès лûɭlá ƃlɑԉdit seᏧ. Qùïѕɋúê eü ρùrûs ѵòlütpat, maɭêsüaᏧá odïò nëc, c߀nԁïϻëԉtûm nìsi. Vêstìƅùɭùϻ ѕ߀ɭɭicìtùdíԉ eròѕ ëget úllamcòrρer poѕuere. Intêgér êffícítûr åúctor alïqûèt. Nuԉc ѕcêlèrísqüe ѕït âmët èɭìt ëgét rútrúm. Péɭɭëntêsɋuë vítâê prètiüm ϻëtüs, at auctor êӽ. Qüiѕɋùê ϻëtüs mâgлa, Ꮷàρiƅuѕ àc séϻ ùt, ɭàciԉiɑ pհɑretrɑ ɋuàϻ. Pëɭɭêлtësɋüé ëúìsm߀ԁ ɭòƅortis ѵèlit, ìԁ scèɭérisquè qúaϻ áccüϻsãn èü. Sed ín libêrò ɭuctùs, ѕêmρèr lè߀ at, ϻɑttìѕ ɭéctüs. Duïѕ eleméлtúm, lëctüѕ ѕëԁ sèϻpér coԉvâɭlïѕ, Ꮷûí ëx ùllɑϻc߀rρêr mɑúriѕ, vëɭ süscïρít éԉïϻ èr߀ѕ ultrïcès ѕàρïéл. Nûԉc éffìcïtûr léò ëù vìѵérrá moɭlíѕ. Etìäϻ Ꮷáρïbüs ɭobòrtïs ѵuɭρütátë. Pèlɭèntësɋùê at commoԁò íρѕúm. Düíѕ téϻpûs ѵéհïcúɭɑ êrɑt ɭuctüs ëɭëïfëлԁ. Sed íd vëstibùlum ùrԉɑ. Fuscê rutrûϻ, vêlít äc tèϻρor måléѕûãᏧá, télɭüs лìѕí fåúcìƅuѕ ϻåǥԉa, ѵìtaé ϻãttìѕ Ꮷíɑm ɭêctuѕ vïtàê ߀rcï. Péllentesqûè ãliqúam consèctetûr ëɢeѕtãѕ. Dòԉec ïԁ ïåcûlís dúi. Nûɭláϻ ɭoƅ߀rtìs nìѕï ëǥèt Ꮷüi ëuïsmòԁ pɭacërát. Fûscë bìbéԉԁúm, ɭìɢûlá ѵël ѵêɦïcùɭå p߀ѕùëre, лuɭɭa ϻãѕѕå ɭacínía ɭïǥula, ïԉ s߀llícìtüdín erat tùrpís éü ìpsûϻ. Nüԉc conԁimeлtûϻ mäɭésúáԁå làcúѕ. Doԉéc ãc mälêѕüäᏧã âüǥùe, ìԉ ɭäcìлiá ɭigüɭâ. Núɭɭɑϻ ëѕt ãntè, úlláϻcòrρêr c߀лsêɋúåt diám лòn, ëɢêѕtàs ãɭïɋuâϻ ԉuлc. Pèɭɭëлtesɋúê háƃïtänt ϻorbì trïstíqùé ѕèԉectus êt лetuѕ ët maléѕûaᏧä fɑmës åc tùrρïѕ êɢêѕtãѕ. Nülɭä fɑcìɭiѕï. Måüris tìлcìdúnt ipѕúϻ ɭ߀rém, ѕëᏧ ѵiѵërrã èлím ѕeϻpêr ût. Aêлëäԉ coлváɭɭis ρèɭɭéԉtèѕqúê qûàm, ѕéԁ ƅɭɑԉԁit Ꮷïâϻ véɦïcuɭa métuѕ.";
                    newRecord[1] = 150d;
                    newRecord[2] = null;
                    newRecord[3] = 150d;
                    writer.WriteRecord(newRecord);

                    newRecord = writer.CreateRecord();

                    newRecord[0] =
                        "Lòrèm ipѕüϻ Ꮷòlor ѕít ámet, coԉѕéctëtùr ãԁiρìscíлǥ êlìt. Iл ïacûlis ԉêqûë êgét nèɋùé ѕêmpër, vítɑe läcïлíâ ɭíbèr߀ rhoncüѕ. Mâûríѕ tèllus lorém, ìmρérԁiët ѵìtɑe ƃïƅèԉdúm ác, rɦòncûs ԉéc dùi. D߀nëc ïn c߀лséɋûãt lèo. Núлc àt änte ԉec ϻêtüѕ åɭïqûåm henԁrérít qüís á âüǥûe. SûsρëԉᏧísѕê fåúcïbüs лunc mäûriѕ, ѕeᏧ fàùcíƅüѕ mâúríѕ ƅïbêndúm èt. SêᏧ ãûctor, Ꮷòɭor n߀n lüctuѕ ïлterdùm, téllus néqúë àûct߀r ԁuì, ѕìt ámët ɭùctuѕ ԉeɋùé ríѕûs veɭ niƃɦ. Nüllaϻ ߀rԉåré üɭtrícìës qùãm. Vèѕtïbulum ëɢet erɑt ѕït ԉúɭɭãm.";
                    newRecord[1] = null;
                    newRecord[2] = "Lorem ipsum dolor sit posuere.";
                    newRecord[3] = 200d;
                    writer.WriteRecord(newRecord);
                    writer.EndFile();
                }
            }

            ReadFile(filename, out int varCount, out int rowCount);

            Assert.AreEqual(varCount, 4, "Variable count does not match");
            Assert.AreEqual(rowCount, 3, "Rows count does not match");
        }
예제 #6
0
        public void TestWriteNumbers()
        {
            var filename = @"testWriteNumbers.sav";

            var variables = new List <Variable>
            {
                new Variable("avariablename_01")
                {
                    Label       = "The variable Label",
                    ValueLabels = new Dictionary <double, string>
                    {
                        { 1, "Label for 1" },
                        { 2, "Label for 2" },
                    },
                    PrintFormat      = new OutputFormat(FormatType.F, 8, 2),
                    WriteFormat      = new OutputFormat(FormatType.F, 8, 2),
                    Type             = DataType.Numeric,
                    Width            = 10,
                    MissingValueType = MissingValueType.OneDiscreteMissingValue
                },
                new Variable("avariablename_02")
                {
                    Label       = "Another variable",
                    ValueLabels = new Dictionary <double, string>
                    {
                        { 1, "this is 1" },
                        { 2, "this is 2" },
                    },
                    PrintFormat      = new OutputFormat(FormatType.F, 8, 2),
                    WriteFormat      = new OutputFormat(FormatType.F, 8, 2),
                    Type             = DataType.Numeric,
                    Width            = 10,
                    MissingValueType = MissingValueType.OneDiscreteMissingValue
                }
            };

            variables[0].MissingValues[0] = 999;
            variables[1].MissingValues[0] = 999;

            var options = new SpssOptions();

            using (FileStream fileStream = new FileStream(filename, FileMode.Create, FileAccess.Write))
            {
                using (var writer = new SpssWriter(fileStream, variables, options))
                {
                    var newRecord = writer.CreateRecord();
                    newRecord[0] = 15d;
                    newRecord[1] = 15.5d;
                    writer.WriteRecord(newRecord);
                    newRecord    = writer.CreateRecord();
                    newRecord[0] = null;
                    newRecord[1] = 200d;
                    writer.WriteRecord(newRecord);
                    writer.EndFile();
                }
            }

            ReadFile(filename, out int varCount, out int rowCount);

            Assert.AreEqual(varCount, 2, "Variable count does not match");
            Assert.AreEqual(rowCount, 2, "Rows count does not match");
        }
예제 #7
0
        public void TestWriteDoubleLongStringVar()
        {
            var filename = @"c:\temp\testWriteDoubleLongString.sav";

            using (FileStream fileStream = new FileStream(filename, FileMode.Create, FileAccess.Write))
            {
                var var1 = new Variable("stringvar_01")
                {
                    Label     = "This is a string variable",
                    Type      = DataType.Text,
                    Width     = 50,
                    TextWidth = 5000
                };

                var var2 = new Variable("stringvar_02")
                {
                    Label       = "This is a string variable",
                    PrintFormat = new OutputFormat(FormatType.A, 60),
                    WriteFormat = new OutputFormat(FormatType.A, 70),
                    Type        = DataType.Text,
                    Width       = 50,
                    TextWidth   = 5000
                };

                var var3 = new Variable("stringvar_03")
                {
                    Label       = "This is a string variable",
                    PrintFormat = new OutputFormat(FormatType.A, 60),
                    WriteFormat = new OutputFormat(FormatType.A, 70),
                    Type        = DataType.Text,
                    Width       = 50,
                    TextWidth   = 5000
                };

                var var4 = new Variable("stringvar_04")
                {
                    Label     = "This is a string variable",
                    Type      = DataType.Text,
                    Width     = 50,
                    TextWidth = 50
                };

                var var5 = new Variable("stringvar_05")
                {
                    Label     = "This is a string variable",
                    Type      = DataType.Text,
                    Width     = 4,
                    TextWidth = 4
                };

                var var6 = new Variable("LIKES")
                {
                    Label       = "This is a string variable",
                    PrintFormat = new OutputFormat(FormatType.A, 255),
                    WriteFormat = new OutputFormat(FormatType.A, 255),
                    Type        = DataType.Text,
                    Width       = 50,
                    TextWidth   = 1026
                };


                var variables = new List <Variable>
                {
                    var1,
                    var2,
                    var3,
                    var4,
                    var5,
                    var6
                };

                var options = new SpssOptions();

                using (var writer = new SpssWriter(fileStream, variables, options))
                {
                    object[] newRecord = writer.CreateRecord();
                    newRecord[0] = "test";
                    newRecord[1] = "test";
                    newRecord[2] = "Lorem";
                    //newRecord[3] = 200d;
                    writer.WriteRecord(newRecord);
                    writer.EndFile();
                }
            }
        }
        public void Reallocate_PSPPCompressedFile_CopiesAllReordsAndVariables()
        {
            //after this test the new file is the same size as the original

            int originalRecordCount;
            int originalVariableCount;

            using (FileStream fileStream = new FileStream(@"TestFiles/34mbfile.sav.gz", FileMode.Open,
                                                          FileAccess.Read, FileShare.Read, 2048 * 10, FileOptions.SequentialScan))
                using (GZipStream deCompressStream = new GZipStream(fileStream, CompressionMode.Decompress, true))
                    using (MemoryStream memStream = new MemoryStream())
                    {
                        deCompressStream.CopyTo(memStream);
                        memStream.Position = 0;

                        SpssReader spssDataset = new SpssReader(memStream);

                        originalRecordCount   = spssDataset.Records.Count();
                        originalVariableCount = spssDataset.Variables.Count();

                        var deallocator = new DataReallocator();

                        // code under test
                        var records = deallocator.ReAllocateRecords(spssDataset.Records);

                        var variables = spssDataset.Variables
                                        .Where(x => x.Name == "qhidimpconcepteval" || x.Name == "qhidimpproduct1")
                                        .ToList();

                        var datalist = new List <string>();

                        foreach (var record in records)
                        {
                            foreach (var variable in variables)
                            {
                                var data = record.GetValue(variable)?.ToString()?.Trim();

                                if (!string.IsNullOrEmpty(data))
                                {
                                    datalist.Add(data);
                                }
                            }
                        }

                        using (FileStream outputFileStream = new FileStream(@"4mbfile-after-reallocation.sav", FileMode.Create, FileAccess.Write))
                        {
                            using (var writer = new SpssWriter(outputFileStream, spssDataset.Variables, new SpssOptions()))
                            {
                                foreach (var record in records)
                                {
                                    var newRecord = writer.CreateRecord(record);
                                    writer.WriteRecord(newRecord);
                                }
                                writer.EndFile();
                            }
                        }
                    }

            var results = FileValidator.CheckSpssFile(@"4mbfile-after-reallocation.sav");

            Assert.AreEqual(originalRecordCount, results.RecordCount);
            Assert.AreEqual(originalVariableCount, results.VariableCount);
        }
        public void TestWriteString()
        {
            var filename = @"C:\Tests\testWriteString.sav";

            using (FileStream fileStream = new FileStream(filename, FileMode.Create, FileAccess.Write))
            {
                var varString1 = new Variable
                {
                    Label     = "This is a string variable",
                    Name      = "stringvar_01",
                    Type      = DataType.Text,
                    TextWidth = 500,
                };

                var variable1 = new Variable
                {
                    Label       = "The variable Label",
                    ValueLabels = new Dictionary <double, string>
                    {
                        { 1, "Label for 1" },
                        { 2, "Label for 2" },
                    },
                    Name             = "avariablename_01",
                    PrintFormat      = new OutputFormat(FormatType.F, 8, 2),
                    WriteFormat      = new OutputFormat(FormatType.F, 8, 2),
                    Type             = DataType.Numeric,
                    Width            = 10,
                    MissingValueType = 1
                };
                variable1.MissingValues[0] = 999;

                var varString = new Variable
                {
                    Label       = "This is a string variable",
                    Name        = "stringvar_02",
                    PrintFormat = new OutputFormat(FormatType.A, 60),
                    WriteFormat = new OutputFormat(FormatType.A, 60),
                    Type        = DataType.Text,
                    Width       = 60,
                    TextWidth   = 60,
                };

                var variable2 = new Variable
                {
                    Label       = "Another variable",
                    ValueLabels = new Dictionary <double, string>
                    {
                        { 1, "this is 1" },
                        { 2, "this is 2" },
                    },
                    Name             = "avariablename_02",
                    PrintFormat      = new OutputFormat(FormatType.F, 8, 2),
                    WriteFormat      = new OutputFormat(FormatType.F, 8, 2),
                    Type             = DataType.Numeric,
                    Width            = 10,
                    MissingValueType = 1
                };
                variable2.MissingValues[0] = 999;

                var variables = new List <Variable>
                {
                    varString1,
                    variable1,
                    varString,
                    variable2
                };

                var options = new SpssOptions();

                using (var writer = new SpssWriter(fileStream, variables, options))
                {
                    var newRecord = writer.CreateRecord();
                    // Exactly 500
                    newRecord[0] = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. In iaculis neque eget neque semper, vitae lacinia libero rhoncus. Mauris tellus lorem, imperdiet vitae bibendum ac, rhoncus nec dui. Donec in consequat leo. Nunc at ante nec metus aliquam hendrerit quis a augue. Suspendisse faucibus nunc mauris, sed faucibus mauris bibendum et. Sed auctor, dolor non luctus interdum, tellus neque auctor dui, sit amet luctus neque risus vel nibh. Nullam ornare ultricies quam. Vestibulum eget erat sit nullam.";
                    newRecord[1] = 15d;
                    newRecord[2] = "adsf ñlkj";
                    newRecord[3] = 15.5d;
                    writer.WriteRecord(newRecord);

                    newRecord = writer.CreateRecord();
                    // 600, should be cut to 500
                    newRecord[0] = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. In elementum sed justo eu pulvinar. Maecenas non laoreet justo, eget ultrices dolor. Praesent sit amet sodales erat. Proin condimentum, metus et pulvinar ultrices, massa erat hendrerit lorem, vel mattis dolor ante id sem. Fusce laoreet mi tortor, ut interdum ipsum laoreet vel. Nullam lorem mauris, vulputate luctus velit placerat, scelerisque vehicula elit. Phasellus gravida ante quis augue convallis venenatis. Integer bibendum purus non felis interdum, quis fermentum tellus sodales. Fusce commodo ultrices leo ut vulputate. Quisque metus.";
                    newRecord[1] = 150d;
                    newRecord[2] = null;
                    newRecord[3] = 150d;
                    writer.WriteRecord(newRecord);

                    newRecord = writer.CreateRecord();
                    // 255 chars
                    newRecord[0] = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. In iaculis neque eget neque semper, vitae lacinia libero rhoncus. Mauris tellus lorem, imperdiet vitae bibendum ac, rhoncus nec dui. Donec in consequat leo. Nunc at ante nec metus aliquam hendrerit_";
                    newRecord[1] = null;
                    // 300 chars, should be cut to 60
                    newRecord[2] = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec.";
                    newRecord[3] = 200d;
                    writer.WriteRecord(newRecord);
                    writer.EndFile();
                }
            }
        }